HDU - 2055——An easy problem
An easy problem
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 33073 Accepted Submission(s): 21354
Problem Description
we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, … f(Z) = 26, f(z) = -26;
Give you a letter x and a number y , you should output the result of y+f(x).
Input
On the first line, contains a number T.then T lines follow, each line is a case.each case contains a letter and a number.
Output
for each case, you should the result of y+f(x) on a line.
Sample Input
6
R 1
P 2
G 3
r 1
p 2
g 3
Sample Output
19
18
10
-17
-14
-4
問題連結:HDU - 2055
問題簡述:把A~ Z當做1~26,把a ~當做-1 ~-26,再加上一個數y,輸出結果,多組輸入輸出。
問題分析:‘A’的ASCII碼為65,將其減去64便可用‘A’代表1,同理,‘a’的ASCII碼為97,將其減 去96並取負數便可用‘a’表示-1,其他的‘A’~‘Z’ ‘a’ ~‘z’也同理。將其值加上y值並輸出即為所求值。
程式說明:i和n用於接下來輸入輸出的組數。ch代表字母,y代表要加的值。getchar()用於接收每次scanf後多餘的字元‘\n’。
AC通過程式:
#include<stdio.h> int main() { int i; int n,y; char c; scanf("%d",&n); getchar(); for(i=0;i<n;i++) { scanf("%c%d",&c,&y); getchar(); if(c>='A'&&c<='Z') { printf("%d\n",y+c-64); }else printf("%d\n",y-c+96); } return 0; }