首页 > 试题广场 >

坐标移动

[编程题]坐标移动
  • 热度指数:563949 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。

输入:

合法坐标为A(或者D或者W或者S) + 数字(两位以内)

坐标之间以;分隔。

非法坐标点需要进行丢弃。如AA10;  A1A;  $%$;  YAD; 等。

下面是一个简单的例子 如:

A10;S20;W10;D30;X;A1A;B10A11;;A10;

处理过程:

起点(0,0)

+   A10   =  (-10,0)

+   S20   =  (-10,-20)

+   W10  =  (-10,-10)

+   D30  =  (20,-10)

+   x    =  无效

+   A1A   =  无效

+   B10A11   =  无效

+  一个空 不影响

+   A10  =  (10,-10)

结果 (10, -10)

数据范围:每组输入的字符串长度满足 ,坐标保证满足 ,且数字部分仅含正数

输入描述:

一行字符串



输出描述:

最终坐标,以逗号分隔

示例1

输入

A10;S20;W10;D30;X;A1A;B10A11;;A10;

输出

10,-10
示例2

输入

ABC;AKL;DA1;

输出

0,0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    int x, y;
    char *str;
    str=malloc(10000);
    fgets(str,10000,stdin);
    int len=strlen(str);
    for(int i=0;i<len;i++)
    {
        if(str[i]==';')
            str[i]='\0';
    }
    int i=0;
    int a=0;
    while(a<len-1)
    {
        if(str[0]=='W'||str[0]=='A'||str[0]=='S'||str[0]=='D')
        {
            int flag=0;
            if(strlen(str)>1)
            {
                for(int i=1;i<strlen(str);i++)
                {
                    if(str[i]<'0'||str[i]>'9')
                        flag=1;
                }
            }
            if(flag==0)
            {
                int c=atoi(str+1);
                if(str[0]=='W')
                    y+=c;
                else if(str[0]=='S')
                    y-=c;
                else if(str[0]=='A')
                    x-=c;
                else if(str[0]=='D')
                    x+=c;
            }
        }
        i=strlen(str)+1;
        a+=i;
        str = str+i;
    }
    printf("%d,%d",x,y);
    return 0;
}
发表于 2024-04-06 18:33:41 回复(0)
#include <stdio.h>
#include <string.h>
int main()
{
    int x=0,y=0;
    char str[10001]={0};
    fgets(str,10001,stdin);
    int len=strlen(str);
    int i=0,j=0;  /*j指向冒号,i指向冒号后第一个字符*/
    while(j++<=len-2)
    {
        if(str[j]==';')
        {
            int mov=0;
            if((j-i)==2&&'0'<=str[i+1]&&str[i+1]<='9')
                mov=str[i+1]-48;
            if((j-i)==3&&'0'<=str[i+1]&&str[i+1]<='9'&&'0'<=str[i+2]&&str[i+2]<='9')
                mov=10*(str[i+1]-48)+str[i+2]-48;
            switch(str[i])
            {
                case 'A':x-=mov;break;
                case 'D':x+=mov;break;
                case 'S':y-=mov;break;
                case 'W':y+=mov;break;
            }
            i=j+1;
        }
    }
    printf("%d,%d",x,y);
    return 0;
}

发表于 2024-03-18 00:12:52 回复(0)
#include<stdio.h>
struct pos{
int x;
int y;
};
struct pos cur={0,0};
void move(char *p){
char dir=p[0];
int n1=0,n2=0,d=0,len;
len=strlen(p);
if(len>3)
return;
if(p[1]>='0'&&p[1]<='9')
n1=p[1]-'0';
else
return;
if(p[2]!=0){
if(p[2]>='0'&&p[2]<='9')
n2=p[2]-'0';
else
return;
}
if(p[2]!=0)
d=n1*10+n2;
else
d=n1;
switch(dir){
case'A':
cur.x-=d;
break;
case'D':
cur.x+=d;
break;
case'W':
cur.y+=d;
break;
case'S':
cur.y-=d;
break;
default;
break;
}
}
int main(){
char op[100]={0};
char str[2000]={0};
int i,j,v,len;
while(scanf("%s",str)!=EOF){
len=strlen(str);
i=0;
while(i<len){
memset(op,0,100);
j=0;
while(str[i]!=';'&&i<len){
if(j<100)
op[j++]=str[i++];
}
i++;
move(op);
}
printf("%d,%d\n",cur.x,cur.y);
}
return 0;
}
编辑于 2024-01-26 11:02:45 回复(0)
看起来很简单的题还能被阴,差得远啊
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
char InputString[10001] = { 0 };
int Coordinate[2] = { 0 };

