题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
思路:暴力解法,使用strtok分割字符串,然后判断每个字符串是否有效,有效就进行移动,无效则break;
#include<stdio.h>
int main()
{
char arr[10001] = {0};
gets(arr);
int x = 0;
int y = 0;
char* par = strtok(arr,";");
while(par!=NULL)
{
switch(par[0])
{
case 'A':
if(par[1]>='0'&&par[1]<='9' && par[2]>='0'&&par[2]<='9')
{
int count = (par[1]-'0')*10+(par[2]-'0');
x-=count;
}
else if(par[1]>='0'&&par[1]<='9' && par[2]=='\0')
{
int count = (par[1]-'0');
x-=count;
}
break;
case 'D':
if(par[1]>='0'&&par[1]<='9' && par[2]>='0'&&par[2]<='9')
{
int count = (par[1]-'0')*10+(par[2]-'0');
x+=count;
}
else if(par[1]>='0'&&par[1]<='9' && par[2]=='\0')
{
int count = (par[1]-'0');
x+=count;
}
break;
case 'W':
if(par[1]>='0'&&par[1]<='9' && par[2]>='0'&&par[2]<='9')
{
int count = (par[1]-'0')*10+(par[2]-'0');
y+=count;
}
else if(par[1]>='0'&&par[1]<='9' && par[2]=='\0')
{
int count = (par[1]-'0');
y+=count;
}
break;
case 'S':
if(par[1]>='0'&&par[1]<='9' && par[2]>='0'&&par[2]<='9')
{
int count = (par[1]-'0')*10+(par[2]-'0');
y-=count;
}
else if(par[1]>='0'&&par[1]<='9' && par[2]=='\0')
{
int count = (par[1]-'0');
y-=count;
}
break;
default:
break;
}
par = strtok(NULL,";");
}
printf("%d,%d\n",x,y);
return 0;
}