题目来源:第十四届蓝桥杯模拟赛第一期
注:代码都是自己写的,不是参考答案!
1. 二进制位数
问题描述:
十进制整数2在十进制中是1位数,在二进制中对应10 ,是2位数。
十进制整数22在十进制中是2位数,在二进制中对应10110 ,是5位数。
请问十进制整数2022在二进制中是几位数?
代码思路:创建一个一位数组,存放每次计算后的余数,最后倒序输出即为转换后的二进制数。十进制数转换为二进制数的方法是:除2取余,当商为0时,整个算法结束。
#include<stdio.h>
int main()
{
int m, number,quotient,length=0;
int array[20], i=0;
printf("Input an integer number:");
scanf_s("%d",&m);
number = m;
do
{
array[i++] = m % 2;
m = m / 2;
length++;
} while (m != 0);
printf("十进制整数%d在二进制中是%d位数\n",number,length);
printf("十进制整数%d的二进制表示为:", number);
for (int j = length-1; j >=0; j--)
printf("%d", array[j]);
}
运行结果
2. 晨跑
问题描述:
小蓝每周六、周日都晨跑,每月的 1、11、21、31日也晨跑。其它时间不晨跑。
已知2022年1月1日是周六,请问小蓝整个2022年晨跑多少天?
算法思路:首先创建一个一维数组,记录每个月的天数。day用来记录每周的星期数,每七天一循环,j用来记录每月的天数,根据一维数组中的值进行每月一循环。
#include<stdio.h>
#include<string.h>
int main()
{
int day = 6, count = 0;
int array[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; //2022年12个月的天数
for (int i = 0; i < sizeof(array) / sizeof(array[0]); i++) //以每月为单位进行循环
{
printf("%d月的晨跑日期为:", i + 1);
for (int j = 1; j <= array[i]; j++, day++) //以每月的天数为单位进行循环
{
if (day == 6 || day == 7 || j == 1 || j == 11 || j == 21 || j == 31) //每周六、周日都晨跑,每月的 1、11、21、31日也晨跑
{
if (day % 7 == 0) day = 0; //每遇到周日就清空
printf(" %d.%d ", i + 1, j);
count++; //满足条件,则晨跑天数+1
}
}
printf("\n");
}
printf("小蓝整个2022年晨跑:%d天", count);
}
运行结果
3. 调和级数
问题描述:
小蓝特别喜欢调和级数 S(n)=1/1+1/2+1/3+1/4+…+1/n 。
请问,n 至少为多大时,S(n)>12 ?
#include<stdio.h>
int main()
{
double Sn = 0.0;
int n = 1;
while (Sn <= 12)
{
Sn = Sn + 1.0 / n;
n++;
}
printf("n至少为%d时,S(n)>12", n);
}
n为整型,因此不能写成1/n(整型除整型,结果也为整型),而要写成1.0/n(浮点型除整型,结果为浮点型)
运行结果
4. 山谷
问题描述:
给定一个字母矩阵,如果矩阵中的某个位置不在四条边上,而且该位置上的字母小于其上下左右四个位置的字母,则称为一个山谷。
例如,对于如下矩阵
共有两个山谷,位于第二行第二列和第四列。请注意第二行第三列和第三行第五列都不是山谷。
对于如下30行60列的字母矩阵(请用等宽字体查看),请问有多少个山谷?
算法思路:既然位置不能在四条边上,则循环取值时只用取中间的一块,循环遍历判断每个位置是否满足条件即可。
#include<stdio.h>
int main()
{
char array[30][60] = { {'P','H','Q','G','H','U','M','E','A','Y','L','N','L','F','D','X','F','I','R','C','V','S','C','X','G','G','B','W','K','F','N','Q','D','U','X','W','F','N','F','O','Z','V','S','R','T','K','J','P','R','E','P','G','G','X','R','P','N','R','V','Y'},
{'S','T','M','W','C','Y','S','Y','Y','C','Q','P','E','V','I','K','E','F','F','M','Z','N','I','M','K','K','A','S','V','W','S','R','E','N','Z','K','Y','C','X','F','X','T','L','S','G','Y','P','S','F','A','D','P','O','O','E','F','X','Z','B','C'},
{'O','E','J','U','V','P','V','A','B','O','Y','G','P','O','E','Y','L','F','P','B','N','P','L','J','V','R','V','I','P','Y','A','M','Y','E','H','W','Q','N','Q','R','Q','P','M','X','U','J','J','L','O','O','V','A','O','W','U','X','W','H','M','S'},
{'N','C','B','X','C','O','K','S','F','Z','K','V','A','T','X','D','K','N','L','Y','J','Y','H','F','I','X','J','S','W','N','K','K','U','F','N','U','X','X','Z','R','Z','B','M','N','M','G','Q','O','O','K','E','T','L','Y','H','N','K','O','A','U'},
{'G','Z','Q','R','C','D','D','I','U','T','E','I','O','J','W','A','Y','Y','Z','P','V','S','C','M','P','S','A','J','L','F','V','G','U','B','F','A','A','O','V','L','Z','Y','L','N','T','R','K','D','C','P','W','S','R','T','E','S','J','W','H','D'},
{'I','Z','C','O','B','Z','C','N','F','W','L','Q','I','J','T','V','D','W','V','X','H','R','C','B','L','D','V','G','Y','L','W','G','B','U','S','B','M','B','O','R','X','T','L','H','C','S','M','P','X','O','H','G','M','G','N','K','E','U','F','D'},
{'X','O','T','O','G','B','G','X','P','E','Y','A','N','F','E','T','C','U','K','E','P','Z','S','H','K','L','J','U','G','G','G','E','K','J','D','Q','Z','J','E','N','P','E','V','Q','G','X','I','E','P','J','S','R','D','Z','J','A','Z','U','J','L'},
{'L','C','H','H','B','F','Q','M','K','I','M','W','Z','O','B','I','W','Y','B','X','D','U','U','N','F','S','K','S','R','S','R','T','E','K','M','Q','D','C','Y','Z','J','E','E','U','H','M','S','R','Q','C','O','Z','I','J','I','P','F','I','O','N'},
{'E','E','D','D','P','S','Z','R','N','A','V','Y','M','M','T','A','T','B','D','Z','Q','S','O','E','M','U','V','N','P','P','P','S','U','A','C','B','A','Z','U','X','M','H','E','C','T','H','L','E','G','R','P','U','N','K','D','M','B','P','P','W'},
{'E','Q','T','G','J','O','P','A','R','M','O','W','Z','D','Q','Y','O','X','Y','T','J','B','B','H','A','W','D','Y','D','C','P','R','J','B','X','P','H','O','O','H','P','K','W','Q','Y','U','H','R','Q','Z','H','N','B','N','F','U','V','Q','N','Q'},
{'Q','L','R','Z','J','P','X','I','O','G','V','L','I','E','X','D','Z','U','Z','O','S','R','K','R','U','S','V','O','J','B','R','Z','M','W','Z','P','O','W','K','J','I','L','E','F','R','A','A','M','D','I','G','P','N','P','U','U','H','G','X','P'},
{'Q','N','J','W','J','M','W','A','X','X','M','N','S','N','H','H','L','Q','Q','R','Z','U','D','L','T','F','Z','O','T','C','J','T','N','Z','X','U','G','L','S','D','S','M','Z','C','N','O','C','K','V','F','A','J','F','R','M','X','O','T','H','O'},
{'W','K','B','J','Z','W','U','C','W','L','J','F','R','I','M','P','M','Y','H','C','H','Z','R','I','W','K','B','A','R','X','B','G','F','C','B','C','E','Y','H','J','U','G','I','X','W','T','B','V','T','R','E','H','B','B','C','P','X','I','F','B'},
{'X','V','F','B','C','G','K','C','F','Q','C','K','C','O','T','Z','G','K','U','B','M','J','R','M','B','S','Z','T','S','S','H','F','R','O','E','F','W','S','J','R','X','J','H','G','U','Z','Y','U','P','Z','W','W','E','I','Q','U','R','P','I','X'},
{'I','Q','F','L','D','U','U','V','E','O','O','W','Q','C','U','D','H','N','E','F','N','J','H','A','I','M','U','C','Z','F','S','K','U','I','D','U','B','U','R','I','S','W','T','B','R','E','C','U','Y','K','A','B','F','C','V','K','D','Z','E','Z'},
{'T','O','I','D','U','K','U','H','J','Z','E','F','C','Z','Z','Z','B','F','K','Q','D','P','Q','Z','I','K','F','O','B','U','C','D','H','T','H','X','D','J','G','K','J','E','L','R','L','P','A','X','A','M','C','E','R','O','S','W','I','T','D','P'},
{'T','P','C','C','L','I','F','K','E','L','J','Y','T','I','H','R','C','Q','A','Y','B','N','E','F','X','N','X','V','G','Z','E','D','Y','Y','H','N','G','Y','C','D','R','U','D','M','P','H','M','E','C','K','O','T','R','W','O','S','P','O','F','G'},
{'H','F','O','Z','Q','V','L','Q','F','X','W','W','K','M','F','X','D','Y','Y','G','M','D','C','A','S','Z','S','G','O','V','S','O','D','K','J','G','H','C','W','M','B','M','X','R','M','H','U','Y','F','Y','Q','G','A','J','Q','K','C','K','L','Z'},
{'N','A','Y','X','Q','K','Q','O','Y','Z','W','M','Y','U','B','Z','A','Z','C','P','K','H','K','T','K','Y','D','Z','I','V','C','U','Y','P','U','R','F','M','B','I','S','G','E','K','Y','R','G','Z','V','X','D','H','P','O','A','M','V','A','F','Y'},
{'R','A','R','X','S','V','K','H','T','Q','D','I','H','E','R','S','I','G','B','H','Z','J','Z','U','J','X','M','M','Y','S','P','N','A','R','A','E','W','K','E','G','J','C','C','V','H','H','R','J','V','B','J','T','S','Q','D','J','O','O','T','G'},
{'P','K','N','F','P','F','Y','C','G','F','I','E','O','W','Q','R','W','W','W','P','Z','S','Q','M','E','T','O','G','E','P','S','P','X','N','V','J','I','U','P','A','L','Y','Y','N','M','K','M','N','U','V','K','L','H','S','E','C','D','W','R','A'},
{'C','G','F','M','Z','K','G','I','P','D','F','O','D','K','J','M','J','Q','W','I','Q','P','U','O','Q','H','I','M','V','F','V','U','Z','W','Y','V','I','J','G','F','U','L','L','K','J','D','U','H','S','J','A','F','B','T','L','K','M','F','Q','R'},
{'M','Y','J','F','J','N','H','H','S','S','Q','C','T','Y','D','T','E','A','M','D','C','J','B','P','R','H','T','N','E','G','Y','I','W','X','G','C','J','W','L','G','R','S','M','E','A','E','A','R','W','T','V','J','S','J','B','A','O','I','O','J'},
{'L','W','H','Y','P','N','V','R','U','I','H','O','S','W','K','I','F','Y','G','T','Y','D','H','A','C','W','Y','H','S','G','E','W','Z','M','T','G','O','N','Z','L','T','J','H','G','A','U','H','N','I','H','R','E','Q','G','J','F','W','K','J','S'},
{'M','T','P','J','H','A','E','F','Q','Z','A','A','U','L','D','R','C','H','J','C','C','D','Y','R','F','V','V','R','I','V','U','Y','E','E','G','F','I','V','D','R','C','Y','G','U','R','Q','D','R','E','D','A','K','U','B','N','F','G','U','P','R'},
{'O','Q','Y','L','O','B','C','W','Q','X','K','Z','M','A','U','S','J','G','M','H','C','M','H','G','D','N','M','P','H','N','Q','K','A','M','H','U','R','K','T','R','F','F','A','C','L','V','G','R','Z','K','K','L','D','A','C','L','L','T','E','O'},
{'J','O','M','O','N','X','R','Q','Y','J','Z','G','I','N','R','N','N','Z','W','A','C','X','X','A','E','D','R','W','U','D','X','Z','R','F','U','S','E','W','J','T','B','O','X','V','Y','N','F','H','K','S','T','C','E','N','A','U','M','N','D','D'},
{'X','F','D','M','V','Z','C','A','U','T','D','C','C','K','X','A','A','Y','D','Z','S','X','T','T','O','B','B','G','Q','N','G','V','V','P','J','G','O','J','O','G','L','M','K','X','G','B','F','C','P','Y','P','C','K','Q','C','H','B','D','D','Z'},
{'W','R','X','B','Z','M','Q','R','L','X','V','O','B','T','W','H','X','G','I','N','F','G','F','R','C','C','L','M','Z','N','M','J','U','G','W','W','B','S','Q','F','C','I','H','U','B','S','J','O','L','L','M','S','Q','S','G','H','M','C','P','H'},
{'E','L','S','O','T','F','L','B','G','S','F','N','P','C','U','Z','S','R','U','P','C','H','Y','N','V','Z','H','C','P','Q','U','G','R','I','W','N','I','Q','X','D','F','J','P','W','P','X','F','B','L','K','P','N','P','E','E','L','F','J','M','T'} };
int count = 0;
for (int i = 1; i < 29; i++)
for (int j = 1; j < 59; j++)
{
if (array[i][j] < array[i - 1][j] && array[i][j] < array[i + 1][j] && array[i][j] < array[i][j - 1] && array[i][j] < array[i][j + 1])
{
count++;
printf("第%d个山谷位于:第%d行,第%d列\n", count, i + 1, j + 1);
}
}
printf("该矩阵中共有%d个山谷\n", count);
}
运行结果
5. 最小矩阵
问题描述:
小蓝有一个100行100列的矩阵,矩阵的左上角为1。其它每个位置正好比其左边的数大2,比其上边的数大1。
例如,第1行第2列为3,第2行第2列为4,第10行第20列为48。
小蓝想在矩阵中找到一个由连续的若干行、连续的若干列组成的子矩阵,使得其和为2022,请问这个子矩阵中至少包含多少个元素(即子矩阵的行数和列数的乘积)。
算法思路:创建一个二维数组b用来存放从当前位置开始到其余各位置的累计值,minnum记录每次满足条件的矩阵中的最少元素。该算法共有四层嵌套循环,最里面的两层是一个小循环,表示每次从某一固定位置开始,计算该位置到其余位置的所有累计值;最外面的两层是一个大循环,表示依次从矩阵最上方开始,计算每个位置到其余位置的所有累计值。
#include<stdio.h>
int main()
{
int a[100][100], b[100][100], count = 0, minnum = 10000, elenum;
for (int i = 0; i < 100; i++)
for (int j = 0; j < 100; j++)
{
if (i == 0)
{
if (j == 0) a[i][j] = 1; //矩阵左上角为1
else a[i][j] = a[i][j - 1] + 2; //第一行从第二个元素开始,每个元素为前一个元素+2
}
else a[i][j] = a[i - 1][j] + 1; //除第一行的其余元素为上方元素+1
}
for (int m = 0; m < 100; m++) //大嵌套,计算矩阵每个位置上的所有累计值
for (int n = 0; n < 100; n++)
for (int i = m; i < 100; i++) //小嵌套,计算以当前位置为起始的所有累计值
for (int j = n; j < 100; j++)
{
if (i == m && j == n) b[i][j] = a[i][j]; //以当前元素为起始位置开始计算其它元素的累计值
else if (i == m && j > n) b[i][j] = a[i][j] + b[i][j - 1]; //若该元素在左上角元素的右方,则累计值为前一个元素的累计值+当前元素的值
else if (j == n && i > m) b[i][j] = a[i][j] + b[i - 1][j]; //若该元素在左上角元素的下方,则累计值为上一个元素的累计值+当前元素的值
else b[i][j] = a[i][j] + b[i][j - 1] + b[i - 1][j] - b[i - 1][j - 1];
//若在其他位置,则累计值为当前元素的值+前一个元素的累计值+上一个元素的累计值-左上角元素的累计值
if (b[i][j] == 2022) //如果该累计值等于2022
{
elenum = (i - m + 1) * (j - n + 1); //记录子矩阵中最少的元素
printf("子矩阵从(%d,%d)开始,到(%d,%d)结束,该矩阵中包含%d个元素\n", m, n, i, j, elenum);
if (elenum < minnum) minnum = elenum; //如果遇到更小的子矩阵,则替换记录
count++;
}
}
printf("和为2022的子矩阵共有:%d个\n", count);
printf("子矩阵中至少包含%d个元素", minnum);
}
运行结果
下图模拟了一下5×5矩阵中,和为54的部分子矩阵b[i][j],其中标红的数字为大循环每次开始计算的位置,而图中每一个5×5黄底方格中填入的数值,是由小循环每次计算得出的。空余位置是不需要用到的数值,但在代码中并没有每次都将它们值为空,而是保留了上一次的计算结果。
6. 核酸日期
问题描述:
如果周一做核酸,周二显示核酸天数为1天,周三显示2天,以此类推,周六显示5天,周日显示6天。
小蓝在某一天做了一次核酸,请问他的核酸显示为几天。已知做核酸和查看核酸不是在同一天,而且相差不超过6天(显示的数为1到6之间的数)。
#include<stdio.h>
int main()
{
int s, t;
printf("小蓝做核酸是周几? ");
scanf_s("%d", &s);
printf("小蓝查看核酸是周几? ");
scanf_s("%d", &t);
if (((t >= s) && (t - s) > 6) || ((t <= s) && (t - s + 7) > 6) || s < 1 || t > 7) printf("Error!");
else if (t > s) printf("相差%d天", t - s);
else printf("相差%d天", t - s + 7);
}
运行结果
7. 英文转换
问题描述:
输入一个由小写英文字母组成的字符串,请将其中的元音字母(a, e, i, o, u)转换成大写,其它字母仍然保持小写。
#include<stdio.h>
#include<string.h>
int main()
{
char str[100] = {"pneumonoultramicroscopicsilicovolcanoconiosis"};
int i;
printf("转换前的字符串为:");
for (i = 0; str[i]!='\0'; i++) printf("%c", str[i]);
for (i = 0; i < str[i] != '\0'; i++)
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u')
str[i] -= 32;
printf("\n转换后的字符串为:");
for (i = 0; str[i] != '\0'; i++) printf("%c", str[i]);
}