题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <stdio.h>
#define N 10000
is_alpha(char ch)
{
return ch >= 'A' && ch <= 'Z';
}
is_number(char ch)
{
return ch >= 48 && ch <= 57;
}
int number(char* ch)
{
if (is_number(*ch) && is_number(*(ch + 1)))
return (*(ch + 1) - 48) + (*ch - 48) * 10;
if (is_number(*ch))
return *ch - 48;
return 0;
}
int main(void)
{
char ch[N];
char* p = ch;
int x, y, i;
i = x = y = 0;
gets(ch);
while (*p != '\0')
{
if (p == ch)
{
if ((is_alpha(p[0]) && is_number(p[1]) && p[2] == ';') || (is_alpha(p[0]) && is_number(p[1]) && is_number(p[2]) && p[3] == ';'))
{
int n = number(p + 1);
switch (p[0])
{
case 'A': x -= n; break;
case 'D': x += n; break;
case 'W': y += n; break;
case 'S': y -= n; break;
default:break;
}
if (p[2] == ';')
p = p + 2;
else
p += 3;
while (*p != ';')
p++;
}
continue;
}
if (*(p-1)==';'&&(is_alpha(p[0]) && is_number(p[1]) && p[2] == ';') || (*(p - 1) == ';' && is_alpha(p[0]) && is_number(p[1]) && is_number(p[2]) && p[3] == ';'))
{
int n = number(p+1);
switch (p[0])
{
case 'A': x -= n; break;
case 'D': x += n; break;
case 'W': y += n; break;
case 'S': y -= n; break;
default:break;
}
if (p[2] == ';')
p = p + 2;
else
p += 3;
while (*p != ';')
p++;
}
p++;
}
printf("%d,%d", x, y);
return 0;
}
#笨办法#