题目: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).
分析:声明一个函数,然后将字符输入,将大写字符减去64或小写减去94就得到相应的数字,再进行运算后得到结果。
#include<iostream>
using namespace std;
int f(char);
int main()
{
char x;
int i,n,k,s;
cin >> s;
for (i = 0; i < s; i++)
{
cin >> x;
cin >> n;
k = f(x);
cout << k + n << endl;
}
}
int f(char x)
{
if(x<='Z'&&x>='A')
return x - 64;
else return -(x - 96);
}