bool IsLegalNumber(char Temp) {
    if ((Temp >= '0') &&
            (Temp <= '9')
       ) {
        return true;
    } else {
        return false;
    }
}


bool IsLegalOrder(char Order[3]) {
    if ((Order[0] == 'W') ||
            (Order[0] == 'A') ||
            (Order[0] == 'S') ||
            (Order[0] == 'D')
       ) {
        if (IsLegalNumber(Order[1])) {
            if(Order[2] != ';')
            {
                if(IsLegalNumber(Order[2]))
                {
                    return true;
                }else {
                    return false;
                }
            }
            else {
                return true;
            }
            
        } else {
            return false;
        }
    } else {
        return false;
    }
}

int main() {
    scanf("%s", InputString);
    for (int i = 0; i < strlen(InputString); i++) {
        if (IsLegalOrder(&InputString[i])) {
            char Temp[3] = { 0 };
            int Step = 0;
            Temp[0] = InputString[i + 1];
            if( InputString[i + 2] != ';')
            {
            Temp[1] = InputString[i + 2];
            Temp[2] = '\0';
            }
            else {
                Temp[1] = '\0';
            }
            Step = atoi(Temp);
            switch (InputString[i]) {
                case 'W':
                    Coordinate[1] += Step;
                    break;
                case 'S':
                    Coordinate[1] -= Step;
                    break;
                case 'A':
                    Coordinate[0] -= Step;
                    break;
                case 'D':
                    Coordinate[0] += Step;
                    break;
                default:
                    break;

            }

        } else {
            while (InputString[i] != ';') {
                i++;
            }
        }
    }
    printf("%d,%d", Coordinate[0], Coordinate[1]);
    return 0;
}


编辑于 2024-01-08 17:42:16 回复(0)
思路:
(1)直接将;替换为\0,那么就分出了多段字符串,就好操作了
(2)再从头开始遍历,按条件进行一步一步排除,最终合法的子串将后面的转为int,即可
关键点:
(1)对于每次用例都用的,或是计数的这些遍量,注意循环前进行清0,要养成习惯,如x,y count。
(2)每段字符串处理结束后,i的跳增:i+=tmp_len-1;很关键
(3)要仔细,比如代码中标记易错的地方
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    int x, y, len, i, j, tmp_len, count = 0;
    char str[10001] = {0};
    while(scanf("%s", str) != EOF) {
        x = y = 0;
        len = strlen(str);
        for(i = 0; i < len; i++) {
            if(str[i] == ';')
                str[i] = '\0';
        }

        for(i = 0; i < len; i++) {
            if(str[i] != '\0')
            {
                tmp_len = strlen(&str[i]);
                if(tmp_len <= 3 && tmp_len >= 2)
                {
                    count = 0;
                    for(j = i; j < i + tmp_len; j++) {  //此处易错:忘记加i
                        if(str[j] >= 'A' && str[j] <= 'Z')
                        {
                            count++;
                        }
                    }
                    if((count == 1) && (str[i] >= 'A' && str[i] <= 'Z'))
                    {
                        switch (str[i]) {
                            case 'A':
                                x -= atoi(&str[i+1]);
                                break;
                            case 'D':
                                x += atoi(&str[i+1]);
                                break;
                            case 'W':
                                y += atoi(&str[i+1]);
                                break;
                            case 'S':
                                y -= atoi(&str[i+1]);
                                break;
                            default:
                                break;                        
                        }

                    }
                }
                i+= tmp_len - 1;
            }
        }

        printf("%d,%d\n", x, y);       

    }

    return 0;
}


