计蒜客练习题:机器人

 样例:

10

back -9

left 3

left 8

back 15

right 10

right -7

right -3

left 11

right 17

left 3

输出

9 -7

#include <bits/stdc++.h>
using namespace std;
/* forward = 0;
   right = 1;
   back = 2;
   left = 3; 
*/ 
int direction=0,botx=0,boty=0;
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		string s;
		int x;
		cin>>s>>x;
                //1对应90°
		if(s=="forward"){
			direction=direction;
		}
		if(s=="right"){
			direction=(direction+1)%4;
		}
		if(s=="back"){
			direction=(direction+2)%4;
		}
		if(s=="left"){
			direction=(direction-1+4)%4;
		}
        	//方向对应x,y正负半轴 
		if(direction==0){
			botx+=x;
		}
		if(direction==1){
			boty-=x;
		}
		if(direction==2){
			botx-=x;
		}
		if(direction==3){
			boty+=x;
		}
	}
	cout<<botx<<" "<<boty<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42580577/article/details/86603778