题目六:2055.An easy problem
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.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int m, n;
char c;
vector<int> num(26);
for (int i = 0; i < 26; i++) {
num[i] = i + 1;
}
while (cin >> m) {
while (m--) {
cin >> c >> n;
if (c >= 'a' && c <= 'z') {
n = n - num[c - 'a'];
} else if (c >= 'A' && c <= 'Z') {
n = n + num[c - 'A'];
}
cout << n << endl;
}
}
return 0;
}
题目七:2010.水仙花数
Problem Description
春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3。
现在要求输出所有在m和n范围内的水仙花数。
Input
输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。
Output
对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class NUM {
public:
NUM(int M, int N) : m_min(M), m_max(N) {}
vector<int> find() const {
vector<int> result;
for (int n = m_min; n <= m_max; ++n) {
if (NN(n)) {
result.push_back(n);
}
}
return result;
}
private:
int m_min, m_max;
bool NN(int number) const {
int sum = 0;
string digits =to_string(number);
for (char digit : digits) {
sum += pow(digit - '0', 3);
}
return sum == number;
}
};
int main() {
int m, n;
while (cin >> m >> n) {
if (100 <= m && m <= n && n <= 999) {
NUM finder(m, n);
vector<int> NUMs = finder.find();
if (!NUMs.empty()) {
for (size_t i = 0; i < NUMs.size(); ++i) {
cout << NUMs[i];
if (i < NUMs.size() - 1) cout << " ";
}
cout << endl;
}
else {
cout << "no" <<endl;
}
}
}
return 0;
}
题目八:2057.A+B Again
Problem Description
There must be many A + B problems in our HDOJ , now a new one is coming.
Give you two hexadecimal integers , your task is to calculate the sum of them,and print it in hexadecimal too.
Easy ? AC it !
Input
The input contains several test cases, please process to the end of the file.
Each case consists of two hexadecimal integers A and B in a line seperated by a blank.
The length of A and B is less than 15.
Output
For each test case,print the sum of A and B in hexadecimal in one line.
#include<iostream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
int main(void){
__int64 a,b,sum;
while(cin>>hex>>a&&cin>>hex>>b){
sum=a+b;
if(sum<0){
sum=-sum;
cout<<hex<<uppercase<<"-"<<sum<<endl;
}else{
cout<<hex<<uppercase<<sum<<endl;
}
}
}
题目九:2011.多项式求和
Problem Description
多项式的描述如下:
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ...
现在请你求出该多项式的前n项的和。
Input
输入数据由2行组成,首先是一个正整数m(m<100),表示测试实例的个数,第二行包含m个正整数,对于每一个整数(不妨设为n,n<1000),求该多项式的前n项的和。
Output
对于每个测试实例n,要求输出多项式前n项的和。每个测试实例的输出占一行,结果保留2位小数。
#include <iostream>
#include <iomanip>
using namespace std;
double Sum(int n) {
double sum = 0.0;
for (int i = 1; i <= n; ++i) {
if (i % 2 == 1) {
sum += 1.0 / i; // 对于奇数项,进行加法操作
}
else {
sum -= 1.0 / i; // 对于偶数项,进行减法操作
}
}
return sum;
}
int main() {
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
int n;
cin >> n;
cout << fixed << setprecision(2) << Sum(n) << endl;
}
return 0;
}
题目十:2012.素数判定
Problem Description
对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。
Input
输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。
Output
对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。
#include <iostream>
using namespace std;
bool Prime(int num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 == 0 || num % 3 == 0) return false;
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) return false;
}
return true;
}
int main() {
int x, y;
while (cin >> x >> y) {
if (x == 0 && y == 0) break;
bool all= true;
for (int n = x; n <= y; ++n) {
int value = n * n + n + 41;
if (!Prime(value)) {
all= false;
break;
}
}
if (all) cout << "OK" << endl;
else cout << "Sorry" << endl;
}
return 0;
}