编辑于 2023-12-04 23:19:39 回复(1)
int move_coord(char *str, int *x, int *y)
{
    if((str[1] < '0' || str[1] > '9') ||
       (str[2] < '0' || str[2] > '9'))
        return 0;
    else
        return (str[1] - '0') *10 + (str[2] - '0');
}

void process_coord(char *str, int *x, int *y)
{
    switch (str[0])
    {
        case 'W':
            *y += move_coord(str, x, y);
            break;
        case 'A':
            *x -= move_coord(str, x, y);
            break;
        case 'S':
            *y -= move_coord(str, x, y);
            break;
        case 'D':
            *x += move_coord(str, x, y);
            break;
        default:
            break;
    }
    return;
}

int main() {
    int x = 0, y = 0;
    char str[10001];
    char substr[4];
    char *p, *q;
    int len = 0;

    memset(str, 0, sizeof(str));
    memset(substr, 0, sizeof(substr));
    scanf("%s", str);
    p = str;
    while(*p != 0)
    {
        q = p;
        while(*p != ';')
        {
            p++;
        }
        len = p - q;
        if(len != 3)
        {
            p++;
            continue;
        }
        memcpy(substr, q, len);
        process_coord(substr, &x, &y);
        p++;
    }
    printf("%d,%d\n", x, y);
    return 0;
}
S87;S7;W56;S75;A8;S84;W23;W19;W40;D73;S87;A39;W97;W78;A53;D16;D15;A50;W41;S87;D47;W56;D56;A23;A91;S25;D61;D53;D58;W88;W58;S61;D69;W74;D89;A92;D39;D62;S78;W72;W73;W35;S76;W35;S36;W39;A4;
我算答案是290,188,用计算器算了一遍也是这个。但系统说答案是278,181。有谁看出来原因吗?
发表于 2023-11-13 20:44:39 回复(1)
// 分享一下我自己的解法

#include <stdio.h>
#include <string.h>

int main() {
    long int x = 0;
    long int y = 0;
    char str[10001] = {0};
    scanf("%s", str);
    int len = strlen(str);
    int pointer = 0;
    int probe = 0;
    int OkFlag = 1;
    int Direction = 0; // A: 1; D: 2; W: 3; S: 4;
    int TempTen = 0; // 十位数
    int TempOne = 0; // 个位数

    while(pointer < len){

        OkFlag = 1;
        switch(str[pointer]){
            case 'A':
                Direction = 1;
                break;
            case 'D':
                Direction = 2;
                break;
            case 'W':
                Direction = 3;
                break;
            case 'S':
                Direction = 4;
                break;
            default:
                OkFlag = 0;
        }

        if(OkFlag){
            probe++;
            if(str[probe]<='9' && str[probe]>='0'){
                TempTen = str[probe] - '0';
                probe++;
                if(str[probe]<='9' && str[probe]>='0'){
                    TempOne = str[probe] - '0';
                    probe++;
                    if(str[probe]==';'){
                        switch(Direction){
                            case 1:
                                x -= 10*TempTen + TempOne;
                                break;
                            case 2:
                                x += 10*TempTen + TempOne;
                                break;
                            case 3:
                                y += 10*TempTen + TempOne;
                                break;
                            case 4:
                                y -= 10*TempTen + TempOne;
                                break;
                        }
                    }else{
                        OkFlag = 0;
                    }
                }else if(str[probe]==';'){
                    switch(Direction){
                        case 1:
                            x -= TempTen;
                            break;
                        case 2:
                            x += TempTen;
                            break;
                        case 3:
                            y += TempTen;
                            break;
                        case 4:
                            y -= TempTen;
                            break;
                    }
                }else{
                    OkFlag = 0;
                }
            }else{
                OkFlag = 0;
            }
        }

        while(str[pointer++] != ';'){}
        probe = pointer;

    }

    printf("%ld,%ld", x, y);

}

