杭电2055AneasyproblemProblemDesc-查字典问答网
分类选择

来自樊其瑾的问题

  杭电2055AneasyproblemProblemDescriptionwedefinef(A)=1,f(a)=-1,f(B)=2,f(b)=-2,...f(Z)=26,f(z)=-26;Giveyoualetterxandanumbery,youshouldoutputtheresultofy+f(x).InputOnthefirstline,containsanumberT.thenTlinesf

  杭电2055Aneasyproblem

  ProblemDescription

  wedefinef(A)=1,f(a)=-1,f(B)=2,f(b)=-2,...f(Z)=26,f(z)=-26;

  Giveyoualetterxandanumbery,youshouldoutputtheresultofy+f(x).

  Input

  Onthefirstline,containsanumberT.thenTlinesfollow,eachlineisacase.eachcasecontainsaletterandanumber.

  Output

  foreachcase,youshouldtheresultofy+f(x)onaline.

  SampleInput

  6

  R1

  P2

  G3

  r1

  p2

  g3

  SampleOutput

  19

  18

  10

  -17

  -14

  -4

  我的代码:这段一直过不了,检查不出错,无奈之下switch了一大串,终于通过了,不过还想求教这段到底哪里有问题,

  #include

  intmain()

  {

  x05inta,s,i;

  x05charz;

  while(scanf("%d",&a)!=EOF)

  {

  x05for(i=0;i='a'&&z='A'&&z

3回答
2020-11-17 22:31
我要回答
请先登录
贺桂芳

  这是我的ac码

  我觉得可能是你的输入控制有问题吧,没有将换行符去掉

  #include

  intmain()

  {

  inta,c;

  charb;

  scanf("%d",&a);

  while(a--)

  {

  getchar();*********去掉换行符,有整数有有字符的时候要注意的.

  b=getchar();

  scanf("%d",&c);

  if(b>='a')printf("%dn",'a'-1-b+c);

  elseprintf("%dn",b-'A'+1+c);

  }return0;

  }

2020-11-17 22:34:36
樊其瑾

  你的代码倒是很神奇地过了,不过为什么我没考虑getchar();在电脑上编译运行后也能得出正确结果?

2020-11-17 22:37:16
贺桂芳

  我帮你分析一下你的代码为什么能得出正确结果:SampleInput还是采用原来的6R1P2G3r1p2g3第1步:把6赋给a,回车还留在缓冲区中第2步:把回车赋给z第3步:把字母'R'赋给整数s,但是字母'R'不是一个整数,赋不成功,此scanf语句结束。此时的s的值是内存中初始的值,是一个不确定数。字母'R'还留在缓冲区中。第4步:判断if(z>='a'&&z='A'&&z='a'&&z='A'&&z

2020-11-17 22:38:26

最新问答

推荐文章

猜你喜欢

附近的人在看

推荐阅读

拓展阅读

  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  •