Problem Description
we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, … f(Z) = 26, f(z) = -26Give you a letter x and a number y , you
should output the result of y+f(x).
Iput
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
Output for each case, you should the result of y+f(x) on a line.
题解:题目要求输入一个数n表示有n组数据;
每组输入数据为一个字母和一个数;
输出数字和以字母为参数调用函数的返回值之和
观察知26个大写英文字母依次对应数字1到26,其小写字母对应为相反数,利用大写字母ascall码连续的特性写函数,小写字母亦然
AC代码如下
```include<stdio.h>
#include<iostream>
using namespace std;
int f(char a)
{ char ch='A';
if(a>=65&&a<91)
{ for(int i=0;;i++,ch++)
if(a==ch)
return i+1;
}
else if(a>=97&&a<123)
{ for(int i=0;;i++,ch++)
if(a==ch+32)
return -1*(i+1);
}
else return 0;
}
int main()
{ int n,y;
char x;
cin>>n;
for(int i=0;i<n;i++)
{ cin>>x>>y;
cout<<( f(x)+y)<<endl;
}
return 0;
}