C++程序设计实践一下（题目来自杭州电子科技大学ACM）

题目六：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。

Input

Output

#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.

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 + ...

Input

Output

#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

Input

Output

#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;
}

|
28天前
|

23 1
|
1月前
|
C++
C++ : 程序设计简单实例
C++ : 程序设计简单实例
22 3
|
1月前
|

C++：程序设计实例
C++：程序设计实例
21 2
|
1月前
|
C++
C++解决线性代数矩阵转置 小实践
【6月更文挑战第3天】C++解决线性代数矩阵转置
22 2
|
1月前
|

35 1
|
1月前
|

C++课程设计实验杭州电子科技大学ACM题目（中）
C++课程设计实验杭州电子科技大学ACM题目（中）
17 1
|
23天前
|

12 0
|
24天前
|
C++

|
1月前
|

18 0
|
1月前
|

C++课程设计实验杭州电子科技大学ACM题目（下）
C++课程设计实验杭州电子科技大学ACM题目（下）
14 0