班级联欢会要买 100 件奖品,一共 100 元。钢笔 5 元一支、笔记本 3 元一本、铅笔 1 元 3 支(必须整 3 支买)。请问有多少种买法?
本题没有输入。
输出一个整数,表示满足条件的买法数(每种物品至少买 1 件)。
(本题没有输入)
4
#include <iostream>
using namespace std;
int main() {
int cnt = 0;
for (int i = 1; i <= 20; i++)
for (int j = 1; j <= 33; j++) {
int k = 100 - i - j;
if (k % 3 != 0) continue;
if (5 * i + 3 * j + k / 3 == 100) cnt++;
}
cout << cnt << endl;
return 0;
}编程社团做徽章,编号要满足「三位数各位数字的立方和等于它本身」才发。请你统计 100~999 中共有多少个这样的编号,并输出最小的一个。
本题没有输入。
输出两个整数,用空格隔开:满足条件的个数、最小的那个数。
(本题没有输入)
4 153
#include <iostream>
using namespace std;
int main() {
int cnt = 0, mn = -1;
for (int n = 100; n <= 999; n++) {
int a = n / 100, b = n / 10 % 10, c = n % 10;
if (a * a * a + b * b * b + c * c * c == n) {
cnt++;
if (mn < 0) mn = n;
}
}
cout << cnt << " " << mn << endl;
return 0;
}数学兴趣小组在研究「孪生质数」:相差 2 的两个质数,比如 3 和 5、11 和 13。请你数一数 2~n 之间有多少对孪生质数。
一行一个整数 n(5 ≤ n ≤ 10000)。
输出一个整数,表示孪生质数的对数。
20
4
#include <iostream>
using namespace std;
bool isP(int x) {
if (x < 2) return false;
for (int i = 2; i * i <= x; i++) if (x % i == 0) return false;
return true;
}
int main() {
int n, cnt = 0;
cin >> n;
for (int i = 2; i + 2 <= n; i++)
if (isP(i) && isP(i + 2)) cnt++;
cout << cnt << endl;
return 0;
}值日表要按天打印,第一件事就是知道这个月有几天。请你根据年份和月份,用枚举 + 闰年判断算出来。
一行两个整数 y 和 m(1900 ≤ y ≤ 2100,1 ≤ m ≤ 12)。
输出一个整数,表示该月的天数。
2024 2
29
#include <iostream>
using namespace std;
bool isLeap(int y) {
return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
}
int main() {
int y, m, d;
cin >> y >> m;
if (m == 2) d = isLeap(y) ? 29 : 28;
else if (m == 4 || m == 6 || m == 9 || m == 11) d = 30;
else d = 31;
cout << d << endl;
return 0;
}班里 n 个同学按学号轮流值日,从 1 号开始,一天一个人,循环往复。开学第 d 天轮到几号同学?
一行两个整数 n 和 d(1 ≤ n ≤ 60,1 ≤ d ≤ 100000)。
输出一个整数,表示第 d 天值日同学的序号。
5 7
2
#include <iostream>
using namespace std;
int main() {
int n, d;
cin >> n >> d;
cout << (d - 1) % n + 1 << endl;
return 0;
}上学路上,公交车一共经过 n 个站。每站先下车 a 人、再上车 b 人。请你模拟整个过程,输出最后车上的人数,以及全程最多时的人数。
第一行两个整数 n 和 s(1 ≤ n ≤ 100,0 ≤ s ≤ 100),表示站数和初始人数;接下来 n 行,每行两个整数 a 和 b(0 ≤ a, b ≤ 50),保证下车人数不超过车上人数。
输出两个整数,用空格隔开:最终人数、全程最多人数(含初始)。
2 10 3 5 4 2
10 12
#include <iostream>
using namespace std;
int main() {
int n, s, a, b, mx;
cin >> n >> s;
mx = s;
for (int i = 1; i <= n; i++) {
cin >> a >> b;
s = s - a + b;
if (s > mx) mx = s;
}
cout << s << " " << mx << endl;
return 0;
}小组合作要凑出恰好 k 分的总分,每个人贡献一个分数。请问有多少对同学(两人一组)的分数和正好是 k?
第一行两个整数 n 和 k(2 ≤ n ≤ 1000,0 ≤ k ≤ 200);第二行 n 个整数(0 ≤ 分数 ≤ 100)。
输出一个整数,表示满足条件的对数(i < j)。
5 100 40 60 60 40 50
4
#include <iostream>
using namespace std;
int main() {
int n, k, a[1005], cnt = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (a[i] + a[j] == k) cnt++;
cout << cnt << endl;
return 0;
}班会上玩报数游戏:n 个同学围成一圈,从 1 号开始报数,报到 m 的人退出,下一个人重新从 1 开始报。请问最后剩下的是几号同学?
一行两个整数 n 和 m(2 ≤ n ≤ 1000,1 ≤ m ≤ 100)。
输出一个整数,表示最后剩下的同学序号。
5 3
4
#include <iostream>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int r = 0;
for (int i = 2; i <= n; i++) r = (r + m) % i;
cout << r + 1 << endl;
return 0;
}数学老师说,1~n 里既是 3 的倍数、数字里又含有 5 的数很特别。请你数一数有多少个。
一行一个整数 n(1 ≤ n ≤ 100000)。
输出一个整数,表示符合条件的个数。
60
5
#include <iostream>
using namespace std;
int main() {
int n, cnt = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
if (i % 3 != 0) continue;
int x = i;
while (x > 0) {
if (x % 10 == 5) { cnt++; break; }
x = x / 10;
}
}
cout << cnt << endl;
return 0;
}学校新装了电梯,小管理员记录了它每次停靠的楼层。电梯从 1 楼出发,按记录依次停靠,请问它一共跑了多少层(上下都算)?
第一行一个整数 n(1 ≤ n ≤ 100),表示停靠次数;第二行 n 个整数(1 ≤ 楼层 ≤ 30)。
输出一个整数,表示总移动层数。
3 5 2 8
13
#include <iostream>
using namespace std;
int main() {
int n, x, cur = 1, s = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
if (x > cur) s = s + (x - cur);
else s = s + (cur - x);
cur = x;
}
cout << s << endl;
return 0;
}