49
#include
#include
#include //std::size_t
using namespace std;
int main()
{
string str;
while(cin>>str){
pair point(0,0); //point.first point.second
size_t found = str.find_first_of(';'); //找到第一个';'的位置
int start = 0;
while(found!=string::npos){
string s1 = str.substr(start,found-start);
//cout << s1 << endl;
start = found+1;
found = str.find_first_of(';',found+1);
if(s1.size()>1 && s1.size()<=3){ //合法的字符个数:2或3
char c = s1[0];
int n = 0;
int invalid = 0; //数字为是否非法
for(int i=1; i
if(s1[i]>='0'&&s1[i]<='9')
n = n*10 + (s1[i]-'0');
else{
invalid=1;
break;
}
}
if(invalid==0){
switch(c)
{
case 'A': {point.first-=n;break;}
case 'D': {point.first+=n;break;}
case 'W': {point.second+=n;break;}
case 'S': {point.second-=n;break;}
}
}
}
}
cout << point.first << ',' << point.second <
}
return 0;
}
发表于 2016-04-23 10:31:30
回复(31)
27
#include
#include
#include
using namespace std;
int main()
{
string Input;
while(cin>>Input)
{
int x=0,y=0;
int len = Input.length();
vector vec;
int keep=0;
for(int i=0;i
{
if(Input[i]!=';')
{
keep++;
continue;
}
vec.push_back(Input.substr(i-keep,keep));
keep=0;
}
for(int i=0;i
{
int num=0;
if(vec[i].length()==3&&vec[i][1]>='0'&&vec[i][1]<='9'&&vec[i][2]>='0'&&vec[i][2]<='9')
num=(vec[i][1]-'0')*10+(vec[i][2]-'0');
if(vec[i].length()==2&&vec[i][1]<='9'&&vec[i][1]>='0')
num=(vec[i][1]-'0');
if(vec[i].length()==1)
num=0;
switch(vec[i][0])
{
case 'A': x -= num;break;
case 'D': x += num;break;
case 'S': y -= num;break;
case 'W': y += num;break;
default:break;
}
}
cout<
}
return 0;
}
发表于 2016-09-05 00:37:07
回复(12)
8
#include
int main()
{
char s[10000],temp[100];int i,j,x,y,d1,d2;//坑爹,字符串的居然要设置成10000才不会发生段错误
while(scanf("%s",&s)!=EOF)
{
i=0;x=0;y=0;
while(s[i]!='\0')
{
j=0;
while(s[i]!=';')
{
temp[j]=s[i];
i++;j++;
if(s[i]=='\0')
{
temp[0]='\0';
break;
}
}
if(j==2)
{
d1=temp[1]-'0';
if(d1>=0&&d1<=9)
{
switch(temp[0])
{
case 'A':
x=x-d1;
break;
case 'D':
x=x+d1;
break;
case 'W':
y=y+d1;
break;
case 'S':
y=y-d1;
break;
default:
break;
}
}
}
if(j==3)
{
d1=temp[1]-'0';d2=temp[2]-'0';
if(d1>=0&&d1<=9&&d2>=0&&d2<=9)
{
switch(temp[0])
{
case 'A':
x=x-d1*10-d2;
break;
case 'D':
x=x+d1*10+d2;
break;
case 'W':
y=y+d1*10+d2;
break;
case 'S':
y=y-d1*10-d2;
break;
default:
break;
}
}
}
i++;
}
printf("%d,%d\n",x,y);
}
}
发表于 2016-04-18 22:49:48
回复(4)
25
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String str=sc.nextLine();
String[] A=str.split(";");
int x=0,y=0;
for(String string:A){
if(string.charAt(0)=='D' && string.substring(1).matches("[0-9]+"))
x+=Integer.parseInt(string.substring(1));
if(string.charAt(0)=='W' && string.substring(1).matches("[0-9]+"))
y+=Integer.parseInt(string.substring(1));
if(string.charAt(0)=='S' && string.substring(1).matches("[0-9]+"))
y-=Integer.parseInt(string.substring(1));
if(string.charAt(0)=='A' && string.substring(1).matches("[0-9]+"))
x-=Integer.parseInt(string.substring(1));
}
System.out.println(x+","+y);
}
sc.close();
}
}
发表于 2016-05-04 21:01:03
回复(20)
11
Python3 也不知道是不是简洁明了
while 1:
try:
s_tr = input()
cd = [0,0]
li = s_tr.split(";") # 字符串分割存入列表
for i in li:
if i.startswith("A") and len(i) <= 3 and i[1:].isdigit():
cd[0] += -int(i[1:])
elif i.startswith("D") and len(i) <= 3 and i[1:].isdigit():
cd[0] += int(i[1:])
elif i.startswith("W") and len(i) <= 3 and i[1:].isdigit():
cd[1] += int(i[1:])
elif i.startswith("S") and len(i) <= 3 and i[1:].isdigit():
cd[1] += -int(i[1:])
else:
continue
print("{0},{1}".format(cd[0], cd[1]))
except:
break
发表于 2019-08-22 15:34:22
回复(12)
7
#include
using namespace std;
inline bool isANumber(char ch){
if(ch >= '0' && ch <= '9')
return true;
return false;
}
int main(){
string str;
while(getline(cin ,str)){
int x = 0, y = 0;
string::size_type s = 0 ,e = 0;
while((e=str.find(';', s)) != string::npos){
string sstr = str.substr(s, e-s);
int num = 0;
if(sstr.size() == 2 && isANumber(sstr[1]))
num = sstr[1] - '0';
else if(sstr.size() == 3 && isANumber(sstr[1]) && isANumber(sstr[2]))
num = (sstr[1] - '0')*10 + (sstr[2] - '0');
if(num != 0){
if(sstr[0] == 'A') x -= num;
if(sstr[0] == 'D') x += num;
if(sstr[0] == 'W') y += num;
if(sstr[0] == 'S') y -= num;
}
s = e + 1;
}
cout<
}
return 0;
}
发表于 2017-06-16 20:13:06
回复(2)
10
OJ有问题吧,,,第一次提交答案错误,我把代码和数据复制到本地运行是对的(我是直接在网页上敲的代码),然后把原来错误的代码复制粘贴交一次就通过了。如果牛客的人看到这条消息,建议找我的提交记录看看。
PS: 评论中大多数人可能没理解我的意思,我的情况是:完全一样的代码提交了两次,一次错误,一次通过,可能是在两次提交之间牛客的人在后台修改了数据或修复了什么Bug,但这样不是应该rejudge一下已有的提交吗 import sys
dx = [-1, 0, 0, 1]
dy = [0, -1, 1, 0]
for line in sys.stdin:
x, y = 0, 0
for cmd in line.split(';'):
cmd = cmd.strip()
if cmd and cmd[0] in 'ASWD':
try:
n = int(cmd[1:])
x += n * dx['ASWD'.find(cmd[0])]
y += n * dy['ASWD'.find(cmd[0])]
#print cmd, n, x, y
except:
pass
print "%d,%d"% (x, y)
编辑于 2017-07-30 10:41:48
回复(24)
6
面向对象的思想 package com.special.spet;
import java.util.Scanner;
/**
*
* @author special
* @date 2017年9月6日 下午5:35:09
*/
class Point{
private int x;
private int y;
private static final char[] INSTRUCT_LETTER = {'A', 'S', 'W', 'D'};
public int getX(){
return x;
}
public int getY(){
return y;
}
private static boolean contain(char ch){
for(char item : INSTRUCT_LETTER)
if(item == ch)
return true;
return false;
}
private static boolean isNum(char ch){
if(ch >= '0' && ch <= '9') return true;
else return false;
}
public static boolean isStandard(String instruct){
if(instruct.length() > 3 || !contain(instruct.charAt(0))) { return false; }
for(int i = 1; i < instruct.length(); i++)
if(!isNum(instruct.charAt(i)))
return false;
return true;
}
/**
* 处理每一个指令,包括判断是否合格
* [@param instruct
* @return](/profile/547241) 一个待移动量的Point对象
*/
public static Point prestationOfInstruct(String instruct){
Point target = new Point();
if(!isStandard(instruct)) {return target;}
int steps = Integer.parseInt(instruct.substring(1));
switch(instruct.charAt(0)){
case 'A': target.x = -steps; break;
case 'S': target.y = -steps; break;
case 'W': target.y = steps; break;
case 'D': target.x = steps; break;
}
return target;
}
/**
* 根据参数的移动量的target来进行相加(减法其实也是一种加法)
* @param target
*/
public void move(Point target){
this.x += target.x;
this.y += target.y;
} [@Override](/profile/992988) public String toString(){
return x + "," + y;
}
}
public class Pro16 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
while(input.hasNext()){
String str = input.nextLine();
String[] instructs = str.split(";");
Point origin = new Point();
for(String instruct : instructs)
origin.move(Point.prestationOfInstruct(instruct));
System.out.println(origin);
}
}
}
进一步优化: package com.special.spet;
import java.util.Scanner;
/**
*
* @author special
* @date 2017年9月6日 下午10:05:48
*/
class Point{
private int x;
private int y;
public int getX(){
return x;
}
public int getY(){
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString(){
return x + "," + y;
}
}
public class Pro16Improve {
/**
* 利用空间存储指令的方向移量
*/
private static final String INSTRUCT_LETTER = "ASWD";
private static final int[] distenceX = {-1, 0, 0, 1};
private static final int[] distenceY = {0, -1, 1, 0};
private static int find(char ch){
for(int i = 0; i < INSTRUCT_LETTER.length(); i++){
if(ch == INSTRUCT_LETTER.charAt(i))
return i;
}
return -1;
}
private static boolean isNum(char ch){
if(ch >= '0' && ch <= '9') return true;
else return false;
}
public static void move(Point point, String instruct){
if(instruct.equals("") || instruct.length() > 3 || find(instruct.charAt(0)) == -1) { return ;}
int x = 0;
int y = 0;
for(int i = 1; i < instruct.length(); i++){
if(isNum(instruct.charAt(i))){
// x,y上的移量为倍数乘以单位移量
x = x * 10 + (instruct.charAt(i) - '0') * distenceX[find(instruct.charAt(0))];
y = y * 10 + (instruct.charAt(i) - '0') * distenceY[find(instruct.charAt(0))];
}
else return;
}
point.setX(point.getX() + x);
point.setY(point.getY() + y);
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
while(input.hasNext()){
String str = input.nextLine();
String[] instructs = str.split(";");
Point origin = new Point();
for(String instruct : instructs)
move(origin, instruct);
System.out.println(origin);
}
}
}
编辑于 2017-09-07 08:28:25
回复(0)
7
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String s = in.nextLine();
String[] str = s.split(";");
int row = 0;
int column = 0;
for (int i = 0; i < str.length; i++) {
if (str[i].length() <= 3) {
if (str[i].charAt(0) == 'A' && str[i].substring(1).matches("[0-9]+")) {
row -= Integer.parseInt(str[i].substring(1));
}
if (str[i].charAt(0) == 'D' && str[i].substring(1).matches("[0-9]+")) {
row += Integer.parseInt(str[i].substring(1));
}
if (str[i].charAt(0) == 'W' && str[i].substring(1).matches("[0-9]+")) {
column += Integer.parseInt(str[i].substring(1));
}
if (str[i].charAt(0) == 'S' && str[i].substring(1).matches("[0-9]+")) {
column -= Integer.parseInt(str[i].substring(1));
}
}
}
System.out.println(row + "," + column);
}
}
}
发表于 2016-08-22 16:37:45
回复(9)
4
while True:
try:
s = raw_input()
res = s.split(';')
x, y = 0, 0
for i in res:
if i.isalnum() and 'A' in i and i.replace('A','',1).isdigit():
x = x - int(i.replace('A',''))
if i.isalnum() and 'S' in i and i.replace('S','',1).isdigit():
y = y - int(i.replace('S',''))
if i.isalnum() and 'W' in i and i.replace('W','',1).isdigit():
y = y + int(i.replace('W',''))
if i.isalnum() and 'D' in i and i.replace('D','',1).isdigit():
x = x + int(i.replace('D',''))
out = str(x) + ',' + str(y)
print out
except:
break
没雕琢,大概就这个意思~
发表于 2016-09-02 23:00:19
回复(3)
6
为什么自己在电脑上测试没问题,在牛客网上测试没有输出,测试用例:
W49;W92;A84;W33;A32;A52;D97;S14;S33;D79;A14;W25;D97;D21;D31;A66;S67;S4;A59;S62;W40;S26;S65;A58;S17;A67;D48;W23;D68;S99;S97;W63;W27;D12;D83;W8;S3;W6;A88;D23;W30;S91;D58;W74;D45;W3;D19;S72;D8;S79;S76;S49;W16;A29;W93;W99;W92;D82;A10;A4;D25;A90;D83;W45;W20;S68;D59;S48;A18;S0;W24;S48;W75;A39;W29;S28;W76;W78;D94;A57;A5;D51;S61;A39;W77;S70;A2;D8;S58;D51;S86;W30;A27;S62;D56;A51;D0;S58;W34;S39;A64;A68;A96;D37;S91;S16;A17;D35;A85;W88;S57;S61;A28;D12;A87;S39;A85;W22;D65;D72;A5;A78;A59;D75;D57;S66;A55;D84;D72;W87;S46;W64;D49;S46;W34;D60;S39;A30;W86;D20;W93;D25;W44;W86;A16;D4;A86;S86;S27;W7;W89;W93;S17;S39;W66;W72;D81;W93;A88;D46;S57;W45;S84;S57;D27;A11;D54;S8;W15;A50;A69;A4;D19;D69;A3;A28;D47;W18;A39;D47;W14;D77;W59;S84;A32;D56;S16;D99;A33;A51;A24;D65;W37;D98;A13;W6;D94;D28;A12;S18;W40;S23;W76;D6;S40;D26;W97;W7;W90;W75;S12;A89;S46;S36;D96;A49;A73;S53;D84;A87;D75;D48;W84;S14;W65;W79;W51;S9;S77;D51;S76;W16;W77;A90;S96;D78;S7;W71;D17;W10;W4;D96;S58;A88;S89;D41;W47;W13;S75;S85;W63;W87;S19;S80;W92;W95;W73;D43;D35;W60;S31;D72;A73;W90;S51;A55;S82;W81;S56;W83;W41;A77;S37;D63;A72;D44;W81;S26;S53;W32;A24;S90;S83;W34;D10;S53;D69;A87;W84;S32;D72;S63;D97;W27;D23;D25;D18;W86;D74;D42;A31;S1;D98;D76;S46;D67;W94;S6;S97;S40;W71;W12;D62;W57;A92;W59;W59;D15;A16;D21;S33;S45;S83;D89;W77;A93;S60;S6;W24;S93;D69;D79;S13;S8;W63;D99;S36;D35;W53;S5;S44;S19;D76;S14;A86;A86;W43;W65;D20;S19;W66;A54;A98;A17;D2;W98;W2;D77;A90;S8;S55;S67;W88;W19;S59;D9;S75;S56;W75;W54;A61;W47;S19;D67;S39;D55;S6;A89;A5;W58;W7;W88;W92;D85;S32;S32;A27;S42;D52;A55;S26;D27;W40;D76;A55;D38;W13;A71;D79;W59;A76;A33;A12;D1;D63;W63;W62;D7;W48;A84;D27;A80;D42;D27;D49;D4;D71;W90;W39;D24;W71;D16;S1;W88;W88;D25;D66;S39;S43;A99;A92;W19;W20;A90;A31;A6;A79;D4;D80;A77;D71;D36;S64;
对应输出应该为:
1207,1244
你的输出为:
而在自己电脑测试是正确的,求大神解答
发表于 2017-08-11 11:12:32
回复(21)
7
#include
#include
using namespace std;
typedef struct Point
{
int x=0;
int y=0;
};
string d("ADSW");
void move(string str, Point &p)
{
if ((str.size() <= 3) && (str.size() >= 2)){
string dir = str.substr(0, 1);
string num = str.substr(1, str.size() - 1);
int n = atoi(num.c_str());
if ((dir.find_first_of(d) != string::npos) && isdigit(num[0]) && isdigit(num[num.size() - 1]))
{
char c = dir[0];
switch (c)
{
case 'S':p.y -= n; break;
case 'W':p.y += n; break;
case 'A':p.x -= n; break;
case 'D':p.x += n; break;
}
/* if (c == 'A')
p.x -= n;
int a = 0;*/
}
}
}
int main()
{
string str;
while (getline(cin,str))
{
Point p;
int pStart = 0;
int pos = str.find_first_of(";", pStart);
if (pos == pos != string::npos)
move(str, p);
while (pos != string::npos)
{
string sub = str.substr(pStart, pos - pStart);
move(sub,p);
pStart = pos + 1;
pos = str.find_first_of(";", pStart);
}
cout << p.x << "," << p.y << endl;
}
return 0;
}
发表于 2016-03-13 17:28:43
回复(0)
3
//思路:
//1 分割字符串
//2 判断分割后的字符串坐标是否合法
//3 对合法的字符串坐标判断方向,并将字符串中的数字提取出来,在前面的坐标基础上进行运算
//4 确定运算规则:初始坐标(x,y)===>
//字符串为Anum===》x-=num
//字符串为Snum===》y-=num
//字符串为Wnum===》y+=num
//字符串为Dnum===》x+=num
#include
#include
#include
using namespace std;
vector split(string str, const char c){//字符串分割函数
vector sstr;
int pos = 0;
while (pos < str.length()){
int end;
for (end = pos; str[end] != c && end < str.length(); end++);
sstr.push_back(str.substr(pos, end - pos));
pos = end + 1;
}
return sstr;
}
bool judge(string str){//判断坐标的合法性,合法返回true,否则返回false
if (str[0] == 'A' || str[0] == 'S' || str[0] == 'D' || str[0] == 'W'){
if (str.length() >= 2 && str.length() <= 3){
if (str.length() == 2){
if (str[1] >= '0' && str[1] <= '9') return true;
}
else{
if ((str[1] >= '0' && str[1] <= '9') && (str[2] >= '0' && str[2] <= '9')) return true;
}
}
}
return false;
}
int main(){
string inputs;
while (cin >> inputs){
int x = 0;
int y = 0;
vector allStr = split(inputs, ';');
for (vector::iterator it = allStr.begin(); it != allStr.end(); it++){
if (judge(*it)){
//截取数字,并将string转换成c字符串,然后将c字符串用atoi转换成数字
int num = atoi(((*it).substr(1)).c_str());
switch ((*it)[0])
{
case 'A':
x -= num; break;
case 'S':
y -= num; break;
case 'W':
y += num; break;
case 'D':
x += num; break;
}
}
}
cout << x << "," << y << endl;
}
system("pause");
return 0;
}
编辑于 2019-07-08 18:13:16
回复(0)
2
#include
#include
#include
#include
using namespace std;
bool Is_Char_Valid(char ch)
{
if (ch != 'W' && ch != 'A' && ch != 'S'
&& ch != 'D')
{
return false;
}
return true;
}
bool Is_All_Valid(string str)
{
if (str.length() == 0 || str.length() > 4)
{
return false;
}
int str_len =str.length();
if(!Is_Char_Valid(str[0]))
{
return false;
}else{
if ((str_len >= 2 && (str[1] < '0' ||
str[1] > '9'))||( str_len == 3 && (str[2] < '0' ||
str[2] > '9')))
{
return false;
}else{
return true;
}
}
}
void Process_function(int &x, int &y, string str)
{
int len = str.length();
int gewei = (len == 3) ? (str[2] - '0') : (str[1] - '0');
int shiwei = (len == 3) ? (str[1] - '0') : 0;
int move = 10 * shiwei + gewei;
switch(str[0])
{
case 'W':
y += move;
break;
case 'A':
x -= move;
break;
case 'S':
y -= move;
break;
case 'D':
x += move;
break;
}
}
int main()
{
char input_data[10000];
const char *p = ";";
while(cin.getline(input_data,10000))
{
vector input_data_vector;
char *data =NULL;
int x = 0;
int y = 0;
input_data_vector.push_back(strtok(input_data,p));
while(data=strtok(NULL,p))
{
input_data_vector.push_back(data);
}
vector :: iterator it;
for (it = input_data_vector.begin(); it !=
input_data_vector.end() ; it++)
{
if(Is_All_Valid(*it))
{
Process_function(x,y,*it);
}
}
cout<
}
return 0;
}
发表于 2016-01-09 16:54:00
回复(0)
3
#include
#include
using namespace std;
int main()
{
string s;
while (getline(cin, s))
{
int x = 0, y = 0;
while (s.find(";") != string::npos)
{
string tmp = s.substr(0, s.find(";"));
s = s.substr(s.find(";") + 1);
int len = tmp.size();
if (len > 1 && len <= 3)
{
char c;
int num = 0;
if (tmp[0] == 'A' || tmp[0] == 'S' || tmp[0] == 'D' || tmp[0] == 'W')
c = tmp[0];
else
continue;
if ((len == 2 && !isdigit(tmp[1])) || (len == 3 && (!isdigit(tmp[1]) || !isdigit(tmp[2]))))
continue;
num = (len == 2) ? (tmp[1] - '0') : num = 10 * (tmp[1] - '0') + (tmp[2] - '0');
switch (c)
{
case 'A':
x -= num;
break;
case 'S':
y -= num;
break;
case 'W':
y += num;
break;
case 'D':
x += num;
break;
}
}
}
cout << x << "," << y << endl;
}
return 0;
}
编辑于 2019-07-30 16:16:13
回复(0)
3
#include #include
#include
using namespace std;
int main() {
string str;
while (getline(cin, str)) {
int x = 0, y = 0;
vector vec;
int f = 0;
int l = 0;
while (l = str.find(';',f)) {
if (l<0) break;
string s = str.substr(f, l - f);
vec.push_back(s);
f = l + 1;
l = 0;
}
for (int i = 0; i
int len = vec[i].size();
if (len <= 1) continue;
int num = 0;
int j = 1;
for (; j= '0'; ++j) {
num = num * 10 + (vec[i][j] - '0');
}
if (j ==len) {
switch (vec[i][0]) {
case'A':x -= num; break;
case'S':y -= num; break;
case'W':y += num; break;
case'D':x += num; break;
}
}
}
cout << x << "," << y << endl;
}
return 0;
}
弄不明白哪里错了:
A37;S1;S72;S41;W21;W32;A45;A98;D97;D69;W57;W11;S41;S0;S24;S83;A75;W77;S32;D18;A24;D20;A65;D95;S18;W56;A84;W30;S3;S50;D64;S84;D82;A86;A36;S85;S94;W64;W62;A12;W12;S84;W29;A52;A73;W55;W71;D43;D76;D49;W19;S56;W63;W56;A68;D68;D40;A48;W5;A12;S84;A2;S44;A93;D51;D64;S4;W32;W27;W15;W70;A47;S77;S88;A25;D51;D8;A34;A71;A5;D83;S36;S72;A34;D46;S29;S5;W57;W16;S42;A23;A30;D43;S0;W62;A34;D60;D31;W89;W91;S87;A15;S15;S18;W83;S82;W87;W73;D42;A92;D48;A65;D36;A11;W50;W38;W2;A0;D65;W29;D56;S64;D31;W8;A56;A45;A56;W54;A97;D97;A90;S72;A95;A89;S78;A35;A31;W68;W42;A73;S73;A24;S28;D69;W53;S54;D80;D27;W24;S86;A17;A36;A41;A1;D19;S53;S96;A31;A52;A63;A18;S54;A35;A82;W95;D8;W48;S75;W11;S9;W60;D68;A92;A96;W32;S30;D26;W61;S0;D10;S89;W31;D4;W37;S49;D79;S56;A87;S61;A61;D96;W86;S81;D50;S91;A68;A82;A36;A16;S6;W25;D76;D94;A20;A37;D91;S58;A54;S77;S27;A35;S6;A88;A14;S72;D12;A95;W93;W1;A73;A55;A13;S55;W43;W6;D37;W19;W79;W56;S26;A36;W85;A6;W94;A54;A12;S0;A13;D18;W14;A52;W44;D83;W17;W73;A72;D56;A63;S14;A64;A84;S54;D67;A92;D46;A51;D99;W42;W2;D22;S14;D96;A79;A41;S65;D8;S41;D30;S61;D77;A52;W2;S11;W26;D47;A65;S11;D2;W98;A30;D36;W26;S41;S71;D65;D76;D28;D25;S24;S24;S58;W13;D80;A58;S64;S52;W54;S51;S40;W20;D54;W13;A7;D37;D93;A74;D24;S0;A5;W52;D86;D22;W22;S13;D56;S99;D67;S34;S5;D47;D87;D41;S15;S66;W10;D55;A63;S57;W12;S96;W28;W96;S33;D70;D75;S20;W76;D83;A34;W29;S24;A7;W11;W81;S85;W1;W71;W73;D20;A62;S2;W43;S22;D18;W35;A15;S72;W12;S12;D93;S37;S55;D33;D52;S85;W97;A88;W47;A17;A50;A40;D9;A0;D13;A16;S87;S61;D12;D8;D69;S59;S71;A7;W99;D81;W6;D48;A92;D23;D47;W37;W30;A5;S96;D28;A13;D53;D17;D9;D58;S78;S26;W17;D88;A11;W87;W94;D45;D60;W52;S71;S71;D35;D60;W60;D73;A42;D57;A25;W87;A4;W74;W70;A50;S22;S63;W15;A62;S70;A15;W58;D19;D56;A80;S95;S57;D75;A16;S8;D88;S33;S94;A43;D97;S11;D53;W55;S52;W59;S92;D39;D40;W34;W33;D52;D19;S87;A21;D94;W5;W42;A50;D27;A29;D47;W12;D5;S70;D47;S16;W52;W30;D2;A48;S1;D68;W82;A47;S84;S56;S60;S14;W64;W8;A41;W8;S4;D80;A49;D72;
oj上说测试用例错误,为什么又无输出呢?明明有cout啊。。我在本地用这个测试用例都是正常的。
发表于 2016-03-24 22:01:58
回复(13)
1
感觉题目没说清楚,就是那个空格出现的位置,好多玩家似乎都没考虑,比如 A 20; 这种,又或者是 A2 0 ; 空格可能出现的位置题目并没有明确说明,只是在例子中提到,但是好多玩家的解法里似乎都没考虑各种花式的空格插入的可能性,但似乎都过了(是我漏掉什么了吗)。
然后是非法字符,我最开始没看到数字长度最多为2,我以为是任意的整数,导致我的做法考虑得稍微多了点。
这个题 O(N) 的时间足矣,我的做法便是使用字符自动机从左开头扫描到结尾即可完成对这个字符串的分析,便类似一个在线算法,不用先分割,也不需要额外的数组去保存某个子串,向前指针走到到哪便分析到哪。
直接上C语言(含注释): #include
#include
#include // 在C中使用bool类型
char buffer[2000]; // 全局缓冲区
int sign; // 决定方向的正负,取+1/-1
int* pxy; // 指向要修改的 x 或者 y (根据按键的方向修改)
char* start; // 开始指针,指向单词的开头
char* forward; // 向前指针,指向单词的结尾
char* cur; // 当前游标位置
bool terminate; // 自动机是否终止
// 自动机启动
void startup()
{
// 指向 buffer 开头的前一个位置
start = forward = cur = buffer - 1;
pxy = NULL;
terminate = false;
}
// 负责获取下一个单词,以及决定自动机是否该停止
void next_word()
{
++forward;
while (*forward == ' ') ++forward; // 跳过下个单词开头的空格
start = forward;
while (*forward != ';') {
if (*forward == '\0') {
terminate = true;
return;
}
++forward;
}
}
// 字符自动机
void automaton(int* x, int* y)
{
startup();
// 每次循环处理一个单词
next_word();
while (!terminate) {
// 如果开头字符不是方向,说明有误,故跳过这个单词
switch (*start) {
case 'A':
sign = -1;
pxy = x;
break;
case 'D':
sign = 1;
pxy = x;
break;
case 'W':
sign = 1;
pxy = y;
break;
case 'S':
sign = -1;
pxy = y;
break;
default:
next_word();
continue;
}
cur = start + 1;
while (*cur == ' ') ++cur; // 跳过空格
if (!isdigit(*cur)) { // 如果方向后跟的不是数字则有误
next_word();
continue;
}
int number = *cur - '0';
while (isdigit(*++cur))
number = number * 10 + (*cur - '0');
while (*cur == ' ') ++cur; // 跳过空格
if (*cur != ';') { // 如果数字后跟的不是分号则有误
next_word();
continue;
}
*pxy += sign * number;
next_word();
}
}
int main()
{
int x, y;
while (scanf("%s", buffer) == 1) {
x = y = 0;
automaton(&x, &y);
printf("%d,%d\n", x, y);
}
return 0;
}
发表于 2021-02-25 21:59:12
回复(0)
1
#include
#include
#include
using namespace std;
int main(){
string s;
while(cin>>s){
stringstream iss(s);
string subs;
vector vec;
int x=0,y=0;
while(getline(iss, subs, ';'))
vec.push_back(subs);
for(auto it=vec.begin();it!=vec.end();it++){
switch((*it)[0]){
case 'A':x-=stoi((*it).substr(1,(*it).size()-1));break;
case 'D':x+=stoi((*it).substr(1,(*it).size()-1));break;
case 'S':y-=stoi((*it).substr(1,(*it).size()-1));break;
case 'W':y+=stoi((*it).substr(1,(*it).size()-1));break;
}
}
cout<
}
return 0;
}//我的是最短的吗?
发表于 2021-02-25 16:35:23
回复(0)
1
a = input()
a = a.split(';')
b = [0, 0]
for item in a:
if 2 <=len(item) <= 3:
if item[0] in ['A', 'S', 'W', 'D'] and item[1:].isdigit():
if item[0] == 'A':
b[0] -= int(item[1:])
elif item[0] == 'W':
b[-1] += int(item[1:])
elif item[0] == 'S':
b[-1] -= int(item[1:])
elif item[0] == 'D':
b[0] += int(item[1:])
print('{},{}'.format(b[0],b[-1]))
发表于 2021-02-22 16:46:22
回复(0)
1
#include
using namespace std;
#define LINE_IS_END 0
#define CMD_UNVALID -1
#define CMD_VALID 1
/* Read a cmd in one line from cin
@command: store the cmd string
@return: status->{LINE_IS_END, CMD_UNVALID, CMD_VALID}
*/
int readCmd(string& command)
{
char tmp;
int cmd_size;
char direction;
command.clear();
while (cin.get(tmp))
{
switch (tmp) {
case '\n':
return LINE_IS_END;
case ';':
goto check;
default:
command.push_back(tmp);
//ignore error input which is too long due to the miss of semicolon
}
}
check:
cmd_size = command.size();
if (cmd_size 3) {
return CMD_UNVALID;
}
direction = command.front();
if (direction != 'A' && direction != 'D' &&
direction != 'W' && direction != 'S') {
return CMD_UNVALID;
}
tmp = command.at(1);
if (tmp '9') {
return CMD_UNVALID;
}
if (cmd_size == 3) {
tmp = command.at(2);
if (tmp '9') {
return CMD_UNVALID;
}
}
return CMD_VALID;
}
typedef struct position {
int x;
int y;
}pos_t;
//Assume the command is valid, if not so, no operation is done.
void move_pos(pos_t& pos, string& command)
{
char direction = command.front();
int dis = 0;
int cmd_size = command.size();
if (cmd_size 3) {
return;
}
dis = command.at(1) - '0';
if (cmd_size == 3){
dis = dis * 10 + command.at(2) - '0';
}
switch (direction) {
case 'A':
pos.x = pos.x - dis;
break;
case 'D':
pos.x = pos.x + dis;
break;
case 'W':
pos.y = pos.y + dis;
break;
case 'S':
pos.y = pos.y - dis;
break;
}
}
int main(void)
{
string cmd;
int ret;
pos_t pos = {0, 0};
while (true) {
ret = readCmd(cmd);
if (ret == CMD_VALID) {
move_pos(pos, cmd);
}
if (ret == LINE_IS_END) {
cout<
break;
}
}
return 0;
}
发表于 2020-12-19 21:23:33
回复(0)