发表于 2023-11-11 20:52:15 回复(0)
#include <stdio.h>
#include <string.h>
int main() {
    char op[10000];
    int x = 0, y = 0, len, i, j, num = 0;
    scanf("%s", op);
    len = strlen(op);
    for (i = 0; i < len; i++) {
        j = i + 1;
        num = 0;
        if (op[i] == 'A' || op[i] == 'W' || op[i] == 'D' || op[i] == 'S') {
            while (op[j] != ';') {
                if (op[j] >= '0' && op[j] <= '9') {
                    num = num * 10 + (op[j] - '0');
                    j++;
                } else break;
                if (op[j] == ';') {
                    if (i < 3) {
                        switch (op[i]) {
                            case 'A':x = x - num;break;
                            case 'D':x = x + num;break;
                            case 'W':y = y + num;break;
                            case 'S':y = y - num;break;
                        }
                    }
                    if (i > 3) {
                        if (op[i - 1] == ';') {
                            switch (op[i]) {
                                case 'A':x = x - num;break;
                                case 'D':x = x + num;break;
                                case 'W':y = y + num;break;
                                case 'S':y = y - num;break;
                            }
                        }
                    }
                }
            }
        }
    }
    printf("%d,%d", x, y);
    return 0;
}
发表于 2023-09-11 15:39:55 回复(0)
#include<stdio.h>
#include<string.h>

int main(){
    char str[10000];
    int i,j,num=0,n,x=0,y=0;

    scanf("%s",str);
    int str_len=strlen(str);

    for(i=0;i<str_len;i++){
        if(i==0){
            j=i+1;
            if(str[i]=='A'||str[i]=='W'||str[i]=='D'||str[i]=='S'){
                while(str[j]!=';'){
                    if(str[j]>='0'&&str[j]<='9'){
                        num=num*10+(str[j]-'0');
                        j++;
                    }
                    else break;
                }
                if(str[j]==';'&&j!=1){
                    if(str[i]=='A')x=x-num;
                    if(str[i]=='D')x=x+num;
                    if(str[i]=='W')y=y+num;
                    if(str[i]=='S')y=y-num;
                }

            }

        }
        num=0;
        if(str[i]==';'){

                if(str[i+1]=='A'||str[i+1]=='W'||str[i+1]=='D'||str[i+1]=='S'){
                    j=i+1;
                    while(str[++j]!=';'){
                        if(str[j]>='0'&&str[j]<='9'){
                            num=num*10+(str[j]-'0');}
                        else break;
                    }
                    if(str[j]==';'&&j!=(i+2)){
                        if(str[i+1]=='A')x=x-num;
                        if(str[i+1]=='D')x=x+num;
                        if(str[i+1]=='W')y=y+num;
                        if(str[i+1]=='S')y=y-num;
                    }
                }
        }
    }

    printf("%d,%d",x,y);
    return 0;
}
发表于 2023-07-19 03:00:33 回复(0)
#include <stdio.h>
#include <string.h>
int judge(char *str)
{
    int num=0;
    int len=strlen(str);
    for(int i=1;i<len;i++)//i=0对应字母,i=1开始读入坐标长度
    {
        if(str[i]>='0'&&str[i]<='9')
        {
            num=num*10+(str[i]-'0');
        }
        else {
        return 0;//非法不输出
        }
    }
    return num;
}
int main() {
    int x=0, y=0;
    char str[10001];
    char str_tmp[10001][10];//每个';'进行分割,存储被';'分开的指令
    scanf("%s",str);
    int len=strlen(str);
    int j=0;//记录单个指令长度
    int k=0;//记录总共指令个数(包含非法的)
    for(int i=0;i<len;i++)
    {
        if(str[i]!=';')
        {
            str_tmp[k][j++]=str[i];
        }
        else {
        j=0;
        k++;
        }
    }
    for(int i=0;i<=k;i++)
    {
        if(str_tmp[i][0]=='A')
        {
            x-=judge(str_tmp[i]);
        }
        else if(str_tmp[i][0]=='D')
        {
            x+=judge(str_tmp[i]);
        }
        else if(str_tmp[i][0]=='W')
        {
            y+=judge(str_tmp[i]);
        }
        else if(str_tmp[i][0]=='S')
        {
            y-=judge(str_tmp[i]);
        }
    } 
    printf("%d,%d",x,y);
    return 0;
}

