当前位置:首页>java>【第35期】21天养成编程习惯:C++刷题第07天

【第35期】21天养成编程习惯:C++刷题第07天

  • 2026-08-20 23:26:44
【第35期】21天养成编程习惯:C++刷题第07天

跟老马一起“升级打怪”学编程!

  • 涉及考试:计算机学会编程能力等级认证(GESP)
  • 活动内容:提供不同等级的真题供小朋友们选择练习
  • 备考建议:根据自己备考的等级选择相应题目
  • 附加价值:可作为白名单比赛的备考训练
  • 本月打卡本月打卡题目

Day07:Openjudge1.4.17_判断闰年

【提交】

http://noi.openjudge.cn/ch0104/17/

【描述】

判断某年是否是闰年。

【输入】

输入只有一行,包含一个整数

【输出】

一行,如果公元a年是闰年输出Y,否则输出N

【样例输入】

2006

【样例输出】

N

【提示】

公历纪年法中,能被4整除的大多是闰年,但能被100整除而不能被400整除的年份不是闰年, 能被3200整除的也不是闰年,如1900年是平年,2000年是闰年,3200年不是闰年。

闰年:

(1)如果year能够被4整除,但是不能被100整除,则year是闰年。

(2)如果year能够被400整除,则year是闰年。

【参考程序】

C语言版本

/** 17:判断闰年* http://noi.openjudge.cn/ch0104/17/*/#include<cstdio>intmain(){int a;scanf("%d", &a);char y = ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0)) ? 'Y' : 'N';printf("%c", y);return0;}

C++版本

/** 17:判断闰年* http://noi.openjudge.cn/ch0104/17/*/#include<iostream>using namespace std;intmain(){int a;cin >> a;char y = ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0)) ? 'Y' : 'N';cout << y;return 0;}

Day07:GESP二级2024.12_寻找数字

【提交】

https://www.luogu.com.cn/problem/B4064

【问题描述】

小杨有一个正整数 ,小杨想知道是否存在一个正整数  满足 

【输入描述】

第一行包含一个正整数 ,代表测试数据组数。

对于每组测试数据,第一行包含一个正整数代表 

【输出描述】

对于每组测试数据,如果存在满足条件的正整数 ,则输出 ,否则输出 

【样例输入1】

3168110

【样例输出1】

23-1

【数据范围】

对于全部数据,保证有 

参考答案:

/** [GESP202412 二级] 寻找数字* https://www.luogu.com.cn/problem/B4064*/#include<iostream>#include<cmath>using namespace std;intmain(){int t, a;cin >> t;for (int i = 0;i < t;i++)    {cin >> a;int b = sqrt(sqrt(a));if (b * b * b * b == a)        {cout << b << endl;        }else        {cout << -1 << endl;        }    }return 0;}

Day07:Openjudge1.7.02_找第一个只出现一次的字符

【提交】

http://noi.openjudge.cn/ch0107/02/

【描述】

给定一个只包含小写字母的字符串,请你找到第一个仅出现一次的字符。如果没有,输出no。

【输入】

一个字符串,长度小于100000。

【输出】

输出第一个仅出现一次的字符,若没有则输出no。

【样例输入】

abcabd

【样例输出】

c

【参考程序】

C++版本

/** 02:找第一个只出现一次的字符* http://noi.openjudge.cn/ch0107/02/*/#include<iostream>#include<string>using namespace std;intmain(){string str;cin >> str;// 字符串的遍历for (int i = 0; i < str.size(); i++)    {char a = str[i];int count = 0;for (int j = 0; j < str.size(); j++)        {if (str[j] == a)            {                count += 1;            }        }if (count == 1)        {cout << a;return 0;        }    }cout << "no";return 0;}

Day07:Openjudge1.8.04_错误探测

【提交】

http://noi.openjudge.cn/ch0108/04/

【描述】

给定由0和1组成的矩阵,如果矩阵的每一行和每一列的1的数量都是偶数,则认为符合条件。

你的任务就是检测矩阵是否符合条件,或者在仅改变一个矩阵元素的情况下能否符合条件。

"改变矩阵元素"的操作定义为0变成1或者1变成0。

【输入】

输入行,第1行为矩阵的大小,以下行为矩阵的每一行的元素,元素之间以一个空格分开。

【输出】

如果矩阵符合条件,则输出OK;

如果矩阵仅改变一个矩阵元素就能符合条件,则输出需要改变的元素所在的行号和列号,以一个空格分开。

如果不符合以上两条,输出Corrupt。

【样例输入】

样例输入141 0 1 00 0 0 01 1 1 10 1 0 1样例输入241 0 1 00 0 1 01 1 1 10 1 0 1样例输入341 0 1 00 1 1 01 1 1 10 1 0 1

【样例输出】

样例输出1OK样例输出22 3样例输出3Corrupt

【参考程序】

C++版本

