從零起步看算法(第十天 4.15)
看點評,總結模擬這一套路的使用
進制轉換,迴文數,機器人的座標爲模擬情況的套路
//q11 機器人座標
一開始沒有讀清楚題意,把問題想簡單了。
1.本題重點:座標問題-----順時針的餘數問題
https://blog.csdn.net/vener_/article/details/79346332
2.cin>>x;while(x--){};形式可以瞭解
#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
int main(){
int n;
int x;
int d[4]={0};
int k=0;//順時針數組
string str1;
while(cin>>n){
int countx=0;
int county=0;
assert(n>=1&&n<=100);
for(int j=0;j<n;j++){
cin>>str1;
cin>>x;
assert(n>=-1000&&n<=1000);
if(str1=="forward"){
d[k]+=x;
}
if(str1=="back"){
k=(k+2)%4;
d[k]+=x;
}
if(str1=="left"){
k=(k+1)%4;
d[k]+=x;
}
if(str1=="right"){
k=(k+3)%4;
d[k]+=x;
}
//else return 0;
}
countx=d[0]-d[2];
county=d[1]-d[3];
cout<<countx<<" "<<county<<endl;
}
return 0;
}