第二周HDU-2055题解

问题链接:HDU-2055

问题简述

定义 f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, … f(Z) = 26, f(z) = -26,在第一行输入n,下面n行,每行一组数据,每组数据包含一个字母,一个整数,每组数据输出y+f(x)。

思路

输入n,用for循环循环n次,定义一个函数计算函数f(x),大学字母时,f(x)=x-64,小写字母时等于-f(x-32),然后输出就完事了。

AC通过的C++语言程序如下:

#include<iostream>
int f(int);
using namespace std;
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		char x; int y;
		cin >> x >> y;
		cout << f(x) + y << endl;
	}
	return 0;
}
int f(int a)
{
	if (a <= 90) return a - 64;
	else return -f(a - 32);
}

猜你喜欢

转载自blog.csdn.net/weixin_43970556/article/details/85008588