首页 › 试卷 › C/C++编程考级 › C/C++编程二级 › 青少年软件编程(C/C++)2级等级考试真题试卷(2023年12月)

青少年软件编程(C/C++)2级等级考试真题试卷(2023年12月) VIP

C/C++编程二级 共 5 题 2023 年 含标准答案与解析
返回试卷列表
本页为整卷浏览:先自己做一遍,再展开「查看答案与解析」核对。

一、简答题

1. 简答题

统计指定范围里的数(2023.12)

给定一个数的序列S,以及一个区间[L, R], 求序列中介于该区间的数的个数,即序列中大于等于L且小于等于R的数的个数。

时间限制:1000

内存限制:65536

输入

第一行1个整数n、,分别表示序列的长度。(0 < n ≤ 10000) 第二行n个正整数,表示序列里的每一个数,每个数小于等于10000000 第三行2个整数L、R,分别表示区间的左端点,区间的右端点。(1 ≤ L ≤ R ≤ 1000000)

输出

输出一个整数,表示序列中大于等于L且小于等于R的数的个数。

样例输入

5
11 8 1 10 16
1 10

样例输出

3
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">// C++参考代码1 #include &lt;bits/stdc++.h&gt; using namespace std; int a[10005]; int main() { int n,l,r,ans=0; cin&gt;&gt;n; for(int i=1;i&lt;=n;i++)cin&gt;&gt;a[i]; cin&gt;&gt;l&gt;&gt;r; for(int i=1,x;i&lt;=n;i++){ if(a[i]&gt;=l&amp;&amp;a[i]&lt;=r) ans++; } cout&lt;&lt;ans;     return 0; }</pre><pre class="brush:cpp;toolbar:false">// C++参考代码2 #include &lt;iostream&gt; #include &lt;vector&gt; int countNumbersInRange(const std::vector&lt;int&gt;&amp; sequence, int L, int R) {     int count = 0;     for (int num : sequence) {         if (num &gt;= L &amp;&amp; num &lt;= R) {             count++;         }     }     return count; } int main() {     int n;     std::cin &gt;&gt; n;          std::vector&lt;int&gt; sequence(n);     for (int i = 0; i &lt; n; i++) {         std::cin &gt;&gt; sequence[i];     }          int L, R;     std::cin &gt;&gt; L &gt;&gt; R;          int result = countNumbersInRange(sequence, L, R);     std::cout &lt;&lt; result &lt;&lt; std::endl;          return 0; }</pre><pre class="brush:cpp;toolbar:false">// C语言参考代码 #include &lt;stdio.h&gt; int main() {     int n, L, R;     scanf("%d", &amp;n);     int sequence[n];     for (int i = 0; i &lt; n; i++) {         scanf("%d", &amp;sequence[i]);     }     scanf("%d %d", &amp;L, &amp;R);     int count = 0;     for (int i = 0; i &lt; n; i++) {         if (sequence[i] &gt;= L &amp;&amp; sequence[i] &lt;= R) {             count++;         }     }     printf("%d\n", count);     return 0; }</pre>
这道题暂未补解析。
2. 简答题

细菌的繁殖与扩散(2023.12)

在边长为9的正方形培养皿中,正中心位置有m个细菌。假设细菌的寿命仅一天,但每天可繁殖10个后代,而且这10个后代,有两个分布在原来的单元格中,其余的均匀分布在其四周相邻的八个单元格中。求经过n(1≤n≤4)天后,细菌在培养皿中的分布情况。

时间限制:10000

内存限制:65536

输入

输入为两个整数,第一个整数m表示中心位置细菌的个数(2 ≤ m ≤ 30),第二个整数n表示经过的天数(1 ≤ n ≤ 4)。

输出

输出九行九列整数矩阵,每行的整数之间用空格分隔。整个矩阵代表n天后细菌在培养皿上的分布情况。

样例输入

2 1

样例输出

