C++语言题库(一)—— 基本知识类
目录
1. Hello World!
2. 据说一个人的标准体重应该是其身高(单位:厘米)减去100、再乘以0.9所得到的公斤数。已知市斤的数值是公斤数值的两倍。现给定某人身高,请你计算其标准体重应该是多少?
3. 给定一个华氏温度F,计算对应的摄氏温度C。计算公式:C=5×(F−32)/9。题目保证输入与输出均在整型范围内。
4. 计算4个整数的和与平均值。题目保证输入与输出均在整型范围内。
5. 程序每次读入一个正3位数,然后输出按位逆序的数字。注意:当输入的数字含有结尾的0时,输出不应带有前导的0。比如输入700,输出应该是7。
6. 输出21世纪中截止某个年份以来的所有闰年年份。注意:闰年的判别条件是该年年份能被4整除但不能被100整除、或者能被400整除。
7. 将输入的任意3个整数从小到大输出。
8. 对于给定的正整数N,求它的位数及其各位数字之和。
9. 给定N个正整数,请统计奇数和偶数各有多少个?
10. 将输入的一系列整数中的最小值与第一个数交换,然后将最大值与最后一个数交换,最后输出交换后的序列。注意:题目保证最大和最小值都是唯一的。
11. 从输入的N个整数中查找给定的X。如果找到,输出X的位置(从0开始数);如果没有找到,输出“Not Found”。
12. 对于给定的正整数N,需要你计算 S=1!+2!+3!+...+N!。
13. 对任意给定的一位正整数N,输出从1*1到N*N的部分口诀表。下面是一个完整的下三角九九口诀表:
14. 求一个给定的m×n矩阵各行元素之和。
15. 求两个给定正整数的最大公约数和最小公倍数。
1. Hello World!
本题要求编写程序,输出一个短句“Hello World!”。输入格式:
本题目没有输入。输出格式:
在一行中输出短句“Hello World!”。
答案:
#include<iostream>
using namespace std;int main(){cout<<"Hello World!";
}
2. 据说一个人的标准体重应该是其身高(单位:厘米)减去100、再乘以0.9所得到的公斤数。已知市斤的数值是公斤数值的两倍。现给定某人身高,请你计算其标准体重应该是多少?
输入格式:
输入第一行给出一个正整数H(100 < H ≤ 300),为某人身高。输出格式:
在一行中输出对应的标准体重,单位为市斤,保留小数点后1位。输入样例:
169输出样例:
124.2
答案:
#include <iostream>
#include <cstdio>
using namespace std;
int main() {double n;scanf("%lf", &n);printf("%.1lf", (n - 100) * 1.8);return 0;
}
3. 给定一个华氏温度F,计算对应的摄氏温度C。计算公式:C=5×(F−32)/9。题目保证输入与输出均在整型范围内。
输入格式:
输入在一行中给出一个华氏温度。输出格式:
在一行中按照格式“Celsius = C”输出对应的摄氏温度C的整数值。输入样例:
150输出样例:
Celsius = 65
答案:
#include<iostream>
using namespace std;int main(){int f; cin>>f;int c = 5*(f-32)/9;cout<<"Celsius = "<<c;return 0;
}
4. 计算4个整数的和与平均值。题目保证输入与输出均在整型范围内。
输入格式:
输入在一行中给出4个整数,其间以空格分隔。输出格式:
在一行中按照格式“Sum = 和; Average = 平均值”顺序输出和与平均值,其中平均值精确到小数点后一位。输入样例:
1 2 3 4
输出样例:
Sum = 10; Average = 2.5
答案:
#include <cstdio>
#include <iostream>
using namespace std;int main() {int a, b, c, d;scanf("%d%d%d%d", &a, &b, &c, &d);int sum = a + b + c + d;printf("Sum = %d; Average = %.1lf", sum, sum / 4.0);return 0;
}
5. 程序每次读入一个正3位数,然后输出按位逆序的数字。注意:当输入的数字含有结尾的0时,输出不应带有前导的0。比如输入700,输出应该是7。
输入格式:
每个测试是一个3位的正整数。输出格式:
输出按位逆序的数。输入样例:
123
输出样例:
321
答案:
#include <cstdio>
#include <iostream>
using namespace std;int main() {int t, a = 0;cin >> t;while (t) {a = a * 10 + t % 10;t /= 10;}cout << a;return 0;
}
6. 输出21世纪中截止某个年份以来的所有闰年年份。注意:闰年的判别条件是该年年份能被4整除但不能被100整除、或者能被400整除。
输入格式:
输入在一行中给出21世纪的某个截止年份。输出格式:
逐行输出满足条件的所有闰年年份,即每个年份占一行。
输入若非21世纪的年份则输出"Invalid year!"。若不存在任何闰年,则输出“None”。输入样例1:
2048
输出样例1:
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048输入样例2:
2000
输出样例2:
Invalid year!
答案:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;int main() {int n, cnt = 0;cin >> n;if (n <= 2000 || n > 2100) {cout << "Invalid year!";return 0;}for (int i = 2001; i <= n; i++) {if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {cout << i << endl;cnt++;}}if (cnt == 0)cout << "None";return 0;
}
7. 将输入的任意3个整数从小到大输出。
输入格式:
输入在一行中给出3个整数,其间以空格分隔。输出格式:
在一行中将3个整数从小到大输出,其间以“->”相连。输入样例:
4 2 8
输出样例:
2->4->8
答案:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;int main() {int a, b, c;cin >> a >> b >> c;if (a > b) {int t = a;a = b;b = t;}if (a > c) {int t = a;a = c;c = t;}if (b > c) {int t = b;b = c;c = t;}cout << a << "->" << b << "->" << c;return 0;
}
8. 对于给定的正整数N,求它的位数及其各位数字之和。
输入格式:
输入在一行中给出一个不超过109的正整数N。输出格式:
在一行中输出N的位数及其各位数字之和,中间用一个空格隔开。输入样例:
321
输出样例:
3 6
答案:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;int main() {int n; cin >> n;int a = 0, b = 0;while (n) {a++;b += n % 10;n /= 10;}cout << a << " " << b;return 0;
}
9. 给定N
个正整数,请统计奇数和偶数各有多少个?
输入格式:
输入第一行给出一个正整N(≤1000);第2行给出N个非负整数,以空格分隔。输出格式:
在一行中先后输出奇数的个数、偶数的个数。中间以1个空格分隔。输入样例:
9
88 74 101 26 15 0 34 22 77
输出样例:
3 6
答案:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;int main() {int n, a = 0, x;cin >> n;for (int i = 1; i <= n; i++) {cin >> x;if (x % 2 == 0){a++;}}cout << n - a << " " <<a;return 0;
}
10. 将输入的一系列整数中的最小值与第一个数交换,然后将最大值与最后一个数交换,最后输出交换后的序列。注意:题目保证最大和最小值都是唯一的。
输入格式:
输入在第一行中给出一个正整数N(≤10),第二行给出N个整数,数字间以空格分隔。输出格式:
在一行中顺序输出交换后的序列,每个整数后跟一个空格。输入样例:
5
8 2 5 1 4
输出样例:
1 2 5 4 8
答案:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;int main() {int a[11], n;cin >> n;for (int i = 1; i <= n; i++)cin >> a[i];int id = 1;for (int i = 1; i <= n; i++) {if (a[id] > a[i])id = i;}swap(a[id], a[1]);for (int i = 1; i <= n; i++) {if (a[id] < a[i])id = i;}swap(a[id], a[n]);for (int i = 1; i <= n; i++)cout << a[i] << " ";//每个整数后跟一个空格return 0;
}
11. 从输入的N个整数中查找给定的X。如果找到,输出X的位置(从0开始数);如果没有找到,输出“Not Found”。
输入格式:
输入在第一行中给出两个正整数N(≤20)和X,第二行给出N个整数。数字均不超过长整型,其间以空格分隔。输出格式:
在一行中输出X的位置,或者“Not Found”。输入样例1:
5 7
3 5 7 1 9
输出样例1:
2输入样例2:
5 7
3 5 8 1 9
输出样例2:
Not Found
答案:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;int main() {int a[21], n, x, id = 0;cin >> n >> x;for (int i = 0; i < n; i++)cin >> a[i];for (int i = 0; i < n; i++) {if (a[i] == x) {id = i;}}if (a[id] != x)cout << "Not Found";elsecout << id;return 0;
}
12. 对于给定的正整数N,需要你计算 S=1!+2!+3!+...+N!。
输入格式:
输入在一行中给出一个不超过10的正整数N。输出格式:
在一行中输出S的值。输入样例:
3
输出样例:
9
答案:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;int main() {int n, s = 0, t = 1;cin >> n;for (int i = 1; i <= n; i++) {t = t * i; // t = i!s += t;}cout << s;return 0;
}
13. 对任意给定的一位正整数N
,输出从1*1
到N*N
的部分口诀表。下面是一个完整的下三角九九口诀表:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
输入格式:
输入在一行中给出一个正整数N(1≤N≤9)。输出格式:
输出下三角N*N部分口诀表,其中等号右边数字占4位、左对齐。输入样例:
4
输出样例:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
答案:
#include <cstdio>
#include <iostream>
using namespace std;int main() {int n;scanf("%d", &n);for (int i = 1; i <= n; i++) {for (int j = 1; j <= i; j++) {printf("%d*%d=%-4d", j, i, i * j);}puts("");}return 0;
}
14. 求一个给定的m×n矩阵各行元素之和。
输入格式:
输入第一行给出两个正整数m和n(1≤m,n≤6)。随后m行,每行给出n个整数,其间以空格分隔。输出格式:
每行输出对应矩阵行元素之和。输入样例:
3 2
6 3
1 -8
3 12
输出样例:
9
-7
15
答案:
#include <iostream>
using namespace std;int main() {int m, n;cin >> m >> n;for (int i = 0; i < m; i++) {int sum = 0, a;for (int j = 0; j < n; j++) {cin >> a;sum += a;}cout << sum << endl;}return 0;
}
15. 求两个给定正整数的最大公约数和最小公倍数。
输入格式:
输入在一行中给出两个正整数M和N(≤1000)。输出格式:
在一行中顺序输出M和N的最大公约数和最小公倍数,两数字间以1空格分隔。输入样例:
511 292
输出样例:
73 2044
答案:
#include <iostream>
using namespace std;int main() {int a, b;cin >> a >> b;int x = a, y = b, tmp = 0;while (y != 0) {tmp = x % y;x = y;y = tmp;}cout << x << " " << a * b / x;return 0;
}