发表于 2023-04-09 00:42:01 回复(1)
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int isValidOp(const char* str)
{
    if ('A' != str[0] &&
        'W' != str[0] &&
        'S' != str[0] &&
        'D' != str[0]) {
        return 0;
    }

    for (int i = 1; i < strlen(str); i ++)
    {
        if (!isdigit(str[i]))
        {
            return  0;
        }
    }

    return 1;
}

int main() {
    char str[10001];
    char *pToke = str;
    int i = 0;
    int a[2] = {0};
    scanf("%[^\n]\n", str);
    int len = strlen(str);
    while (i < len) {
        if (';' == str[i]) {
            str[i] = '\0';
            if (isValidOp(pToke)) {
                switch (pToke[0]) {
                    case 'A':
                        a[0] -= atoi(&pToke[1]);
                        break;
                    case 'D':
                        a[0] += atoi(&pToke[1]);
                        break;
                    case 'W':
                        a[1] += atoi(&pToke[1]);
                        break;
                    case 'S':
                        a[1] -= atoi(&pToke[1]);
                        break;
                }
            }
            pToke = &str[i+1];
        }
        i ++;
    }
    printf("%d,%d\n", a[0], a[1]);
    return 0;
}
发表于 2023-03-28 21:01:25 回复(0)
#include <stdio.h>
#include <string.h>
int main() {
    char str[10001];
    scanf("%s\n", str);
    int len, count = 0;
    int x = 0, y = 0;
    int num = 0;
    len = strlen(str);
    for (int i = 0; i < len; i++) {
        count ++;
        if (str[i] == ';' && count == 4  && str[i - 2] >= '0' && str[i - 2] <= '9' &&
                str[i - 1] >= '0' && str[i - 1] <= '9' && (str[i - 3] == 'A' ||
                        str[i - 3] == 'S' || str[i - 3] == 'W' || str[i - 3] == 'D')) {
            num = (str[i - 2] - '0') * 10 + (str[i - 1] - '0');
            count = 0;
            if (str[i - 3] == 'A') {
                x = x - num;
            } else if (str[i - 3] == 'S') {
                y = y - num;
            } else if (str[i - 3] == 'W') {
                y = y + num;
            } else if (str[i - 3] == 'D') {
                x = x + num;
            }

        }
        if (str[i] == ';' && count == 3  && str[i - 1] >= '0' && str[i - 1] <= '9' && (str[i - 2] == 'A' || str[i - 2] == 'S' || str[i - 2] == 'W' || str[i - 2] == 'D')) {
            num = str[i - 1] - '0';
            count = 0;
            if (str[i - 2] == 'A') {
                x = x - num;
            } else if (str[i - 2] == 'S') {
                y = y - num;
            } else if (str[i - 2] == 'W') {
                y = y + num;
            } else if (str[i - 2] == 'D') {
                x = x + num;
            }
        }
        if (str[i] == ';') {
            count = 0;
        }
    }
    printf("%d,%d", x, y);
    return 0;
}
发表于 2023-02-28 14:01:48 回复(0)
#include <stdio.h>
#include <string.h>
int main()
{
    char pos[10000];
    int lenth,x=0,y=0;
    gets(pos);
    lenth=strlen(pos);
    for(int i=0;i<lenth;i++)
    {
        if((pos[i]==';')&&(pos[i-4]==';')||(i==3&&pos[i]==';'))
        {
            if(pos[i-3]=='A'&&pos[i-2]<='9'&&pos[i-2]>='0'&&pos[i-1]<='9'&&pos[i-1]>='0')  x=x-(pos[i-2]-48)*10-pos[i-1]+48;
           
            else if(pos[i-3]=='D'&&pos[i-2]<='9'&&pos[i-2]>='0'&&pos[i-1]<='9'&&pos[i-1]>='0') x=x+(pos[i-2]-48)*10+pos[i-1]-48;

            else if(pos[i-3]=='W'&&pos[i-2]<='9'&&pos[i-2]>='0'&&pos[i-1]<='9'&&pos[i-1]>='0') y=y+(pos[i-2]-48)*10+pos[i-1]-48;

            else if(pos[i-3]=='S'&&pos[i-2]<='9'&&pos[i-2]>='0'&&pos[i-1]<='9'&&pos[i-1]>='0') y=y-(pos[i-2]-48)*10-pos[i-1]+48;
        }


        else if((pos[i]==';')&&(pos[i-3]==';')||(i==2&&pos[i]==';'))
        {
            if(pos[i-2]=='A'&&pos[i-1]<='9'&&pos[i-1]>='0') x=x-pos[i-1]+48;

            if(pos[i-2]=='D'&&pos[i-1]<='9'&&pos[i-1]>='0') x=x+pos[i-1]-48;

            if(pos[i-2]=='W'&&pos[i-1]<='9'&&pos[i-1]>='0') y=y+pos[i-1]-48;

            if(pos[i-2]=='S'&&pos[i-1]<='9'&&pos[i-1]>='0') y=y-pos[i-1]+48;

        }
    }
    printf("%d,%d",x,y);
}