0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 2 2 2 0 0 0
0 0 0 2 4 2 0 0 0
0 0 0 2 2 2 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">// C++参考代码1 // 数据量比较小 暴力模拟即可 #include &lt;bits/stdc++.h&gt; using namespace std; int a[10][10], b[10][10]; int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; int main() {   int n, m;   cin &gt;&gt; m &gt;&gt; n;   a[5][5] = m;   for (int k = 1; k &lt;= n; k++) { // n天     memset(b, 0, sizeof(b));     // 清空b     for (int i = 1; i &lt;= 9; i++)       for (int j = 1; j &lt;= 9; j++) {         if (a[i][j] &gt; 0) {           b[i][j] += a[i][j] * 2;       // 原单元格           for (int t = 0; t &lt; 8; t++) { // 向四周8个单元格扩散             if (i + dx[t] &gt;= 1 &amp;&amp; i + dx[t] &lt;= 9 &amp;&amp; j + dy[t] &gt;= 1 &amp;&amp;                 j + dy[t] &lt;= 9)               b[i + dx[t]][j + dy[t]] += a[i][j];           }         }       }     memcpy(a, b, sizeof(b));   }   // 输出   for (int i = 1; i &lt;= 9; i++) {     for (int j = 1; j &lt;= 9; j++)       cout &lt;&lt; a[i][j] &lt;&lt; " ";     cout &lt;&lt; endl;   }   return 0; }</pre><pre class="brush:cpp;toolbar:false">// C++参考代码2 #include &lt;iostream&gt; int main() {     int m, n, a[10][10] = {0}, b[10][10] = {0};     std::cin &gt;&gt; m &gt;&gt; n;     a[5][5] = m;     for (int k = 1; k &lt;= n; k++) {         for (int i = 1; i &lt;= 9; i++) {             for (int j = 1; j &lt;= 9; j++) {                 if (a[i][j] != 0) {                     for (int x = i - 1; x &lt;= i + 1; x++) {                         for (int y = j - 1; y &lt;= j + 1; y++) {                             if (x == i &amp;&amp; y == j) {                                 b[x][y] += 2 * a[i][j];                             } else {                                 b[x][y] += a[i][j];                             }                         }                     }                 }             }         }         for (int i = 1; i &lt;= 9; i++) {             for (int j = 1; j &lt;= 9; j++) {                 a[i][j] = b[i][j];                 b[i][j] = 0;             }         }     }     for (int i = 1; i &lt;= 9; i++) {         for (int j = 1; j &lt;= 9; j++) {             std::cout &lt;&lt; a[i][j] &lt;&lt; " ";         }         std::cout &lt;&lt; std::endl;     }     return 0; }</pre><pre class="brush:cpp;toolbar:false">// C语言参考代码 #include &lt;stdio.h&gt; int main() {     int m, n, a[10][10] = {0}, b[10][10] = {0};     scanf("%d %d", &amp;m, &amp;n);     a[5][5] = m;     for (int k = 1; k &lt;= n; k++) {         for (int i = 1; i &lt;= 9; i++) {             for (int j = 1; j &lt;= 9; j++) {                 if (a[i][j] != 0) {                     for (int x = i - 1; x &lt;= i + 1; x++) {                         for (int y = j - 1; y &lt;= j + 1; y++) {                             if (x == i &amp;&amp; y == j) {                                 b[x][y] = b[x][y] + 2 * a[i][j];                             } else {                                 b[x][y] = b[x][y] + a[i][j];                             }                         }                     }                 }             }         }         for (int i = 1; i &lt;= 9; i++) {             for (int j = 1; j &lt;= 9; j++) {                 a[i][j] = b[i][j];                 b[i][j] = 0;             }         }     }     for (int i = 1; i &lt;= 9; i++) {         for (int j = 1; j &lt;= 9; j++) {             printf("%d ", a[i][j]);         }         printf("\n");     }     return 0; }</pre>
这道题暂未补解析。
3. 简答题

错误探测(2023.12)

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

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

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

时间限制:1000

内存限制:65536

输入

输入n + 1行,第1行为矩阵的大小n(0 < n < 100),以下n行为矩阵的每一行的元素,元素之间以一个空格分开。

输出

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

样例输入

样例输入1

4
1 0 1 0
0 0 0 0
1 1 1 1
0 1 0 1

样例输入2

4
1 0 1 0
0 0 1 0
1 1 1 1
0 1 0 1

样例输入3

4
1 0 1 0
0 1 1 0
1 1 1 1
0 1 0 1

样例输出

样例输出1

OK

样例输出2

2 3

样例输出3

