2020-10-17 十一届蓝桥杯B组第二场C++
我是菜鸡就是自己记录一下
题目
试题 A: 门牌制作(5)
这个题搞个判断方式,暴力遍历一遍即可
答案是624
我考试时居然忘了设置一个数有多个2的情况我是真的混呐
#include<iostream>
using namespace std;
int ans = 0;
int temp;
void judge(int n) //某个数有几个2就加到ans上
{
while (n)
{
temp = n % 10;
if (temp == 2)
{
ans++;
}
n /= 10;
}
}
int main()
{
for (int i = 1; i <= 2020; i++)
{
judge(i);
}
cout << ans;
}
试题 B: 既约分数(5)
先说我的思路,首先用一个gcd函数判断最大公约数
然后双重for循环暴力了就可以
答案是2481215
#include<iostream>
using namespace std;
int ans = 0;
int gcd(int a, int b) //辗转相除法 比如a:45 b:13 ,
//45%13=6,b左移变为a:13,b变成6
//直至最后余数即b变为零 这次循环的a就是他俩的最大公约数
{
return b == 0 ? a : gcd(b,a % b);
}
int main()
{
for (int i = 1; i <= 2020; i++)
{
for (int j = 1; j <= 2020; j++)
{
if (gcd(i, j) == 1)
{
ans++;
}
}
}
cout << ans;
}
试题 C: 蛇形填数(10)
这个题可以侧着看,就像一个大三角形
第一层有一个数 第二层有两个数 第n层有n个数
另外第二行第二列 是在三角形的第三层
第三行第三列 是在三角形的第五层
第n行第n列 在 三角形的2n-1层
题中20行20列 对应着39层 从左往右第20个数因为图中奇数层都是从左往右增长的所以求出前38层之和 再加上20就可以
前n项和公式n(a1+an)/2=38*(1+38)/2=741
741+20=761 也就是答案
试题 D:跑步锻炼(10)
这个题麻烦但称不上难 当然作为菜鸡 我又错了
模拟日期变化即可 注意闰年判断以及年月日星期的更替就好
考试时候搞错了贼鸡儿气
我的代码
#include<iostream>
using namespace std;
int ans = 0;
int mouth[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
bool judge(int n) //闰年
{
if (n % 400 == 0 || (n % 100 != 0 && n % 4 == 0))
{
return 1;
}
return 0;
}
int main()
{
int nn, yy, rr;
int xingqi = 6;
for (nn = 2000, yy = 1, rr = 1; nn < 2020 || yy < 10 || rr <= 1; rr++)
{
if (rr > mouth[yy])
{
rr -= mouth[yy];
yy++;
if (yy > 12)
{
yy -= 12;
nn++;
if (judge(nn)) mouth[2] = 29; //判断闰年 因为已经看了2000是闰年所以最开始让mouth2=29了
else mouth[2] = 28;
}
}
if (rr == 1 || xingqi == 1) ans += 2; //加步数
else ans += 1;
xingqi = (xingqi + 1) % 7;
}
cout << ans;
}
试题 E: 七段码(15)
看到这个题是自闭的,感觉和dfs啥的有关系但是不会运用,然后闲着也是闲着
暴力杯就硬暴力试了试 当然又错了- -正确答案应该是80
我的是82 但不知道哪错了
我的代码
#include<iostream>
using namespace std;
int ans = 0;
bool judge(int a, int b, int c, int d, int e, int f, int g)
{
int sum = a + b + c + d + e + f + g;
if (a == 1) //与a相连的有b和f
{
if (b == 0 && f == 0&&sum>1) return 0;
}
if (b == 1) //与b相连的有a和c g
{
if (a == 0 && c == 0 && g == 0 && sum > 1) return 0;
}
if (c == 1) //与c相连的有bgd
{
if (b == 0 && d == 0 && g == 0 && sum > 1) return 0;
}
if (d == 1) //与d相连的有ce
{
if (c == 0 && e == 0 && sum > 1) return 0;
}
if (e == 1) //与e相连的有dgf
{
if (f == 0 && d == 0 && g == 0 && sum > 1) return 0;
}
if (f == 1) //与f相连的有gae
{
if (a == 0 && e == 0 && g == 0 && sum > 1) return 0;
}
if (g == 1) //与g相连的有cbfe
{
if (b == 0 && c == 0 && e == 0 && f == 0 && sum > 1) return 0;
}
//全0页不行哦
if (a == 0 && b == 0 && c == 0 && d == 0 && f == 0 && g == 0) return 0;
return 1;
}
int main()
{
int a, b, c, d, e, f, g;
for (a = 0; a <= 1; a++)
{
for (b = 0; b <= 1; b++)
{
for (c = 0; c <= 1; c++)
{
for (d = 0; d <= 1; d++)
{
for (e = 0; e <= 1; e++)
{
for (f = 0; f <= 1; f++)
{
for (g = 0; g <= 1; g++)
{
if (judge(a, b, c, d, e, f, g) == 1) ans++;
}
}
}
}
}
}
}
cout << ans;
}
大佬的 输出是80
/*
* @Date: 2020-10-17 21:20:03
* @LastEditTime: 2020-10-17 21:27:00
* @Author's blog: nuoyanli
* @Description: Plum blossom from the bchter cold!
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
char a[N][N];
int n, m, vis[N][N];
void dfs(int r, int c, int f) {
if (r < 0 || r >= m || c < 0 || c >= n) {
return;
}
if (vis[r][c] > 0 || a[r][c] != '1') {
return;
}
vis[r][c] = f;
for (int i = -1; i <= 1; i++){
for (int j = -1; j <= 1; j++){
if (i != 0 || j != 0){
dfs(r + i, c + j, f);
}
}
}
}
int main() {
int sum = 0;
freopen("in.txt", "r", stdin);
while (~scanf("%d%d", &m, &n)) {
for (int i = 0; i < m; i++){
scanf("%s", a[i]);
}
memset(vis, 0, sizeof(vis));
int cnt = 0;
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
if (vis[i][j] == 0 && a[i][j] == '1'){
dfs(i, j, ++cnt);
}
}
}
if (cnt == 1){
sum += 1;
}
}
printf("%d\n", sum);
return 0;
}
试题 F: 成绩统计(15)
水题 就是有个四舍五入
样例输入:7 80 92 45 74 88 100 0
#include<iostream>
using namespace std;
int main()
{
int n,jige=0,youxiu=0;
cin >> n;
int* a = new int[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
if (a[i] >= 60)
{
jige++;
if (a[i] >= 85)
{
youxiu++;
}
}
}
//四舍五入 因为计算机默认是吧小数都去掉,那我们就多加0.5,这样就变相让计算机实现了四舍五入
/*double ji = jige*100 + n * 0.5;
double you = youxiu*100 + n * 0.5;*/
cout << (int)(jige * 100 + n * 0.5) / n <<"%"<< endl << (int)(youxiu * 100 + n * 0.5) / n <<"%" ;
}
试题 G: 回文日期(20)
借用上一个相关试题代码
本题因为最大就是8999年的最后一天所以条件就是小于9000年即可
我当然是做错了
#include<iostream>
#include<iomanip>
using namespace std;
bool flag = 0;
int mouth[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
bool judge(int n) //闰年
{
if (n % 400 == 0 || (n % 100 != 0 && n % 4 == 0))
{
return 1;
}
return 0;
}
bool judge1(int nn, int yy, int rr)
{ //第一对应 第八 第二对应第七 第三对应第六
if (nn / 1000 == rr % 10 && nn / 100 % 10 == rr / 10 % 10 && nn / 10 % 10 == yy % 10 && nn % 10 == yy / 10 % 10)
{
return 1;
}
return 0;
}
bool judge2(int nn, int yy, int rr)
{
if (judge1(nn, yy, rr))
{
if (nn / 1000 == nn / 10 % 10 && nn % 10 == nn / 100 % 10)
{
return 1;
}
}
return 0;
}
int main()
{
int nn, yy, rr;
int xingqi = 6;
int n;
cin >> n;
nn = n / 10000;
yy = nn / 100 % 10 + nn / 1000 % 10 * 10;
rr = nn % 10 + nn / 10 % 10 * 10;
for ( ;nn < 9000; rr++)
{
if (rr > mouth[yy])
{
rr -= mouth[yy];
yy++;
if (yy > 12)
{
yy -= 12;
nn++;
if (judge(nn)) mouth[2] = 29; //判断闰年 因为已经看了2000是闰年所以最开始让mouth2=29了
else mouth[2] = 28;
}
}
if (judge1(nn, yy, rr))//是不是回文
{
if (flag==0) //只输出一次回文
{
//注意输出填充
cout << nn << setfill('0') << setw(2)<<yy << setfill('0') << setw(2) << rr << endl;
flag = 1;
}
if (judge2(nn, yy, rr))
{
cout << nn << setfill('0') << setw(2) << yy << setfill('0') << setw(2) << rr; return 0;
}
}
}
return 0;
}
试题 H: 子串分值和(20)
我的思路是一个函数用来判断字串的权值
主函数用两个for循环分别确定开始位置和长度来遍历
#include<iostream>
using namespace std;
int ans = 0;
int a[27];
void fenzhi(string str, int i, int j) //确定某个子串的分值
{
//a[27] = { 0 };
int a[27] = { 0 };
for (int t = j; t < j + i; t++)
{
if (a[str[t] - 'a'] == 0)
{
ans++;
a[str[t] - 'a'] = 1;
}
}
}
int main()
{
string str;
cin >> str;
for (int i = 1; i <= str.length(); i++) //子串长度
{
for (int j = 0; j < str.length(); j++) //开始位置
{
if (i + j <= str.length()) //长度足够或者说允许
{
fenzhi(str, i, j);
}
}
}
cout << ans;
}
试题 I: 平面切分(25)
别问问就是不会 待补
试题 J: 字串排序(25)
不会待补
总结
算下来就对了4个题 50分? 事后再做感觉也不是很难,还是自己积累的不够深
绝壁连个三都混不到啊我的三百块么得了唉- -