牛客专题训练

专题链接

1. 顺序结构习题

牛客竞赛语法入门班顺序结构习题

1001 这是一道签到题(多行字符串)

可用\拼接多行字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main()
{
cout << "zhe\n\
shi\n\
yi\n\
dao\n\
qian\n\
dao\n\
ti" << endl;
return 0;
}

1002 排列式(全排列)

c++可以用next_permutation()生成下一个排列

思路一:暴力遍历所有数字判断是否有重复数字,再检查式子是否符合乘法等式,超时。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstring>

using namespace std;

void check(int x)
{
int cnt[10];
memset(cnt, 0, sizeof cnt);
for (int a = x; a; a/=10) ++cnt[a%10];
for (int i = 1; i < 10; i ++) if (cnt[i] != 1) return;
int a = x / 100000;
int b = x % 100000 / 1000;
int c = x % 1000;
if (a == b * c) printf("%d = %d x %d\n", a, b, c);
b = x % 100000 / 10000;
c = x % 10000;
if (a == b * c) printf("%d = %d x %d\n", a, b, c);
}

int main()
{
for (int i = 100000000; i < 1000000000; i ++) check(i);
return 0;
}

思路二:全排列,AC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 9;
int a[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

int get(int i, int j)
{
int ans = 0;
while (i < j) ans = ans * 10 + a[i++];
return ans;
}
void check()
{
int a = get(0, 4);
int b = get(4, 6);
int c = get(6, 9);
if (a == b * c) printf("%d = %d x %d\n", a, b, c);
b = get(4, 5);
c = get(5, 9);
if (a == b * c) printf("%d = %d x %d\n", a, b, c);
}

void dfs(int idx)
{
if (idx == N) check();
for (int i = idx; i < N; i ++)
{
swap(a[idx], a[i]);
dfs(idx + 1);
swap(a[idx], a[i]);
}
}
int main()
{
dfs(0);
return 0;
}

方法三:next_permutation()函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 9;
int a[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

int get(int i, int j)
{
int ans = 0;
while (i < j) ans = ans * 10 + a[i++];
return ans;
}
void check()
{
int a = get(0, 4);
int b = get(4, 6);
int c = get(6, 9);
if (a == b * c) printf("%d = %d x %d\n", a, b, c);
b = get(4, 5);
c = get(5, 9);
if (a == b * c) printf("%d = %d x %d\n", a, b, c);
}

int main()
{
while (next_permutation(a, a + 9)) check();
return 0;
}

1003 小飞机

直接打印。

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
cout << " ** " << endl;
cout << " ** " << endl;
cout << "************" << endl;
cout << "************" << endl;
cout << " * * " << endl;
cout << " * * " << endl;
return 0;
}

1004 学姐的”Helloworld!”

看清楚,一般复制字符串过来比较稳妥。

1
2
3
4
5
6
7
#include <cstdio>
using namespace std;

int main() {
printf("Helo word!");
return 0;
}

1005 乘法表

格式输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 9; i ++)
{
for (int j = 1; j <= i; j ++)
{
printf("%d*%d= %d ",j, i, j * i);
}
printf("\n");
}
return 0;
}

1006 KiKi学程序设计基础

注意输出\n的时候要转义\

1
2
3
4
5
6
7
8
9
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
printf("printf(\"Hello world!\\n\");\n");
printf("cout << \"Hello world!\" << endl;");
return 0;
}

1007 疫情死亡率

百分号输出用%%

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
printf("%.3f%%\n", b*100.0 / a);
return 0;
}

1008 爱因斯坦的名言

难受,cout输出%时不需要转义

1
2
3
4
5
6
7
8
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
cout << "\"Genius is 1% inspiration and 99% perspiration.\"" << endl;
return 0;
}

1009 字符串输出1.0

没啥。

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
cout << "Welcome to ACM / ICPC!" << endl;
cout << "Welcome to ACM / ICPC!" << endl;
cout << "Welcome to ACM / ICPC!" << endl;
return 0;
}

1010 牛牛学说话之-整数

整数输入输出

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
int a;
cin >> a;
cout << a;
return 0;
}

1011 牛牛学说话之-浮点数

浮点数输入输出

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
double a;
cin >> a;
printf("%.3f", a);
return 0;
}

1012 牛牛学加法

加法。

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
cout << a + b;
return 0;
}

1013 牛牛学除法

整除

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
cout << a / b;
return 0;
}

1014 牛牛学取余

