大致题意:
- 按照给定的指令移动,输出最后到达的点。
- 若没有走动过则输出
(0,0)
。
基本思路
- 这题就是模拟,主要是判断指令的时候不太好判断,
- 先用字符串把指令读取进来,看看第一位是否是数字(如果是数字那么就证明整个指令都是数字),
- 如果不是数字那么这个指令就是"left"或"right"了,
- 然后按照指令去走就可以咯。
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
using namespace std;
#define R read()
#define GC getchar()
#define ll long long
#define ull unsigned long long
#define INF 0x7fffffff
#define LLINF 0x7fffffffffffffff
ll read(){
ll s=0,f=1;
char c=GC;
while(c<'0'||c>'9'){if(c=='-')f=-f;c=GC;}
while(c>='0'&&c<='9'){s=s*10+c-'0';c=GC;}
return s*f;
}
void fre(){
freopen("way.in","r",stdin);
freopen("way.out","w",stdout);
}
int n;
string s;
int p=1,c,x,y;//p是方向,c是记录指令数字的,x和y是表示坐标的
int fl=1;//标记变量,1表示没有走动过,0表示走动过
int main(){
fre();
cin>>n;
for(int i=1;i<=n;++i){
cin>>s;
if(s[0]>='0'&&s[0]<='9'){//判断是否是数字
c=0;
for(int j=0;j<s.length();++j){
c=c*10+(s[j]-'0');
}//把字符串变成int型的
if(p==1){//看方向更改坐标
y+=c;
}else if(p==2){
y-=c;
}else if(p==3){
x-=c;
}else if(p==4){
x+=c;
}
printf("(%d,%d)
",x,y);//输出
fl=0;//标记为走动过
}else{
if(s=="left"){//判断指令的方向
if(p==1){//根据现在的方向改动方向
p=3;
}else if(p==2){
p=4;
}else if(p==3){
p=2;
}else if(p==4){
p=1;
}
}else if(s=="right"){//同上
if(p==1){
p=4;
}else if(p==2){
p=3;
}else if(p==3){
p=1;
}else if(p==4){
p=2;
}
}
}
}
if(fl){//没有动过就输出"(0,0)"
printf("(0,0)");
}
return 0;
}