/** 04:错误探测* http://noi.openjudge.cn/ch0108/04/*/include<iostream>using namespace std;intmain(){int arr[105][105] = { 0 };int a[105] = { 0 };//存行累加,即1的个数int b[105] = { 0 };//存列累加,即1的个数int n;cin >> n;//读入数据for (int i = 1;i <= n;i++)    {for (int j = 1;j <= n;j++)        {cin >> arr[i][j];            a[i] += arr[i][j];//行累加        }    }//列累加for (int j = 1;j <= n;j++)    {for (int i = 1;i <= n;i++)        {            b[j] += arr[i][j];        }    }//计算行和列奇数的个数int cnt1 = 0, cnt2 = 0, r = 0, c = 0;for (int i = 1;i <= n;i++)    {if (a[i] % 2 != 0)        {            cnt1 += 1;            r = i;        }if (b[i] % 2 != 0)        {            cnt2 += 1;            c = i;        }    }//输出结果if (cnt1 == 0 && cnt2  == 0)    {//矩阵符合条件//没有奇数cout << "OK";    }elseif (cnt1 == 1 && cnt2 == 1)    {//仅改变一个矩阵元素就能符合条件//只有一个奇数cout << r << " " << c;    }else    {cout << "Corrupt";    }return 0;}

Day07:GESP五级2025.09_数字选取

【提交】

https://www.luogu.com.cn/problem/P14073

【问题描述】

给定正整数 ,现在有  共计  个整数。你需要从这  个整数中选取一些整数,使得所选取的整数中任意两个不同的整数均互质(也就是说,这两个整数的最大公因数为 )。请你最大化所选取整数的数量。

例如,当  时,可以选择  共计  个整数。可以验证不存在数量更多的选取整数的方案。

【输入描述】

一行,一个正整数 ,表示给定的正整数。

【输出描述】

一行,一个正整数,表示所选取整数的最大数量。

【样例输入1】

6

【样例输出1】

4

【样例输入2】

9

【样例输出2】

5

【说明/提示】

对于  的测试点,保证 

对于所有测试点,保证 

参考答案:

/** [GESP202509 五级] 数字选取* https://www.luogu.com.cn/problem/P14073*/include<iostream>include<vector>using namespace std;vector<intLinearSieve(int n){vector<int> primes;vector<boolis_prime(n + 1);//初始化为0,0-质数,1-合数for (int i = 2; i <= n; i++)    {if (is_prime[i] == 0)        {            primes.push_back(i);        }int len = primes.size();for (int j = 0; j < len && i * primes[j] <= n; j++)        {            is_prime[i * primes[j]] = 1;if (i % primes[j] == 0)break;        }    }return primes;}intmain(){int n;cin >> n;vector<int> lst = LinearSieve(n);cout << lst.size() + 1 << endl;return 0;}

Day07:GESP六级2024.03_好斗的牛

【提交】

https://www.luogu.com.cn/problem/P10377

【问题描述】

你有  个牛棚,从左到右一字排开。你希望把  头牛安置到牛棚里。麻烦的是,你的牛很好斗,如果他们附近有其他的牛,他们就会不安分地去挑事。其中,第  头牛的攻击范围是 ,这意味着,如果他的左边  个牛棚或者右边  个牛棚有其他牛,它就会去挑事。

你想留下一段连续的牛棚,并把其他牛棚都卖掉。请问您最少需要留下多少牛棚,才能保证至少存在一种方案能够把所有的  头牛都安置进剩余的牛棚里,且没有牛会挑事?

【输入描述】

第一行一个正整数 

第二行  个正整数 

第三行  个正整数 

【输出描述】

输出一行一个整数表示答案。

【样例输入1】

21 21 2

【样例输出1】

4

【样例1解释】

留下第 1、2、3、4 个牛棚,并在第  两个牛棚分别放下两头牛。

【样例输入2】

31 2 33 2 1

【样例输出2】

7

【说明/提示】

  • 对  的数据,保证 
  • 另有  的数据,保证 
  • 对  的数据,保证 
  • 对于所有的测试数据,保证 

参考答案:

方法一:

/** [GESP202403 六级] 好斗的牛* https://www.luogu.com.cn/problem/P10377*/#include<iostream>#include<vector>include<climits>using namespace std;int n, a[10], b[10], ans = INT_MAX;vector<boolvis(10);//idx 当前要安置的牛的编号;x当前牛的位置(即当前连续牛棚的右边界),s已经安置的牛的数量voiddfs(int idx, int x, int s){if (ans <= x)return;if (s == n)//n个牛全部装入牛棚    {        ans = min(ans, x);return;    }    vis[idx] = true;for (int i = 1;i <= n;i++)    {if (vis[i] == false)        {            dfs(i, x + max(b[idx], a[i]) + 1, s + 1);        }    }        vis[idx] = false;//通过 vis 数组标记和取消标记,实现所有排列的枚举。}intmain(){cin >> n;for (int i = 1;i <= n;i++)cin >> a[i];for (int i = 1;i <= n;i++)cin >> b[i];for (int i = 1;i <= n;i++)    {            dfs(i, 11);    }cout << ans << endl;return 0;}

方法二:

#include<iostream>#include<vector>#include<algorithm>#include<climits>using namespace std;intmain(){int n;cin >> n;vector<inta(n)b(n);for (int i = 0; i < n; ++i) {cin >> a[i];    }for (int i = 0; i < n; ++i) {cin >> b[i];    }vector<intcows(n);for (int i = 0; i < n; ++i) {        cows[i] = i;    }int min_length = INT_MAX;do {vector<intpositions(n, 0);for (int i = 1; i < n; ++i) {int prev_cow = cows[i-1];int current_cow = cows[i];int required = max(a[current_cow], b[prev_cow]) + 1;            positions[i] = positions[i-1] + required;        }int length = positions.back() + 1;if (length < min_length) {            min_length = length;        }    } while (next_permutation(cows.begin(), cows.end()));cout << min_length << endl;return 0;}

青少年编程竞赛交流

「青少年编程竞赛交流群」已成立(适合6至18周岁的青少年),添加小助手微信,让他邀请大家进入学习群。进群之后大家可以参与定期组织的21天刷题打卡、等级考试测评、教育部白名单比赛辅导以及青少年编程组队竞赛等活动。

最新文章

随机文章