取余

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
cout << a % b;
return 0;
}

1015 浮点除法

整数浮点除法。

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
printf("%.3f", a * 1.0 / b);
return 0;
}

1016 计算带余除法

整除、求余

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
printf("%d %d", a / b, a % b);
return 0;
}

1017 K蝴蝶

减法

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
cout << a - b;
return 0;
}

1018 水题再次来袭:明天星期几?

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
int a;
cin >> a;
cout << a % 7 + 1;
return 0;
}

1019 开学?

好难

1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
int c = (a + (b % 7));
cout << (c > 7 ? c % 7 : c);
return 0;
}

1020 helloworld

如果用char数组,用范围遍历会多出一个以\0字符结尾的输出,

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int main() {
string a = "hello world";
for (auto i : a) printf("%c", i + 1);
return 0;
}

1021 a+b

%x输出十六进制数

1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
printf("%x\n", a + b);
return 0;
}

1022 整数的个位

1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int main() {
int a;
cin >> a;
printf("%d\n", a % 10);
return 0;
}

1023 整数的十位

1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int main() {
int a;
cin >> a;
printf("%d\n", a / 10 % 10);
return 0;
}

1024 反向输出一个四位数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int main() {
int a;
cin >> a;
while (a) {
cout << a % 10;
a /= 10;
}
return 0;
}

1025 总成绩和平均分计算

1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int main() {
double a, b, c;
cin >> a >> b >> c;
double sum = a + b + c;
printf("%.2f %.2f", sum, sum / 3);
return 0;
}

1026 计算平均成绩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int main() {
int x, sum = 0;
for (int i = 0; i < 5; i ++) {
cin >> x;
sum += x;
}
printf("%.1f", sum * 1.0 / 5);
return 0;
}

1027 牛牛学梯形

1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int main() {
int u, d, h;
cin >> u >> d >> h;
printf("%.3f", (u + d) * h / 2.0);
return 0;
}

1028 牛牛学矩形

1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
printf("%d\n%d", (a + b) * 2, a * b);
return 0;
}

1029 牛牛学立体

1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int main() {
int a, b, c;
cin >> a >> b >> c;
printf("%d\n%d", (a * b + b * c + a * c) * 2, a * b * c);
return 0;
}

1030 计算三角形的周长和面积

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a, b, c;
cin >> a >> b >> c;
double d = a + b + c;
double p = d / 2.0;
double s = sqrt(p * (p - a) * (p - b) * (p - c));
printf("circumference=%.2f area=%.2f", d, s);
return 0;
}

1031 你能活多少秒

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
const int s = 3.156 * 1e7;
int a;
cin >> a;
cout << 1LL * a * s << endl;
return 0;
}

1032 时间转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a;
cin >> a;
int h = a / 3600;
int m = a % 3600 / 60;
int s = a % 60;
printf("%d %d %d", h, m, s);
return 0;
}

1033 温度转换

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
double f;
cin >> f;
printf("%.3f", 5.0/9*(f - 32));
return 0;
}

1034 计算机内存

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int n;
cin >> n;
cout << (n * 1024 * 1024 / 4) << endl;
return 0;
}

1035 [NOIP2017]成绩

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a, b, c;
cin >> a >> b >> c;
cout << (0.2 * a) + (0.3 * b) + (0.5 * c) << endl;
return 0;
}

1036 KiKi的最高分

max

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a, b, c;
cin >> a >> b >> c;
cout << max(a, max(b, c)) << endl;
return 0;
}

1037 组队比赛

贪心

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
int sum = a + b + c + d;
int mx = max(a, max(b, max(c, d)));
int mi = min(a, min(b, min(c, d)));
cout << abs((mx + mi) * 2 - sum) << endl;
return 0;
}

1038 平方根

数据量不大,直接暴力了。这题标准做法应该用二分。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a;
cin >> a;
int ans = 1;
while (ans * ans <= a) ans ++;
cout << ans - 1;
return 0;
}

1039 长方体

列方程求解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a, b , c;
cin >> a >> b >> c;
int x, y, z;
y = sqrt(a * c / b);
x = a / y;
z = c / y;
cout << (x + y + z) * 4 << endl;
return 0;
}

1040 使徒袭来

要看清题目,求的是正实数不是正整数

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int n;
cin >> n;
printf("%.3f", 3 * pow(n, 1.0 / 3));
return 0;
}

1041 白兔的分身术

思维、看题目!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

