题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
用栈的思想解,把每两个分号之间的子串压入栈中,然后判断和执行坐标移动操作
#include<stdio.h>
#include<string.h>
int main(void)
{
char str[10000]={0};
scanf("%[^\n]",&str);
int x=0;
int y=0;
char order_stack[100]={0};
int o_size=0;
//判断每步操作是否有效
int len=strlen(str);
int flag=0;
int temp=0;
for(int i=0;i<len;i++)
{
if((str[i]>='0'&&str[i]<='9')||(str[i]>='A'&&str[i]<='Z'))
{
order_stack[o_size++]=str[i];
}
if(str[i]==';')
{
if(order_stack[0]=='W'||order_stack[0]=='A'||order_stack[0]=='S'||order_stack[0]=='D')
{
for(int j=1;j<o_size;j++)
{
if(!(order_stack[j] >= '0' && order_stack[j] <= '9'))
{
flag++;
}
}
if(flag==0)
{
for(int k=1;k<o_size;k++)
{
temp=temp*10+(order_stack[k]-'0');
}
if (order_stack[0] == 'W') { y += temp; }
else if (order_stack[0] == 'S') { y -= temp; }
else if (order_stack[0] == 'A') { x -= temp; }
else if (order_stack[0] == 'D') { x += temp; }
}
}
flag=0;
temp=0;
o_size=0;order_stack[0]='0';
}
}
//for(int i=0;i<o_size;i++){printf("%c ",order_stack[i]);}
//printf("%d",temp);
printf("%d,%d",x,y);
return 0;
}
小米集团公司氛围 371人发布