這是一個非常特殊的情況,但我是C++的新手,並且不理解我爲項目euler網站編寫的這個程序的輸出。循環中的C++範圍
int x = 999;
int y = 999;
string result;
while (x > 320)
{
while(y > 320)
{
int inttoconvert = (x*y);
cout<<inttoconvert<<"<-----This is product"<<endl;
string result;
ostringstream convert;
convert << inttoconvert;
result = convert.str();
if (result[0] == result[5] && result[1] == result[4] && result[2] == result[3])
{
cout<<"The largest palindrome of 2 3-digit numbers is "<<result<<endl;
y = 0;
x = 0;
}
else
{
cout<<y<<endl;
y--;
}
}//end while
cout<<"this is x---->"<<x<<endl;
x--;
}//end while
輸出顯示X減Y已遞減到321只後,但隨後一再遞減,程序不進入第二而再次循環。
我開始變得疑神疑鬼關於視覺表達
你期望發生的?一旦'y <= 320','y'永遠不會再次增加,所以代碼不會有任何理由多次進入內部循環。 – Mankarse
這不是一個範圍界定問題,而是一個邏輯/算法問題。 – squiguy
錯誤在於你的代碼,而不是編譯器。 –