hdu2055

这段代码是引用的,对scanf()运用比较熟练才行

#include <stdio.h>

int main()
{
 int n, a;
 char c;

 scanf("%d%*c", &n);
 while (n-- && scanf("%c%d%*c", &c, &a))
  printf("%d\n", a + (c < 97 ? c - 'A' + 1 : 'a' - c - 1));

 return 0;
}

下面的是本人的,有点惭愧了

#include <stdio.h>
int main()
{
    int  nCase;
    int  y;
    char  ch;
    scanf("%d",&nCase);
    getchar();
    while(nCase--)
    {
        scanf("%c %d",&ch,&y);
        getchar();

        if(ch>='A'&&ch<='Z')
            printf("%d\n",y+ch-'A'+1);
        else
            printf("%d\n",y-(ch-'a'+1));
    }
    
    return 0;
}

此处为什么要加一个getchar(),这要好好研究scanf()函数才行


1. scanf()函数接收char时,空格,回车,tab此时均为有意义的字符了,不会被忽略。%d时会视为分隔
一般加上getchar()吸收/n
2. scanf("%s", str);此时不接收str中的空格,到空格就断了
如果想接收空格成一个字符串,则用如下:


scanf("%[^\n]",string); 

这里的意思是直到\n

posted @ 2013-11-17 23:12  海滨银枪小霸王  阅读(122)  评论(0编辑  收藏  举报