Corrupt
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">// C++参考代码1 #include &lt;bits/stdc++.h&gt; using namespace std; int a[105][105],row[105],col[105]; int main() { int n; cin&gt;&gt;n; for(int i=1;i&lt;=n;i++) for(int j=1;j&lt;=n;j++){ cin&gt;&gt;a[i][j]; if(a[i][j]==1){ row[i]++;//统计i行有几个1 col[j]++;//统计j列有几个1 } } int x=0,xi=0,y=0,yi=0; for(int i=1;i&lt;=n;i++){ if(row[i]%2!=0) x++,xi=i;//i行1的数量不是偶数 x+=1 if(col[i]%2!=0) y++,yi=i;//i列1的数量不是偶数 y+=1 } if(x==0&amp;&amp;y==0) cout&lt;&lt;"OK"; else if(x==1&amp;&amp;y==1) cout&lt;&lt;xi&lt;&lt;" "&lt;&lt;yi; else cout&lt;&lt;"Corrupt";     return 0; }</pre><pre class="brush:cpp;toolbar:false">// C++参考代码2 #include &lt;iostream&gt; int main() {     int n;     std::cin &gt;&gt; n;     int a[110][110];     int i, j;          // 输入元素     for (i = 0; i &lt; n; i++) {         for (j = 0; j &lt; n; j++) {             std::cin &gt;&gt; a[i][j];         }     }          int h[110], l[110], sumh = 0, suml = 0;          // 遍历     for (i = 0; i &lt; n; i++) {         sumh = 0, suml = 0;         for (j = 0; j &lt; n; j++) {             if (a[i][j] == 1) {                 sumh++;             }             if (a[j][i] == 1) {                 suml++;             }         }                  // 判断该行、列的1的数量         if (sumh % 2 == 0) {             h[i] = 1;         } else {             h[i] = 0;         }         if (suml % 2 == 0) {             l[i] = 1;         } else {             l[i] = 0;         }     }       sumh = 0, suml = 0;     int I = -1, J = -1; // 记录几行几列          // 遍历h, l数组     for (i = 0; i &lt; n; i++) {         if (h[i] == 0) {             sumh++;             I = i + 1;         }         if (l[i] == 0) {             suml++;             J = i + 1;         }     }          if (sumh == 0 &amp;&amp; suml == 0) {         std::cout &lt;&lt; "OK";     } else if (sumh == 1 &amp;&amp; suml == 1) {         std::cout &lt;&lt; I &lt;&lt; " " &lt;&lt; J;     } else {         std::cout &lt;&lt; "Corrupt";     }          return 0; }</pre><pre class="brush:cpp;toolbar:false">// C语言参考代码 #include &lt;stdio.h&gt; int main() {     int n;     scanf("%d", &amp;n);     int a[110][110];     int i, j;          // 输入元素     for (i = 0; i &lt; n; i++) {         for (j = 0; j &lt; n; j++) {             scanf("%d", &amp;a[i][j]);         }     }          int h[110], l[110], sumh = 0, suml = 0;          // 遍历     for (i = 0; i &lt; n; i++) {         sumh = 0, suml = 0;         for (j = 0; j &lt; n; j++) {             if (a[i][j] == 1) {                 sumh++;             }             if (a[j][i] == 1) {                 suml++;             }         }                  // 判断该行、列的1的数量         if (sumh % 2 == 0) {             h[i] = 1;         } else {             h[i] = 0;         }         if (suml % 2 == 0) {             l[i] = 1;         } else {             l[i] = 0;         }     }       sumh = 0, suml = 0;     int I, J; // 记录几行几列          // 遍历h, l数组     for (i = 0; i &lt; n; i++) {         if (h[i] == 0) {             sumh++;             I = i + 1;         }         if (l[i] == 0) {             suml++;             J = i + 1;         }     }          if (sumh == 0 &amp;&amp; suml == 0) {         printf("OK");     } else if (sumh == 1 &amp;&amp; suml == 1) {         printf("%d %d", I, J);     } else {         printf("Corrupt");     }          return 0; }</pre><p><br/></p>
这道题暂未补解析。
4. 简答题

单词排序(2023.12)

输入一行单词序列,相邻单词之间由 1 个或多个空格间隔,请按照字典序输出这些单词,要求重复的单词只输出一次。(区分大小写)

输入

一行单词序列,最少 1 个单词,最多 100 个单词,每个单词长度不超过 50,单词之间用至少 1 个空格间隔。数据不含除字母、空格外的其他字符。

输出

按字典序输出这些单词,重复的单词只输出一次。


输入样例

She wants to go to Peking University to study Chinese

输出样例