using LL = long long;
int main() {
LL n;
cin >> n;
cout << n + 1 << endl;
return 0;
}

1042 纸牌

贪心

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int n;
cin >> n;
cout << (n + 1) / 2 << endl;
return 0;
}

1043 Tobaku Mokushiroku Kaiji

贪心

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a, b, c, d, e, f;
cin >> a >> b >> c >> d >> e >> f;
int ans = min(a, e) + min(b, f) + min(c, d);
cout << ans << endl;
return 0;
}

1044 珂朵莉的假动态仙人掌

贪心

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int n;
cin >> n;
int ans = 0;
int t = 1;
while (n >= t)
{
n -= t;
t = 3 - t;
ans ++;
}
cout << ans << endl;
return 0;
}

1045 旅游观光

思维

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int n;
cin >> n;
cout << (n + 1) / 2 - 1 << endl;
return 0;
}

1046 [NOIP2002]自由落体

1

1047 挂科

数学

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int n, x, y;
cin >> n >> x >> y;
cout << min(x, y) << " " << max(0, (x + y - n)) << endl;
return 0;
}

1048 得不到的爱情

属实不懂了

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int n, m;
cin >> n >> m;
cout << 1LL * n * m - n - m << endl;
return 0;
}

2. 选择结构习题

牛客竞赛语法入门班选择结构习题

A 比大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
if (a == b) cout << '=';
if (a > b) cout << '>';
if (a < b) cout << '<';
return 0;
}

B 卡拉兹函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a;
cin >> a;
if (a & 1) cout << 3 * a + 1;
else cout << a / 2;
return 0;
}

C 默契

这个空格没复制到,wa了几次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
if (a == b) cout << "Tacit!";
else cout << "No Tacit!";
return 0;
}

D 整除判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
if (a % b == 0) cout << "YES";
else cout << "NO";
return 0;
}

E CSimplemathproblem

题目没看清

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
long long x, y;
cin >> x >> y;
if (y % x == 0) cout <<x + y;
else cout << y - x;
return 0;
}

F 吃瓜群众

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int w;
cin >> w;
if (w > 2 && w % 2 == 0) cout << "YES, you can divide the watermelon into two even parts.";
else cout << "NO, you can't divide the watermelon into two even parts.";
return 0;
}

G jyq跳格子

思维题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int w;
cin >> w;
if ((w - 1) % 2 == 0) cout << w;
else cout << -1;
return 0;
}

H 小名的回答

思维

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a, b, n;
cin >> a >> b >> n;
if (a + b <= n && (n - a - b) % 2 == 0) cout << "YES";
else cout << "NO";
return 0;
}

I 牛妹数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int n;
cin >> n;
if (n > 50 && n % 2 == 0) cout << "yes";
else cout << "no";
return 0;
}

J 判断闰年

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int n;
cin >> n;
if ((n % 100 != 0 && n % 4 == 0) || n % 400 == 0) cout << "yes";
else cout << "no";
return 0;
}

K 统计数据正负个数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int x, a = 0, b = 0, n = 10;
while (n --)
{
cin >> x;
if (x > 0) a ++;
else if (x < 0) b ++;
}
cout << "positive:" << a << endl << "negative:" << b;
return 0;
}

L 小乐乐是否被叫家长

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a, b, c;
cin >> a >> b >> c;
if (((a + b + c) / 3.0) < 60) cout << "YES";
else cout << "NO";
return 0;
}

M 最大最小值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a, b, c;
cin >> a >> b >> c;
int mx = max(a, max(b, c));
int mi = min(a, min(b, c));
cout << "The maximum number is : " << mx << endl;
cout << "The minimum number is : " << mi << endl;
return 0;
}

N 送分题

有1的话,就加,没有就乘。可以添加括号,意味后面两个可能先加得到的结果更大

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int get(int a, int b)
{
return (a == 1 || b == 1) ? a + b : a * b;
}
int main() {
int a, b, c;
cin >> a >> b >> c;
if (c == 1)
cout << get(a, get(b, c));
else
cout << get(get(a, b), c);
return 0;
}

O 四季

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int ym;
cin >> ym;
int m = ym % 100;
string ans;
if (m >= 3 && m <= 5) ans = "spring";
else if (m >= 6 && m <= 8) ans = "summer";
else if (m >= 9 && m <= 11) ans = "autumn";
else ans = "winter";
cout << ans;
return 0;
}