巨蠢的方法
发表于 2023-01-09 19:40:31 回复(0)
#include <stdio.h>
#include "string.h"
int main() {
    char c = 0;
    char coordinate[5] = {0};
    int step = 0, x = 0, y = 0;
    int i = 0;

    while ((c = getchar()) != '\n') {//获取坐标,使用getchar()节省空间
        if (c == ';') { //获取到分号后对之前的数据尽显处理
            if ((coordinate[1] >= '0' && coordinate[1] <= '9') && i <= 3) {
                step = 0;
                if (i == 3 && (coordinate[2] >= '0' &&
                               coordinate[2] <= '9')) {//三位坐标,坐标改变值
                    step = ( coordinate[1] - '0') * 10 + (coordinate[2] - '0');
                } else if ( i == 2) {//两位坐标,坐标改变值
                    step =  coordinate[1] - '0';
                } 
                i = 0;
                switch (coordinate[0]) {//判断坐标改变方向
                    case 'A':
                        x = x - step;
                        break;
                    case 'D':
                        x = x + step;
                        break;
                    case 'W':
                        y = y + step;
                        break;
                    case 'S':
                        y = y - step;
                        break;
                    default:
                        step = 0;
                        break;
                }
                memset(coordinate, 0, sizeof(coordinate));
            } else {
                i = 0;
                memset(coordinate, 0, sizeof(coordinate));
            }
        } else {//保存输入的字符串
            coordinate[i] = c;
            i ++;
        }
    }
    printf("%d,%d", x, y);
    return 0;
}

发表于 2022-09-26 16:05:18 回复(0)
#include <stdio.h>
#include <string.h>