Chinese
Peking
She
University
go
study
to
wants
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">// C++参考代码1 #include &lt;bits/stdc++.h&gt; using namespace std; string s[105],x; int main() { int i=0; while(cin&gt;&gt;x){ s[i++]=x; } sort(s,s+i); cout&lt;&lt;s[0]&lt;&lt;endl; for(int j=1;j&lt;i;j++){ if(s[j]!=s[j-1]) cout&lt;&lt;s[j]&lt;&lt;endl; }     return 0; }</pre><pre class="brush:cpp;toolbar:false">// C++参考代码2 #include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;set&gt; #include &lt;string&gt; int main() {     std::string input;     std::getline(std::cin, input);  // 读取一行输入          std::set&lt;std::string&gt; words;  // 使用集合存储单词,确保输出时没有重复的单词          std::stringstream ss(input);  // 使用字符串流处理输入          std::string word;     while (ss &gt;&gt; word) {         words.insert(word);  // 将单词插入集合中     }          // 遍历集合,按字典序输出单词     for (const std::string&amp; w : words) {         std::cout &lt;&lt; w &lt;&lt; std::endl;     }          return 0; }</pre><pre class="brush:cpp;toolbar:false">// C语言参考代码 #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; // 比较函数,用于排序 int compare(const void* a, const void* b) {     return strcmp(*(const char**)a, *(const char**)b); } int main() {     char words[100][50];     char* unique_words[100];     int num_words = 0;          // 读取输入的单词序列     char input[10000];     fgets(input, sizeof(input), stdin);          // 解析单词     char* token = strtok(input, " ");     while (token != NULL) {         strcpy(words[num_words], token);         num_words++;         token = strtok(NULL, " ");     }          // 去除重复单词,保存到unique_words数组     int unique_count = 0;     for (int i = 0; i &lt; num_words; i++) {         int is_duplicate = 0;         for (int j = 0; j &lt; unique_count; j++) {             if (strcmp(words[i], unique_words[j]) == 0) {                 is_duplicate = 1;                 break;             }         }         if (!is_duplicate) {             unique_words[unique_count] = words[i];             unique_count++;         }     }          // 按字典序排序     qsort(unique_words, unique_count, sizeof(char*), compare);          // 输出结果     for (int i = 0; i &lt; unique_count; i++) {         printf("%s\n", unique_words[i]);     }          return 0; }</pre>
这道题暂未补解析。
5. 简答题

计算好数(2023.12)

若将一个正整数化为二进制数,在此二进制数中,我们将数字1的个数多于数字0的个数的这类二进制数称为好数。

例如:

(13)10 = (1101)2,其中1的个数为3,0的个数为1,则此数是好数;

(10)10 = (1010)2,其中1的个数为2,0的个数也为2,则此数不是好数;

(24)10 = (11000)2,其中1的个数为2,0的个数为3,则此数不是好数;

对于给定的N,写程序求出1~N之中(包括1与N)中的好数个数。

时间限制:1000

内存限制:65536

输入

一个整数,题目中的N(N ≤ 1000)

输出

一个整数,表示1~N之中(包括1与N)中的好数个数


样例输入

10

样例输出

5
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">// C++参考代码1 #include &lt;bits/stdc++.h&gt; using namespace std; bool is_good(int n){ int a=0,b=0; while(n){ if(n&amp;1) a++; else b++; n&gt;&gt;=1; } return a&gt;b? true:false; } int main() { int n,ans=0; cin&gt;&gt;n; for(int i=1;i&lt;=n;i++) if(is_good(i)) ans++; cout&lt;&lt;ans;     return 0; }</pre><pre class="brush:cpp;toolbar:false">// C++参考代码2 #include &lt;iostream&gt; int main() {     int j;     std::cin &gt;&gt; j;     int t;     int s = 0, c = 0, w = 0;     for (int n = 1; n &lt;= j; n++) {         for (int i = n; i; i /= 2) {             t = i % 2;             if (t == 1) {                 s++;             } else {                 c++;             }         }         if (s &gt; c) {             w++;         }         s = 0;         c = 0;     }     std::cout &lt;&lt; w;     return 0; }</pre><pre class="brush:cpp;toolbar:false">// C语言参考代码 #include &lt;stdio.h&gt; int main() {     int j;     scanf("%d", &amp;j);     int t;     int s = 0, c = 0, w = 0;     for (int n = 1; n &lt;= j; n++) {         for (int i = n; i; i /= 2) {             t = i % 2;             if (t == 1) {                 s++;             } else {                 c++;             }         }         if (s &gt; c) {             w++;         }         s = 0;         c = 0;     }     printf("%d", w);     return 0; }</pre>
这道题暂未补解析。