题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <ctype.h>
#include <stdio.h>
#include "string.h"
typedef struct{
int x;
int y;
}POS;
POS pos;
void _move(char direction, int value)
{
switch(direction)
{
case 'A':
{
pos.x -= value;
break;
}
case 'D':
{
pos.x += value;
break;
}
case 'W':
{
pos.y += value;
break;
}
case 'S':
{
pos.y -= value;
break;
}
}
}
int main() {
char str[10000] = {0};
scanf("%s",str);
char* p = strtok(str, ";");
int num;
int len;
while(p!=NULL)
{
len = strlen(p);
if(len==2)
{
if((p[0]=='A'||p[0]=='D'||p[0]=='W'||p[0]=='S')&&isdigit(p[1]))
{
num = p[1] - '0';
_move(p[0],num);
}
}
else if(len==3)
{
if((p[0]=='A'||p[0]=='D'||p[0]=='W'||p[0]=='S')&&isdigit(p[1])&&isdigit(p[2]))
{
num = (p[1] - '0')*10+p[2] -'0';
_move(p[0],num);
}
}
p = strtok(NULL, ";");
}
printf("%d,%d",pos.x,pos.y);
return 0;
}
查看19道真题和解析