我试图解决该问题,并且细分错误不断发生,我已经发布了问题,但是有人关闭了该问题,并给了我没有任何帮助的建议。它要么什么都不做,要么给我分段错误。相同的方法在C ++和Python中有效,但在C中执行时会出错。例如,如果我在输入x = 3,y = 4,方向= N,指令= A后给出它,则应返回3,5 N,但它什么也不做,返回3,4,N。如果我给出了多个步骤,则会给我带来细分错误。我浏览了该网站,但似乎找不到解决方案。在此先感谢:)
int x ;
int y ;
int d;
char *direction; //edited in, I left it out when posting originally but it was in the code
char *instructions[100] ;
scanf("%d", &x) ;
scanf("%d", &y) ;
scanf("%s", &direction) ; //I get char input for direction but then need to change it to int to get d
scanf("%s", instructions) ;
\\And here is the problematic part of the code:
int l = strlen(&instructions);
for (int i = 0; i < l; i++){
if (strcmp(instructions[i],"L") == 0){
d = d - 1 ;
}
else if (strcmp(instructions[i],"R") == 0){
d = d + 1 ;
}
else if (strcmp(instructions[i],"A") == 0 && (d % 4 == 0) && (y - 1 > -1)){
y = y - 1 ;
}
else if (strcmp(instructions[i],"A") == 0 && (d % 4 == 3) && (x - 1 > -1)){
x = x - 1 ;
}
else if (strcmp(instructions[i],"A") == 0 && (d % 4 == 2)){
y = y + 1 ;
}
else if (strcmp(instructions[i],"A") == 0 && (d % 4 == 1)){
x = x + 1 ;
}
else {
continue;
};
};