题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include<stdio.h>
#include<string.h>
int main(){
char s[2048],*p,c,tmp[100];//c为A/D/W/S字母
int len,x=0,y=0,n;
// fgets(s,2048,stdin);//*从输入流stdin即输入缓冲区中读取2048个字符到字符数组str中*/
gets(s);
len=strlen(s)-1;
s[len]='\0';
p=strtok(s,";");//字符串分割,s是被分割的字符串,";"为分割符
while(p!=NULL){//sscanf(p, "%c%d%s",&c,&n,tmp)==2
if(strlen(p)<=3&&sscanf(p, "%c%d%s",&c,&n,tmp)==2){
switch(c){
case 'A':
x-=n;
break;
case 'S':
y-=n;
break;
case 'W':
y+=n;
break;
case 'D':
x+=n;
break;
}
}
p=strtok(NULL,";");//说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。
// strtok在s中查找包含在delim中的字符并用NULL(’\0’)来替换,直到找遍整个字符串。
// 返回指向下一个标记串。当没有标记串时则返回空字符NULL。
}
printf("%d,%d",x,y);
}
#include<string.h>
int main(){
char s[2048],*p,c,tmp[100];//c为A/D/W/S字母
int len,x=0,y=0,n;
// fgets(s,2048,stdin);//*从输入流stdin即输入缓冲区中读取2048个字符到字符数组str中*/
gets(s);
len=strlen(s)-1;
s[len]='\0';
p=strtok(s,";");//字符串分割,s是被分割的字符串,";"为分割符
while(p!=NULL){//sscanf(p, "%c%d%s",&c,&n,tmp)==2
if(strlen(p)<=3&&sscanf(p, "%c%d%s",&c,&n,tmp)==2){
switch(c){
case 'A':
x-=n;
break;
case 'S':
y-=n;
break;
case 'W':
y+=n;
break;
case 'D':
x+=n;
break;
}
}
p=strtok(NULL,";");//说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。
// strtok在s中查找包含在delim中的字符并用NULL(’\0’)来替换,直到找遍整个字符串。
// 返回指向下一个标记串。当没有标记串时则返回空字符NULL。
}
printf("%d,%d",x,y);
}