所以这里我有一个程序,显示解决河内谜题的模拟。我的程序在大多数情况下工作正常,但在应该的时候不会终止。一旦所有光盘都在C挂钩(C C C)上,它应该终止,但我的继续到第四张没有给出的光盘。应该只有三张光盘。任何帮助将不胜感激!
#include <iostream>
using namespace std;
const int num = 3;
const char from_peg = 'A';
const char to_peg = 'B';
const char temp_peg = 'C';
char position[num];
void moveDiscs(int num,int disc,char source,char dest, char spare){
if (disc == 0){
position[disc]=dest;
cout<<"Moved disc "<<disc+1<<" to peg "<<dest;
cout<<" ( ";
for(int i = 0;i<num;i++){
cout<<position[i]<<" ";
}
cout<<")"<<endl;
}else{
moveDiscs(num,disc-1,source,spare,dest);
position[disc]=dest;
cout<<"Moved disc "<<disc+1<<" to peg "<<dest;
cout<<" ( ";
for(int i = 0;i<num;i++){
cout<<position[i]<<" ";
}
cout<<")"<<endl;
moveDiscs(num,disc-1,spare,dest,source);
}
}
int main() {
cout<<"Starting Position for 3 discs are (";
for(int i = 0;i<num;i++){
position[i]='A';
cout<<position[i]<<" ";
}
cout<<")"<<endl;
moveDiscs(3,3,from_peg,to_peg,temp_peg);
return 0;
}
输出:
Starting Position for 3 discs are (A A A )
Moved disc 1 to peg C ( C A A )
Moved disc 2 to peg B ( C B A )
Moved disc 1 to peg B ( B B A )
Moved disc 3 to peg C ( B B C )
Moved disc 1 to peg A ( A B C )
Moved disc 2 to peg C ( A C C )
Moved disc 1 to peg C ( C C C )
Moved disc 4 to peg B ( C C C )
Moved disc 1 to peg B ( B C C )
Moved disc 2 to peg A ( B A C )
Moved disc 1 to peg A ( A A C )
Moved disc 3 to peg B ( A A B )
Moved disc 1 to peg C ( C A B )
Moved disc 2 to peg B ( C B B )
Moved disc 1 to peg B ( B B B )
答案 0 :(得分:0)
由于@Das指出你的光盘从0开始,但你传递了Dim strName As String
strName = InputBox(Prompt:="Save To:", Title:="Save file to:", _
Default:="C:\Users\PRESTONAVH\Desktop\Task Order Files\")
If strName = vbNullString Then
Exit Sub
Else
End If
Dim intValue1 As Integer
Dim intValue2 As Integer
intValue1 =
ActiveDocument.Sections(1).Range.Information(wdActiveEndPageNumber) + 1
intValue2 =
ActiveDocument.Sections(2).Range.Information(wdActiveEndPageNumber)
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
strName & "PLAN.pdf", _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportFromTo, From:=intValue1,
To:=intValue2, Item:= _
wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
的数量,从discs = 3
到0
的光盘总数实际上是4(同样的错误通常发生在数组长度)。实际上,您应该将3
设置为等于2.