P B是不是太迟了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int y, m, d;
char c;
cin >> y >> c >> m >> c >> d;
if (m < 10 || (m == 10 && d < 29)) cout << "No. It's not too late.";
else cout << "QAQ";
return 0;
}

Q 前天是哪天

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int y, m, d;
char c;
cin >> y >> c >> m >> c >> d;
d -= 2;
if (d < 1) {
--m;
if (m < 1) {
m = 12;
--y;
}
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
d = 31 + d;
}
if (m == 4 || m == 6 || m == 9 || m == 11) {
d = 30 + d;
}
if (m == 2)
{
if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) d = 29 + d;
else d = 28 + d;
}
}
printf("%d-%02d-%02d", y, m, d);
return 0;
}

R L1-2单位换算

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int a;
cin >> a;
cout << a * 12.0 *2.54 * 10;
return 0;
}

S 纸牌

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int n;
cin >> n;
cout << (n + 1) / 2 << endl;
return 0;
}

T 排队领水

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int n, a, b;
cin >> n >> a >> b;
cout << min(n - a, b + 1);
return 0;
}

U 可编程拖拉机比赛

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int n;
cin >> n;
int a = ceil(n * 0.1) - floor(n * 0.1);
int b = a + ceil(n * 0.2) - floor(n * 0.2);
int c = b + ceil(n * 0.3) - floor(n * 0.3);
printf("%d %d %d", a, b, c);
return 0;
}

V [NOIP2004]不高兴的津津

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
int d = 0, mx = 0;
for (int i = 1; i <= 7; i ++)
{
int a, b; cin >> a >> b;
if (a + b >= 8 && a + b > mx) {
mx = a + b;
d = i;
}
}
cout << d;
return 0;
}

W [NOIP2008]ISBN号码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
string s;
cin >> s;
int num = 0;
int id = 1;
for (int i = 0; i < s.size() - 1; i ++)
{
if (isdigit(s[i])) num += (s[i] - '0') * id++;
}
num %= 11;
if ((num == 10 && s.back() == 'X') || (num == s.back() - '0'))
cout << "Right";
else {
if (num == 10) s.back() = 'X';
else s.back() = (char)(num + '0');
cout << s;
}
return 0;
}

3. 循环结构习题

1001 上下金字塔

以中间最长的行,分割成上下两部分,先输出上部分,再输出最长的行,然后是下部分。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <bits/stdc++.h>

using namespace std;

void p(int n)
{
int len = n * 2 - 1;
for (int i = 1; i < n; i ++)
{
int sum = 2 * i - 1;
for (int j = 0; j < (len - sum) / 2; j ++) printf(" ");
for (int j = 0; j < sum; j ++) printf("*");
for (int j = 0; j < (len - sum) / 2; j ++) printf(" "); puts("");
}
for (int i = 0; i < len; i ++) printf("*"); puts("");
for (int i = n - 1; i > 0; i --)
{
int sum = 2 * i - 1;
for (int j = 0; j < (len - sum) / 2; j ++) printf(" ");
for (int j = 0; j < sum; j ++) printf("*");
for (int j = 0; j < (len - sum) / 2; j ++) printf(" "); puts("");
}
}
int main()
{
int n;
while (cin >> n)
{
p(n);
}
return 0;
}

1002 数字三角形

模拟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
int n;
cin >> n;
int t = 1;
for (int i = 1; i <= n; i ++)
{
for (int j = 0; j < i; j ++)
{
printf("%4d", t ++);
}
puts("");
}
return 0;
}

1003 字符金字塔

每行分四个部分考虑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
char c;
cin >> c;
for (int i = 'A'; i <= c; i ++)
{
for (int j = 0; j < c - i; j ++) printf(" ");
for (int j = 'A'; j < i; j ++) printf("%c", j);
printf("%c", i);
for (int j = i - 1; j >= 'A'; j --) printf("%c", j);
puts("");
}
return 0;
}

1004 涂小天与他的画

麻了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
int T;
cin >> T;
while (T --)
{
int n;
cin >> n;

int len = 2 * n;
// 前
for (int i = 1; i <= n / 2; i ++)
{
for (int j = i; j <= n / 2; j ++) printf(" ");
for (int j = 1; j <= 2 * i - 1; j ++) printf("*");
puts("");
}
// 中
for (int i = 0; i < n; i ++) printf("*"); puts("");
// 后
for (int i = 1; i <= n / 2; i ++)
{
for (int j = 1; j <= i; j ++) printf(" ");
for (int j = 1; j <= n - 2 * i; j ++) printf("*");
puts("");
}
}
return 0;
}

