这道题不难,但中间却有个小插曲,困扰了我好久。
已AC的代码如下:
#include<iostream>
using namespace std;
int main(){
int t,y;
char x;
cin>>t;
while(t--){
cin>>x>>y;
if(x<=90&&x>=65)
cout<<y+x-'A'+1<<endl;
else if(x>=97&&x<=122)
cout<<y-x+'a'-1<<endl;
}
return 0;
}
之前我是用 scanf("%c%d",&x,&y); 输入的,可是输入 t,循环总是只有 t/2次。一值找不出什么原因。后来换成 cin 就没异常了。
代码一:
#include<iostream>
using namespace std;
int main(){
int t,y;
char x;
scanf("%d",&t);
while(t--){
scanf("%c%d",&x,&y);
if(x<=90&&x>=65)
printf("%d\n",y+x-'A'+1);
else if(x>=97&&x<=122)
printf("%d\n",y-x+'a'-1);
}
return 0;
}
刚开始这段代码执行时不管我以 “R 1”(中间有空格)这个形式输入,还是以 “R1”(中间没空格)这个形式输入,循环总是只有 t/2次。
改成scanf("%c",&x); scanf("%d",&y); 分两句输入,循环次数还是t/2次。
一直不明白是什么原因,求大神解释。