首页 > 试题广场 >

坐标移动

[编程题]坐标移动
  • 热度指数:597590 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}我们定义一个无限大的二维网格上有一个小人,小人初始位置为 (0,0) 点,小人可以读取指令上下左右移动。

\hspace{15pt}一个合法的指令由三至四个符号组成:
\hspace{23pt}\bullet\,第一个符号为 \texttt{ 中的一个,代表小人移动的方向;分别代表向左、向右、向上、向下移动;记某个时刻小人的坐标为 (x,y) ,向左移动一格即抵达 (x-1,y) 、向右移动一格即抵达 (x+1,y) 、向上移动一格即抵达 (x,y+1) 、向下移动一格即抵达 (x,y-1)
\hspace{23pt}\bullet\,最后一个符号为 \texttt{ ,代表指令的结束,该符号固定存在;
\hspace{23pt}\bullet\,中间为一个 \texttt{1-99} 的数字,代表小人移动的距离。
\hspace{15pt}如果你遇到了一个不合法的指令,则直接忽略;例如,指令 \texttt{ 是不合法的,因为 \texttt{100} 超出了 \texttt{1-99} 的范围;\texttt{ 也是不合法的,因为 \texttt{Y} 不是 \texttt{ 中的一个。

\hspace{15pt}输出小人最终的坐标。

输入描述:
\hspace{15pt}在一行上输入一个长度 1 \leqq {\rm length}(s) \leqq 10^4 ,仅由可见字符构成的字符串 s ,代表输入的指令序列。


输出描述:
\hspace{15pt}在一行上输出一个两个整数,代表小人最终位置的横纵坐标,使用逗号间隔。
示例1

输入

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

输出

10,-10

说明

\hspace{15pt}对于这个样例,我们模拟小人的移动过程:
\hspace{23pt}\bullet\,第一个指令 \texttt{ 是合法的,向左移动 10 个单位,到达 (-10,0) 点;
\hspace{23pt}\bullet\,第二个指令 \texttt{ 是合法的,向下移动 20 个单位,到达 (-10,-20) 点;
\hspace{23pt}\bullet\,第三个指令 \texttt{ 是合法的,向上移动 10 个单位,到达 (-10,-10) 点;
\hspace{23pt}\bullet\,第四个指令 \texttt{ 是合法的,向右移动 30 个单位,到达 (20,-10) 点;
\hspace{23pt}\bullet\,第五个指令 \texttt{ 不合法,跳过;
\hspace{23pt}\bullet\,第六个指令 \texttt{ 不合法,跳过;
\hspace{23pt}\bullet\,第七个指令 \texttt{ 不合法,跳过;
\hspace{23pt}\bullet\,第八个指令 \texttt{ 不合法,跳过;
\hspace{23pt}\bullet\,第九个指令 \texttt{ 是合法的,向左移动 10 个单位,到达 (10,-10) 点。
示例2

输入

ABC;AKL;DA1;

输出

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

void movResult(char *pchar, int *pos)
{
    char *ptemp;
    while(*pchar!='\0')
    {
        ptemp=strchr(pchar,';');
        if(ptemp)
        {
            int movlen=0;
            int islegal=1;
            char cmdtype=*pchar;
            *ptemp='\0';
            if(cmdtype=='A'|| cmdtype=='D'|| cmdtype=='W'|| cmdtype=='S')
            {
                while(*(++pchar)!='\0')
                {
                    if(*pchar>='0' && *pchar<='9')
                        movlen = movlen*10+*pchar-'0';
                    else
                    {
                        islegal=0;
                        break;
                    }
                }
                if(islegal && movlen>=0 && movlen<=99)
                {
                    if(cmdtype=='A')
                        pos[0]=pos[0]-movlen;
                    else if(cmdtype=='D')
                        pos[0]=pos[0]+movlen;
                    else if(cmdtype=='W')
                        pos[1]=pos[1]+movlen;
                    else if(cmdtype=='S')
                        pos[1]=pos[1]-movlen;
                }
            }

            pchar=ptemp+1;//指向当前分号后的字符地址;

        }
        else
            break;
    }
}

int main() 
{   
    int pos[2]={0,0};
    char cmd[10001];
    char *pchar;

    scanf("%s",cmd);

    pchar=cmd;

    movResult(pchar,pos);

    printf("%d,%d",pos[0],pos[1]);

    return 0;
}

发表于 2025-02-16 00:47:13 回复(0)
我申请了一个栈的空间来存放用分号间隔的指令,
好处是每个进来的指令都是显式的,便于分析和查看,
坏处就是占用了额外的空间,以及出现的特殊情况的时候,即便收的正确也会处理错误。
#include <stdio.h>
#include <ctype.h>

int ismovesign(char s);

int main()
{
    int x, y;
    char ch;
    char sign[5] = {0};
    int top = -1;
    int warnning;
    int num, digit;

    x = y = 0;
    while ((ch = getchar()) != '\n')
    {
        sign[++top] = ch;
        if (sign[top] == ';')
        {
            num = 0;
            digit = 1;
            sign[top--] = 0; // 把分号扔掉
            while (top > 0)
            {
                if (!isdigit(sign[top])) // 默认栈底是字母,检查是否有未到栈底就出现了非数字的情况
                {
                    while (top != -1)
                    {
                        sign[top--] = 0;
                    }
                    warnning = 1;
                    break;
                }
                else // 从个位开始执行加和
                {
                    num += (sign[top] - '0') * digit;
                    digit *= 10;
                    sign[top--] = 0;
                    warnning = 0;
                }
            }
            if (warnning == 0) // 
            {
                if(num < 100 && num > 0) // num∈[1,99]
                {
                    switch (sign[top]) // 查询栈底的方向
                    {
                        case 'W':y += num; break;
                        case 'A':x -= num; break;
                        case 'S':y -= num; break;
                        case 'D':x += num; break;
                        default: break;
                    }
                }
                sign[top--] = 0;
            }      
        }
    }
    printf("%d,%d", x, y);
    return 0;
}

int ismovesign(char s)
{
    return (s == 'W' || s == 'A' || s == 'S' || s == 'D');
}


发表于 2025-02-06 16:03:57 回复(1)
#include <stdio.h>
#include <string.h>
int main() {
    int a, b, j, i = 0, k, l, m, n, p, q, x = 0, y = 0;
    char c[10000] = {0}, d;
    scanf("%s", c);
    l = sizeof(c);
    while (i < l) {
        if (i == 0) {
            if ((c[i] == 'A' && (c[i + 1] >= 48 && c[i + 1] <= 57) && (c[i + 2] >= 48 &&
                    c[i + 2] <= 57) && c[i + 3] == ';') || (c[i] == 'A' && (c[i + 1] >= 48 &&
                            c[i + 1] <= 57) && c[i + 2] == ';')) {
                if (c[i + 2] == ';') {
                    x = x - (c[i + 1] - '0');
                    i = i + 3;
                    continue;
                } else {
                    x = x - ((c[i + 1] - '0') * 10) - (c[i + 2] - '0');
                    i = i + 4;
                    continue;
                }

            }
            if ((c[i] == 'D' && (c[i + 1] >= 48 && c[i + 1] <= 57) && (c[i + 2] >= 48 &&
                    c[i + 2] <= 57) && c[i + 3] == ';') || (c[i] == 'D' && (c[i + 1] >= 48 &&
                            c[i + 1] <= 57) && c[i + 2] == ';')) {
                if (c[i + 2] == ';') {
                    x = x + (c[i + 1] - '0');
                    i = i + 3;
                    continue;
                } else {
                    x = x + ((c[i + 1] - '0') * 10) + (c[i + 2] - '0');
                    i = i + 4;
                    continue;
                }
            }
            if ((c[i] == 'W' && (c[i + 1] >= 48 && c[i + 1] <= 57) && (c[i + 2] >= 48 &&
                    c[i + 2] <= 57) && c[i + 3] == ';') || (c[i] == 'W' && (c[i + 1] >= 48 &&
                            c[i + 1] <= 57) && c[i + 2] == ';')) {
                if (c[i + 2] == ';') {
                    y = y + (c[i + 1] - '0');
                    i = i + 3;
                    continue;
                } else {
                    y = y + ((c[i + 1] - '0') * 10) + (c[i + 2] - '0');
                    i = i + 4;
                    continue;
                }
            }
            if ((c[i] == 'S' && (c[i + 1] >= 48 && c[i + 1] <= 57) && (c[i + 2] >= 48 &&
                    c[i + 2] <= 57) && c[i + 3] == ';') || (c[i] == 'S' && (c[i + 1] >= 48 &&
                            c[i + 1] <= 57) && c[i + 2] == ';')) {
                if (c[i + 2] == ';') {
                    y = y - (c[i + 1] - '0');
                    i = i + 3;
                    continue;
                } else {
                    y = y - ((c[i + 1] - '0') * 10) - (c[i + 2] - '0');
                    i = i + 4;
                    continue;
                }
            }
            i++;
        } else {
            if (c[i - 1] == ';' && ((c[i] == 'A' && (c[i + 1] >= 48 && c[i + 1] <= 57) &&
                                     (c[i + 2] >= 48 && c[i + 2] <= 57) && c[i + 3] == ';') || (c[i] == 'A' &&
                                             (c[i + 1] >= 48 && c[i + 1] <= 57) && c[i + 2] == ';'))) {
                if (c[i + 2] == ';') {
                    x = x - (c[i + 1] - '0');
                    i = i + 3;
                    continue;
                } else {
                    x = x - ((c[i + 1] - '0') * 10) - (c[i + 2] - '0');
                    i = i + 4;
                    continue;
                }
            }
            if (c[i - 1] == ';' && ((c[i] == 'D' && (c[i + 1] >= 48 && c[i + 1] <= 57) &&
                                     (c[i + 2] >= 48 && c[i + 2] <= 57) && c[i + 3] == ';') || (c[i] == 'D' &&
                                             (c[i + 1] >= 48 && c[i + 1] <= 57) && c[i + 2] == ';'))) {
                if (c[i + 2] == ';') {
                    x = x + (c[i + 1] - '0');
                    i = i + 3;
                    continue;
                } else {
                    x = x + ((c[i + 1] - '0') * 10) + (c[i + 2] - '0');
                    i = i + 4;
                    continue;
                }
            }
            if (c[i - 1] == ';' && ((c[i] == 'W' && (c[i + 1] >= 48 && c[i + 1] <= 57) &&
                                     (c[i + 2] >= 48 && c[i + 2] <= 57) && c[i + 3] == ';') || (c[i] == 'W' &&
                                             (c[i + 1] >= 48 && c[i + 1] <= 57) && c[i + 2] == ';'))) {
                if (c[i + 2] == ';') {
                    y = y + (c[i + 1] - '0');
                    i = i + 3;
                    continue;
                } else {
                    y = y + ((c[i + 1] - '0') * 10) + (c[i + 2] - '0');
                    i = i + 4;
                    continue;
                }
            }
            if (c[i - 1] == ';' && ((c[i] == 'S' && (c[i + 1] >= 48 && c[i + 1] <= 57) &&
                                     (c[i + 2] >= 48 && c[i + 2] <= 57) && c[i + 3] == ';') || (c[i] == 'S' &&
                                             (c[i + 1] >= 48 && c[i + 1] <= 57) && c[i + 2] == ';'))) {
                if (c[i + 2] == ';') {
                    y = y - (c[i + 1] - '0');
                    i = i + 3;
                    continue;
                } else {
                    y = y - ((c[i + 1] - '0') * 10) - (c[i + 2] - '0');
                    i = i + 4;
                    continue;
                }
            }
            i++;
        }

    }

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

    return 0;
}
发表于 2024-11-14 15:27:56 回复(0)
// C语言,获取字符串后,先遍历字符数组,找到分号后,判断经过了几个字符,因为题目说了,最多就是一个字符加俩数字,所以当经过的字符数量超过3,直接进入下一次查找分号。当数量是2时,先判断第二个是否是数字,下标是i - a + 1,再判断第一个是不是ASDW其中一个,如果都是,进行计算,如果都不是,跳出当前循环进入下一次查找分号。当数量是3时同理

#include <stdio.h>

int main() {
    char ch;
    char str[10000];

    int length = 0;
    int a = 0;
    int x = 0, y = 0;
    char next = ';';

    ch = getchar();
    while (ch != '\n') {
        str[length++] = ch;
        ch = getchar();
    }

    for (int i = 0; i < length; i++) {
        if (str[i] != next) {
            a++;
        }
        else {
            {
                if (a == 2) {
                    if(str[i - a + 1] >= '0' && str[i - a + 1] <= '9')
                    {
                        int moveValue = str[i - a + 1] - '0';
                        if(str[i - a] == 'A')
                        {
                            x -= moveValue;
                        }
                        else if (str[i - a] == 'S') {
                            y -= moveValue;
                        }
                        else if (str[i - a] == 'D') {
                            x += moveValue;
                        }
                        else if (str[i - a] == 'W') {
                            y += moveValue;
                        }
                        else {
                            a = 0;
                            continue;
                        }
                    }
                    else
                    {
                        a = 0;
                        continue;
                    }
                }
                else if (a == 3) {
                    if(str[i - a + 1] >= '0' && str[i - a + 1] <= '9' && str[i - a + 2] >= '0' && str[i - a + 2] <= '9')
                    {
                        int moveValue = (str[i - a + 1] - '0') * 10 + str[i - a + 2] - '0';
                        if(str[i - a] == 'A')
                        {
                            x -= moveValue;
                        }
                        else if (str[i - a] == 'S') {
                            y -= moveValue;
                        }
                        else if (str[i - a] == 'D') {
                            x += moveValue;
                        }
                        else if (str[i - a] == 'W') {
                            y += moveValue;
                        }
                        else {
                            a = 0;
                            continue;
                        }
                    }
                    else {
                        a = 0;
                        continue;
                    }
                } else {
                    a = 0;
                    continue;
                }
            }
            a = 0;
        }
    }
    printf("%d,%d", x, y);
    return 0;
}


发表于 2024-07-20 23:02:03 回复(0)
#include <stdio.h>
#include  <string.h>
#include <stdlib.h>
int x = 0, y = 0;
void op(char* op) {
    int flag;
    char num[5];
    char c = op[0];
    if (c != 'A' && c != 'W' && c != 'S' && c != 'D') return ;
    for (int i = 1; i < strlen(op); i++) {
        c = op[i];
        if (!(c >= '0' && c <= '9')) return;
        num[i - 1] = c;
    }
    num[strlen(op)-1]='\0';
    int a = 0;
    for (int i = 0; i < strlen(num) ; i++) {
        a = a * 10 + (num[i] - '0');
    }
    if (op[0] == 'A') x -= a;
    else if (op[0] == 'W') y += a;
    else if (op[0] == 'S') y -= a;
    else if (op[0] == 'D') x += a;
}
int main() {
    char s[10005];
    gets(s);
    char* token = NULL;
    token = strtok(s, ";");
    while (token != NULL) {
        op(token);
        token = strtok(NULL,";"); 
    }
    printf("%d,%d",x,y);
}

发表于 2024-05-15 13:03:58 回复(1)
#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 回复(2)
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)