1005 箭形图案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n;
while (cin >> n)
{
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= (n - i + 1) * 2; j ++) cout << ' ';
for (int j = 1; j <= i; j ++) cout << '*';
puts("");
}
for (int i = 0; i <= n; i ++) cout << '*'; puts("");
for (int i = n; i > 0; i --)
{
for (int j = 1; j <= (n - i + 1) * 2; j ++) cout << ' ';
for (int j = 1; j <= i; j ++) cout << '*';
puts("");
}
}
return 0;
}

1006 牛牛学数列

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin >> n;
int ans;
for (int i = 1; i <= n; i ++)
ans += (i & 1 ? i : -i);
cout << ans;
return 0;
}

1007 牛牛学数列2

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin >> n;
double ans;
for (int i = 1; i <= n; i ++)
ans += 1.0 / i;
printf("%.6f", ans);
return 0;
}

1008 牛牛学数列3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin >> n;
double ans;
int t = 0;
for (int i = 1; i <= n; i ++)
{
t += (i & 1 ? 2 * i - 1 : -(2 * i - 1));
ans += 1.0 / t;
}
printf("%.3f", ans);
return 0;
}

1009 牛牛学数列4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin >> n;
long ans = 0;
int sum = 0;
for (int i = 1; i <= n; i ++)
{
sum += i;
ans += sum;
}
cout << ans;
return 0;
}

1010 牛牛学数列5

斐波那契数列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin >> n;
int a = 0, b = 1, c;
while (--n)
{
c = a + b;
a = b;
b = c;
}
cout << b;
return 0;
}

1011 牛牛学数列6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin >> n;
int a = 0, b = 1, c = 1, d;
while (--n)
{
d = a + 2 * b + c;
a = b;
b = c;
c = d;
}
cout << a;
return 0;
}

1012 A+B

1
2
3
4
5
6
7
8
9
10
11
12
#include <bits/stdc++.h>
using namespace std;

int main()
{
long long a, b;
while (cin >> a >> b)
{
cout << a + b << endl;
}
return 0;
}

1013 多组输入a+b

1
2
3
4
5
6
7
8
9
10
11
12
#include <bits/stdc++.h>
using namespace std;

int main()
{
long long a, b;
while (cin >> a >> b)
{
cout << a + b << endl;
}
return 0;
}

1014 多组输入a+b II

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin >> n;
long long a, b;
while (n -- && cin >> a >> b)
{
cout << a + b << endl;
}
return 0;
}

1015 多组数据a+b III

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
using namespace std;

int main()
{
long long a, b;
while (cin >> a >> b)
{
if (!a && !b) break;
cout << a + b << endl;
}
return 0;
}

1016 [NOIP2018]标题统计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <bits/stdc++.h>
using namespace std;

int main()
{
string a;
getline(cin, a);
int ans = 0;
for (auto c : a)
{
if(isdigit(c) || isalpha(c)) ans ++;
}
cout << ans;
return 0;
}

1017 栗酱数数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;
bool not4(int n)
{
while (n) {
if (n % 10 == 4) return false;
n /= 10;
}
return true;
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
if (i % 4 != 0 && not4(i))
cout << i << endl;
return 0;
}

1018 有趣的二进制

1

1019 [NOIP2006]数列

1

1020 只能吃土豆的牛牛

1

1021 [NOIP2015]金币

1

1022 托米历险记

1

1023 绩点

1

1024 买铅笔

1

1025 整数个数

1

1026 栗酱的文明2

1

1027 黑大火与他的猫

1

1028 [NOIP2000]进制转换

1

1029 素数判断

1

1030 Game

1

1031 [NOIP1999]Cantor表

1

1032 最大的差

1

1033 成绩统计

1

1034 糖果俱乐部

1

1035 数字计数

1

1036 热杆上的蚂蚁

1

1037 焦虑的蚂蚁

1

1038 拯救小a

1

1040 YoungManDon’tSayFive

1

1041 魔法数字变换

1

1042 [NOIP2005]陶陶摘苹果

1

1043 [NOIP2004]津津的储蓄计划

1

1044 [NOIP2002]级数求和

1

1045 cayun日常之赏月

1

1046 鹏

1

1047 D博弈与核心能源动力

1

1048 好数

1

1049 3和5和7

1

1050 回文对称数

1

1051 数位之和

1

1047 D博弈与核心能源动力

1


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!