-4
我需要幫助找到我通過Thor挑戰Codeingame上的x和y值的差異替代方法所犯的錯誤。爲什麼這個代碼只輸出「」? (Codeingame的Thor挑戰)
這裏是遊戲圈的代碼:
// game loop
while (1) {
int remainingTurns; // The remaining amount of turns Thor can move. Do not remove this line.
cin >> remainingTurns; cin.ignore();
int difx = lightX-initialTX;
int dify = lightY-initialTY;
float mathquot=dify/difx;
string out ="";
if(difx==0 || dify==0){
if(difx=0){
if(dify<0) out="W"; initialTY-=1;
if(dify>0) out="E"; initialTY+=1;
}
if(dify=0){
if(difx<0) out="N"; initialTX-=1;
if(difx>0) out="S"; initialTX+=1;
}
}else{
if(mathquot>0 && (difx>0 && dify>0)) out="SE"; initialTY +=1; initialTX+=1;
if(mathquot>0 && (difx<0 && dify<0)) out="NW"; initialTY -=1; initialTX-=1;
if(mathquot<0 && difx<0) out="SW"; initialTY +=1; initialTX-=1;
if(mathquot<0 && dify<0) out="NE"; initialTY -=1; initialTX+=1;
}
// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;
// A single line providing the move to be made: N NE E SE S SW W or NW
cout << out << endl;
}
歡迎來到Stack Overflow!這聽起來像你可能需要學習如何使用調試器來遍歷代碼。使用一個好的調試器,您可以逐行執行您的程序,並查看它與您期望的偏離的位置。如果你打算做任何編程,這是一個重要的工具。進一步閱讀:** [如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver