题目
c语言
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//#include <string.h>
int main()
{
char str[10001] = { 0 };
//strcpy(str, "A10;S20;W10;D30;X;A1A;B10A11;;A10;");
scanf("%s", str);
char *p = str;
int x = 0;
int y = 0;
int step = 0;
char d = 0; //方向
while (*p != '\0')
{
//匹配方向
if (!d)
{
if (*p== 'A' || *p == 'D' || *p == 'W' || *p == 'S')
{
d = *p;
*p++;
continue;
}
else
{
while (*p != '\0' && *p != ';')
{
*p++;
}
}
}
//匹配到方向
if (d)
{
if (*p >= '0' && *p <= '9')
{
step *= 10;
step += (*p - '0');
}
else if (*p == ';')
{
if (d == 'A') x -= step;
else if (d == 'D') x += step;
else if (d == 'W') y += step;
else if (d == 'S') y -= step;
step = 0;
d = 0;
}
else
{
step = 0;
d = 0;
}
}
p++;
}
printf("%d,%d", x, y);
return 0;
}
c++
#include <iostream>
#include <string>
#include <sstream>
#include <regex>
using namespace std;
int main()
{
//"A10;S20;W10;D30;X;A1A;B10A11;;A10;"
string s, c;
//getline(cin, s);
//cout<<s<<endl;
while (getline(cin, s))
{
stringstream ss(s);
pair<int, int> x_y(0, 0);
while (getline(ss, c, ';'))
{
//cout << c << endl;
if (c.empty()) continue;
string _ = c.substr(1);
//cout << _ << endl;
if (regex_match(_, regex("[0-9]*")))
{
switch (c[0])
{
case 'A':x_y.first -= stoi(_); break;
case 'D':x_y.first += stoi(_); break;
case 'W':x_y.second += stoi(_); break;
case 'S':x_y.second -= stoi(_); break;
default:break;
}
}
}
cout << x_y.first << ',' << x_y.second;
}
return 0;
}
python
s=input()
s=s.split(';')
x=y=0
for _ in s:
if not _:
continue
try:
if _[0]=='A':
x-=int(_[1:])
elif _[0]=='D':
x+=int(_[1:])
elif _[0]=='W':
y+=int(_[1:])
elif _[0]=='S':
y-=int(_[1:])
else:
continue
except:
continue
print(f"{x},{y}")
标签:elif,A10,int,每日,一道,算法,continue,str,include
From: https://blog.csdn.net/weixin_65816128/article/details/139699685