题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void movePosition(char *str, int *x, int *y)
{
int flag = 0;
int val = 0;
if(str[0] == 'A' || str[0] == 'S' || str[0] == 'W' || str[0] == 'D')
{
if(str[1] >= '0' && str[1] <= '9')
{
if(str[2] == ';')
{
val = str[1] - '0';
flag = 1;
}
if(str[2] >= '0' && str[2] <= '9')
{
val = (str[1] - '0') * 10 + (str[2] - '0');
flag = 1;
}
}
}
if(flag == 1)
{
switch(str[0])
{
case 'A':
*x = *x - val;
break;
case 'S':
*y = *y - val;
break;
case 'W':
*y = *y + val;
break;
case 'D':
*x = *x + val;
break;
default:
break;
}
}
}
int main(void)
{
char str[10001];
int len = 0;
int i = 0;
int x = 0, y = 0;
char str1[4] = {0};
int cnt = 0;
while(scanf("%s", str) != EOF)
{
len = strlen(str);
for(i = 0; i < len; i++)
{
if(cnt < 5)
{
str1[cnt] = str[i];
}
cnt++;
if(str[i] == '\0' || str[i] == ';')
{
if(cnt < 5)
{
movePosition(str1, &x, &y);
}
memset(str1, 0, 4);
cnt = 0;
}
}
printf("%d,%d\n", x, y);
}
return 0;
}
#include <string.h>
#include <stdlib.h>
void movePosition(char *str, int *x, int *y)
{
int flag = 0;
int val = 0;
if(str[0] == 'A' || str[0] == 'S' || str[0] == 'W' || str[0] == 'D')
{
if(str[1] >= '0' && str[1] <= '9')
{
if(str[2] == ';')
{
val = str[1] - '0';
flag = 1;
}
if(str[2] >= '0' && str[2] <= '9')
{
val = (str[1] - '0') * 10 + (str[2] - '0');
flag = 1;
}
}
}
if(flag == 1)
{
switch(str[0])
{
case 'A':
*x = *x - val;
break;
case 'S':
*y = *y - val;
break;
case 'W':
*y = *y + val;
break;
case 'D':
*x = *x + val;
break;
default:
break;
}
}
}
int main(void)
{
char str[10001];
int len = 0;
int i = 0;
int x = 0, y = 0;
char str1[4] = {0};
int cnt = 0;
while(scanf("%s", str) != EOF)
{
len = strlen(str);
for(i = 0; i < len; i++)
{
if(cnt < 5)
{
str1[cnt] = str[i];
}
cnt++;
if(str[i] == '\0' || str[i] == ';')
{
if(cnt < 5)
{
movePosition(str1, &x, &y);
}
memset(str1, 0, 4);
cnt = 0;
}
}
printf("%d,%d\n", x, y);
}
return 0;
}
安克创新 Anker公司福利 782人发布