int main(void)
{									//15
	char str[10000]; //= {"A10;S20;W10;D30;X;A1A;B10A11;;A10;"};
	int i,head = 0, tail = 0, len = 0, temp = 0, x = 0,y = 0;
	
	scanf("%[^\n]%*c",str);
	len = strlen(str);
	
	for(i = 0; i < len; i++)
	{
		if(str[i] == ';')
		{
			tail = i;
			
			if((tail - head == 3 )
			&& (str[head] == 'A'||str[head] == 'W'||str[head] == 'S'||str[head] == 'D' )
			&& (str[head+1]>='0'&&str[head+1]<='9')
			&& (str[head+2]>='0'&&str[head+2]<='9'))
			{
				temp = 10 * (str[head+1] - '0') + str[head+2] - '0';
			}
			else if((tail - head == 2 )
			&& (str[head] == 'A'||str[head] == 'W'||str[head] == 'S'||str[head] == 'D')
			&& (str[head+1]>='0'&&str[head+1]<='9'))
			{
				temp = str[head+1] - '0';
			}
			else
			{
				head = tail+1;
				continue;
			}
			
			switch(str[head])
			{
				case 'A':
					x-=temp;
					break;
				case 'W':
					y+=temp;
					break;
				case 'S':
					y-=temp;
					break;
				case 'D':
					x+=temp;
					break;	
			}
			
			head = tail+1;
		}
	}
	
	printf("%d,%d",x,y);
	
	
	return 0;
}

发表于 2022-09-15 00:02:55 回复(0)
使用指针解决较为方便
需要注意的是:1.数字最多为2位有可能是1位,所以引入jud进行判断
2.B10A11;情况无效需要判断p-4||p-3是否为';'
#include <stdio.h>

int main()
{
    char str[10000];
    int x=0,y=0,num,jud=1;//如果是一位数则jud=1,如果是两位数jud=0
    gets(str);
    char *p=str,*p1=NULL;
    while(*p!='\0')
    {
        jud=1;//默认为一位
        if(*p=='A'||*p=='D'||*p=='S'||*p=='W')
        {
            p1=p;
            p++;
            if('0'<=*p&&*p<='9')
            {
                num=(*p)-'0';
                p++;
                if('0'<=*p&&*p<='9')
                {
                    num=num*10+(*p)-'0';
                    p++;
                    jud=0;//两位数
                }
                if(*p==';')
                {
                    if(p>str+4-jud)
                    {
                        if(*(p-(4-jud))==';')
                        {
                            if(*p1=='A') x=x-num;
                            else if(*p1=='D') x=x+num;
                            else if(*p1=='W') y=y+num;
                            else y=y-num;
                        }
                    }
                    else
                    {
                        if(*p1=='A') x=x-num;
                        else if(*p1=='D') x=x+num;
                        else if(*p1=='W') y=y+num;
                        else y=y-num;
                    }
                }
            }
        }
        p++;
    }
    printf("%d,%d",x,y);
    return 0;
}

发表于 2022-07-13 16:23:48 回复(4)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Pos
{
    int x;
    int y;
};
int main()
{
    char str[100001] = { 0 };
    int len = 0;
    int i = 0;
    int mem = 0;
    int j = 0;
    char tmp[2] = { 0 };
    int num = 0;
    struct Pos P = { 0 };
    while (scanf("%s", str) != EOF)
    {
        len = strlen(str);
        for (i = 0; i < len; i++)
        {
            int count = 0;
            if (str[i] == ';')
            {
                for (j = mem; j < i; j++)//判断是否有3个字符
                {
                    count++;
                }
                if (count == 3 || count == 2)//处理
                {
                    if (count == 3)
                    {
                        tmp[0] = str[mem + 1];
                        tmp[1] = str[mem + 2];
                    }
                    if (count == 2)
                    {
                        tmp[0] = '0';
                        tmp[1] = str[mem + 1];
                    }

                    if (tmp[1] >= '0' && tmp[1] <= '9' && tmp[0] >= '0' && tmp[0] <= '9')
                    {
                        //转换成整型
                        num = atoi(tmp);
                        switch (str[mem])
                        {
                        case 'A':
                            P.x -= num;
                            break;
                        case 'D':
                            P.x += num;
                            break;
                        case 'W':
                            P.y += num;
                            break;
                        case 'S':
                            P.y -= num;
                            break;
                        default:
                            break;
                        }
                    }



                }

                mem = i + 1;
            }

        }
        printf("%d,%d", P.x, P.y);
    }



    return 0;
}

发表于 2022-07-08 20:11:56 回复(1)

问题信息

难度:
38条回答 92823浏览

热门推荐

通过挑战的用户

查看代码