笨蛋的難題
阿新 • • 發佈:2019-02-02
笨蛋之所以稱為笨蛋,是因為他有點路痴。他一旦不高興,就必然一個人漫無目的的出去走走。今天下雨了,他又不高興了,怎麼辦?那就出去走唄,這不又丟了,這次幸好記下出來時的方向,並且在一張紙上密密麻麻的記下了他拐的彎(他很聰明吧,拐的彎都是90度的彎),0代表左拐,1代表右拐,那麼多0、1,他實在看不下去了,正好遇見善良加聰明的你,你能告訴他,他現在面向哪嗎?
第一行
輸入:他開始時的面對方向,和他拐彎次數n(0<n<100)。
接著n行數字表示拐的彎。
#include<cstdio>
#include<cstring>
using namespace std;
//East 1 south 2 west 3 north 4
int main(){
int sl,sr,s,s1,i,n,m,flag;
char way[10],way1[]="East",way2[]="South",way3[]="West",way4[]="North";
while(~scanf("%s %d",way,&n)){
sl=sr=0;
flag=0;
if(strcmp(way,way1)==0) s=1;
if(strcmp(way,way2)==0) s=2;
if(strcmp(way,way3)==0) s=3;
if(strcmp(way,way4)==0) s=4;
for(i=0;i<n;i++){
cin>>m;
if(m==0)
sl++;
else
sr++;
}
if(sl>sr){
flag=1;
s1=sl-sr;
}
else{
flag=2;
s1=sr-sl;
}
if(flag==2){
s+=s1;
s=s%4;
if(s==0) s=4;
switch(s){
case 1:cout<<"East"<<endl;break;
case 2:cout<<"South"<<endl;break;
case 3:cout<<"West"<<endl;break;
case 4:cout<<"North"<<endl;break;
}
}
if(flag==1){
s1=s1%4;
s+=(4-s1);
s=s%4;
if(s==0) s=4;
switch(s){
case 1:cout<<"East"<<endl;break;
case 2:cout<<"South"<<endl;break;
case 3:cout<<"West"<<endl;break;
case 4:cout<<"North"<<endl;break;
}
}
}
return 0;
}
輸入
多組測試資料第一行
輸入:他開始時的面對方向,和他拐彎次數n(0<n<100)。
接著n行數字表示拐的彎。
輸出
他現在所面向的方向(West,East,North,South)樣例輸入
East 1 0 North 1 1
樣例輸出
North East#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
//East 1 south 2 west 3 north 4
int main(){
int sl,sr,s,s1,i,n,m,flag;
char way[10],way1[]="East",way2[]="South",way3[]="West",way4[]="North";
while(~scanf("%s %d",way,&n)){
sl=sr=0;
flag=0;
if(strcmp(way,way1)==0) s=1;
if(strcmp(way,way2)==0) s=2;
if(strcmp(way,way3)==0) s=3;
if(strcmp(way,way4)==0) s=4;
for(i=0;i<n;i++){
cin>>m;
if(m==0)
sl++;
else
sr++;
}
if(sl>sr){
flag=1;
s1=sl-sr;
}
else{
flag=2;
s1=sr-sl;
}
if(flag==2){
s+=s1;
s=s%4;
if(s==0) s=4;
switch(s){
case 1:cout<<"East"<<endl;break;
case 2:cout<<"South"<<endl;break;
case 3:cout<<"West"<<endl;break;
case 4:cout<<"North"<<endl;break;
}
}
if(flag==1){
s1=s1%4;
s+=(4-s1);
s=s%4;
if(s==0) s=4;
switch(s){
case 1:cout<<"East"<<endl;break;
case 2:cout<<"South"<<endl;break;
case 3:cout<<"West"<<endl;break;
case 4:cout<<"North"<<endl;break;
}
}
}
return 0;
}