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

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

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

一、简答题

1. 简答题

书架(2023.12)

John最近买了一个书架用来存放奶牛养殖书籍,但书架很快被存满了,只剩最顶层有空余。

John共有N头奶牛(1 ≤ N ≤ 20,000),每头奶牛有自己的高度Hi(1 ≤ Hi ≤ 10,000),N头奶牛的总高度为S。书架高度为B(1 ≤ B ≤ S < 2,000,000,007).

为了到达书架顶层,奶牛可以踩着其他奶牛的背,像叠罗汉一样,直到他们的总高度不低于书架高度。当然若奶牛越多则危险性越大。为了帮助John到达书架顶层,找出使用奶牛数目最少的解决方案吧。

时间限制:10000

内存限制:65536

输入

第1行:空格隔开的整数N和B 第2~N+1行:第i+1行为整数Hi

输出

能达到书架高度所使用奶牛的最少数目


样例输入

6 40
6
18
11
13
19
11

样例输出

3
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">// 参考代码1 #include &lt;algorithm&gt; #include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cstring&gt; #include &lt;iostream&gt; #define INF 999999999 #define N 20001 using namespace std; int a[N]; void qsort(int x, int y) {   int i = x, j = y, mid = a[(x + y) / 2];   while (i &lt;= j) {     while (a[i] &gt; mid)       i++;     while (a[j] &lt; mid)       j--;     if (i &lt;= j) {       swap(a[i], a[j]);       i++;       j--;     }   }   if (x &lt; j)     qsort(x, j);   if (i &lt; y)     qsort(i, y); } int main() {   int n, b;   int sum = 0;   int i;   cin &gt;&gt; n &gt;&gt; b;   for (i = 1; i &lt;= n; i++)     cin &gt;&gt; a[i];   qsort(1, n);   for (i = 1; i &lt;= n; i++) {     sum += a[i];     if (sum &gt;= b)       break;   }   cout &lt;&lt; i &lt;&lt; endl;   ;   return 0; }</pre><pre class="brush:cpp;toolbar:false">// 参考代码2 #include &lt;bits/stdc++.h&gt; using namespace std; int n, b, a[20002]; int main() {   cin &gt;&gt; n &gt;&gt; b;   for (int i = 0; i &lt; n; i++)     cin &gt;&gt; a[i];   sort(a, a + n);   long long s = 0;   for (int i = n - 1; i &gt;= 0; i--) {     s += a[i];     if (s &gt;= b) {       cout &lt;&lt; n - i &lt;&lt; endl;       return 0;     }   }   return 0; }</pre>
这道题暂未补解析。
2. 简答题

抓牛(2023.12)

农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000)。农夫有两种移动方式:

1、从X移动到X-1或X+1,每次移动花费一分钟

2、从X移动到2*X,每次移动花费一分钟

假设牛没有意识到农夫的行动,站在原地不动。农夫最少要花多少时间才能抓住牛?

时间限制:2000

内存限制:65536

输入

两个整数,N和K

输出

一个整数,农夫抓到牛所要花费的最小分钟数


样例输入

5 17

样例输出

4
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">// 参考代码1 #include &lt;bits/stdc++.h&gt; using namespace std; int b[200100]; int a[10]; queue&lt;int&gt; q; int n, k; void bfs(int x) {   q.push(x);   b[x] = 1;   while (!q.empty()) {     int s = q.front();     a[1] = s - 1;     a[2] = s + 1;     a[3] = 2 * s;     for (int i = 1; i &lt; 4; i++) {       if (b[a[i]] == 0 &amp;&amp; a[i] &gt;= 0 &amp;&amp; a[i] &lt; 100100) {         b[a[i]] = b[s] + 1;         q.push(a[i]);         if (a[i] == k) {           cout &lt;&lt; b[k] - 1 &lt;&lt; endl;         }       }     }     q.pop();   } } int main() {   cin &gt;&gt; n &gt;&gt; k;   bfs(n);   return 0; }</pre><pre class="brush:cpp;toolbar:false">// 参考代码2 #include &lt;bits/stdc++.h&gt; using namespace std; int n, k; bool v[100005]; struct nod {   int x, dis; }; int main() {   cin &gt;&gt; n &gt;&gt; k;   queue&lt;nod&gt; q;   if (n == k) {     cout &lt;&lt; 0;     return 0;   }   v[n] = true;   q.push({n, 0});   while (!q.empty()) {     int l = q.size();     for (int i = 1; i &lt;= l; i++) {       if (q.front().x + 1 == k || q.front().x - 1 == k ||           q.front().x * 2 == k) {         cout &lt;&lt; q.front().dis + 1;         return 0;       }       if (q.front().x + 1 &gt;= 0 &amp;&amp; q.front().x + 1 &lt;= 100000 &amp;&amp;           !v[q.front().x + 1]) {         v[q.front().x + 1] = true;         q.push({q.front().x + 1, q.front().dis + 1});       }       if (q.front().x - 1 &gt;= 0 &amp;&amp; q.front().x - 1 &lt;= 100000 &amp;&amp;           !v[q.front().x - 1]) {         v[q.front().x - 1] = true;         q.push({q.front().x - 1, q.front().dis + 1});       }       if (q.front().x * 2 &gt;= 0 &amp;&amp; q.front().x * 2 &lt;= 100000 &amp;&amp;           !v[q.front().x * 2]) {         v[q.front().x * 2] = true;         q.push({q.front().x * 2, q.front().dis + 1});       }       q.pop();     }   }   return 0; }</pre>
这道题暂未补解析。
3. 简答题

鸣人和佐助(2023.12)

描述

佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢?

已知一张地图(以二维矩阵的形式表示)以及佐助和鸣人的位置。地图上的每个位置都可以走到,只不过有些位置上有大蛇丸的手下,需要先打败大蛇丸的手下才能到这些位置。鸣人有一定数量的查克拉,每一个单位的查克拉可以打败一个大蛇丸的手下。假设鸣人可以往上下左右四个方向移动,每移动一个距离需要花费1个单位时间,打败大蛇丸的手下不需要时间。如果鸣人查克拉消耗完了,则只可以走到没有大蛇丸手下的位置,不可以再移动到有大蛇丸手下的位置。佐助在此期间不移动,大蛇丸的手下也不移动。请问,鸣人要追上佐助最少需要花费多少时间?

输入

输入的第一行包含三个整数:M,N,T。代表M行N列的地图和鸣人初始的查克拉数量T。0 < M,N < 200,0 ≤ T < 10

后面是M行N列的地图,其中@代表鸣人,+代表佐助。*代表通路,#代表大蛇丸的手下。

输出

输出包含一个整数R,代表鸣人追上佐助最少需要花费的时间。如果鸣人无法追上佐助,则输出-1。

 

样例输入1

4 4 1
#@##
**##
###+
****


样例输入2

4 4 2
#@##
**##
###+
****

 

样例输出1

6


样例输出2

4
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">#include &lt;bits/stdc++.h&gt; using namespace std; int n, m, t, s2[10][256][256], fx[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; char s1[256][256]; struct AAA {   int i;   int j;   int bs;   int st; }; int bfs() {   int i, j, k, ti, tj, ret = -1;   queue&lt;AAA&gt; dl;   AAA tmp, tmp1;   for (i = 0; i &lt; m; i++) {     for (j = 0; j &lt; n; j++)       if (s1[i][j] == '@') {         tmp.i = i;         tmp.j = j;         tmp.bs = 0;         tmp.st = t;         break;       }     if (j &lt; n)       break;   }   dl.push(tmp);   for (i = 0; i &lt; 10; i++)     for (j = 0; j &lt; m; j++)       for (k = 0; k &lt; n; k++)         s2[i][j][k] = 0;   s2[0][tmp.i][tmp.j] = 1;   while (!dl.empty()) {     tmp = dl.front();     dl.pop();     if (s1[tmp.i][tmp.j] == '+') {       ret = tmp.bs;       break;     }     for (i = 0; i &lt; 4; i++) {       ti = tmp.i + fx[i][0];       tj = tmp.j + fx[i][1];       if ((ti &lt; 0) || (ti &gt;= m) || (tj &lt; 0) || (tj &gt;= n))         continue;       if (s2[t - tmp.st][ti][tj] == 1)         continue;       if ((s1[ti][tj] == '#') &amp;&amp; (tmp.st == 0))         continue;       s2[t - tmp.st][ti][tj] = 1;       tmp1.i = ti;       tmp1.j = tj;       tmp1.bs = tmp.bs + 1;       tmp1.st = tmp.st;       if (s1[ti][tj] == '#')         tmp1.st--;       dl.push(tmp1);     }   }   return ret; } int main() {   int i, j, k;   char tmp[16];   gets(tmp);   sscanf(tmp, "%d%d%d", &amp;m, &amp;n, &amp;t);   for (i = 0; i &lt; m; i++)     gets(s1[i]);   printf("%d", bfs());   return 0; }</pre><p><br/></p><pre class="brush:cpp;toolbar:false">//参考代码2 #include&lt;stdio.h&gt; #include&lt;algorithm&gt; #include&lt;string.h&gt; using namespace std; const int inf=0x3f3f3f3f; int n,m,sx,sy,st,minstep=inf; int step[201][201][11],dx[]={0,1,-1,0},dy[]={1,0,0,-1}; char mp[201][201]; bool vis[201][201]; void dfs(int x,int y,int t,int stp) {     if(x&lt;1 || x&gt;n || y&lt;1 || y&gt;m || vis[x][y]) return ;     if(t&lt;0) return ;     if(stp&gt;=minstep) return ;     if(stp&gt;=step[x][y][t]) return ;     step[x][y][t]=stp;          if(mp[x][y]=='#') t--;     else if(mp[x][y]=='+') {         if(stp&lt;minstep) minstep=stp;         return ;     }          vis[x][y]=1;     for(int i=0;i&lt;4;++i){         int nx=x+dx[i];         int ny=y+dy[i];         dfs(nx,ny,t,stp+1);     }     vis[x][y]=0; } int main() {     scanf("%d%d%d",&amp;n,&amp;m,&amp;st);     getchar();     for(int i=1;i&lt;=n;++i){         for(int j=1;j&lt;=m;++j){             scanf("%c",&amp;mp[i][j]);             if(mp[i][j]=='@') {                 sx=i;                 sy=j;             }         }         getchar();     }     memset(step,0x3f,sizeof(step));     dfs(sx,sy,st,0);     if(minstep==inf) printf("-1");     else printf("%d",minstep);     return 0; }</pre><pre class="brush:cpp;toolbar:false">//参考代码3 #include&lt;cstdio&gt; #include&lt;queue&gt; using namespace std; struct POINT {     int x,y,tot,T; } point; queue&lt;POINT&gt; que; int main() {     bool walked[205][205][15]={};     int r,c,T,F[4][2]={{-1,0},{1,0},{0,-1},{0,1}};     scanf("%d%d%d",&amp;r,&amp;c,&amp;T);     char maze[205][205]={};     for(int i=0;i&lt;r;i++)     {         scanf("%s",maze[i]);         for(int j=0;j&lt;c;j++)             if(maze[i][j]=='@')             {                 point.x=i;point.y=j;                 point.tot=0;point.T=T;             }     }     que.push(point);     while(!que.empty())     {         for(int i=0;i&lt;4;i++)         {             point.x=que.front().x+F[i][0];             point.y=que.front().y+F[i][1];             point.tot=que.front().tot+1;             point.T=que.front().T;             if(point.x&lt;0 || point.x&gt;=r || point.y&lt;0 || point.y&gt;=c || walked[point.x][point.y][point.T]) continue;             if(maze[point.x][point.y]=='#')                 if(point.T) point.T--;                 else continue;             if(maze[point.x][point.y]=='+') {printf("%d\n",point.tot);return 0;}             walked[point.x][point.y][point.T]=true;             que.push(point);         }         que.pop();     }     printf("-1\n");     return 0; }</pre><pre class="brush:cpp;toolbar:false">// 参考代码4 #include &lt;bits/stdc++.h&gt; using namespace std; char a[202][202]; int v_ckl[202][202]; // 走到该坐标点时的最小消耗的查克拉 struct nod {   int x, y, ckl, step; }; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; int main() {   int n, m, k, mi, mj;   cin &gt;&gt; m &gt;&gt; n &gt;&gt; k;   for (int i = 1; i &lt;= m; i++)     for (int j = 1; j &lt;= n; j++) {       cin &gt;&gt; a[i][j];       if (a[i][j] == '@') {         mi = i;         mj = j;       }     }   queue&lt;nod&gt; q;   q.push({mi, mj, k, 0});   v_ckl[mi][mj] = k;   memset(v_ckl, -1,          sizeof(v_ckl)); // 注意:初始值 查克拉为零时条件允许也可以走的   while (!q.empty()) {     nod t = q.front();     q.pop();     for (int i = 0; i &lt; 4; i++) {       if (t.x + dx[i] &lt; 1 || t.x + dx[i] &gt; m || t.y + dy[i] &lt; 1 ||           t.y + dy[i] &gt; n)         continue;       else if (a[t.x + dx[i]][t.y + dy[i]] == '+') {         cout &lt;&lt; t.step + 1;         return 0;       } // 注意:比较查克拉值 好判断该点是否需要更新       else if (a[t.x + dx[i]][t.y + dy[i]] == '*' &amp;&amp;                t.ckl &gt; v_ckl[t.x + dx[i]][t.y + dy[i]]) {         q.push({t.x + dx[i], t.y + dy[i], t.ckl, t.step + 1});         v_ckl[t.x + dx[i]][t.y + dy[i]] = t.ckl;       } else if (a[t.x + dx[i]][t.y + dy[i]] == '#' &amp;&amp; t.ckl &gt; 0 &amp;&amp;                  t.ckl &gt; v_ckl[t.x + dx[i]][t.y + dy[i]]) {         q.push({t.x + dx[i], t.y + dy[i], t.ckl - 1, t.step + 1});         v_ckl[t.x + dx[i]][t.y + dy[i]] = t.ckl - 1;       }     }   }   cout &lt;&lt; "-1";   return 0; }</pre>
这道题暂未补解析。
4. 简答题

献给阿尔吉侬的花束

阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫。今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪。现在研究员们想知道,如果阿尔吉侬足够聪明,它最少需要多少时间就能吃到奶酪。

迷宫用一个R×C的字符矩阵来表示。字符S表示阿尔吉侬所在的位置,字符E表示奶酪所在的位置,字符#表示墙壁,字符.表示可以通行。阿尔吉侬在1个单位时间内可以从当前的位置走到它上下左右四个方向上的任意一个位置,但不能走出地图边界。

时间限制:100

内存限制:65536

输入

第一行是一个正整数T(1 <= T <= 10),表示一共有T组数据。 每一组数据的第一行包含了两个用空格分开的正整数R和C(2 <= R, C <= 200),表示地图是一个R×C的矩阵。 接下来的R行描述了地图的具体内容,每一行包含了C个字符。字符含义如题目描述中所述。保证有且仅有一个S和E。

输出

对于每一组数据,输出阿尔吉侬吃到奶酪的最少单位时间。若阿尔吉侬无法吃到奶酪,则输出“oop!”(只输出引号里面的内容,不输出引号)。每组数据的输出结果占一行。

样例输入

3
3 4
.S..
###.
..E.
3 4
.S..
.E..
....
3 4
.S..
####
..E.

样例输出

5
1
oop!
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">#include &lt;bits/stdc++.h&gt; using namespace std; char a[202][202]; struct nod {   int x, y, step; }; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; int main() {   int t, r, c, mi, mj;   cin &gt;&gt; t;   while (t--) {     cin &gt;&gt; r &gt;&gt; c;     memset(a, 0, sizeof(a));     for (int i = 1; i &lt;= r; i++)       for (int j = 1; j &lt;= c; j++) {         cin &gt;&gt; a[i][j];         if (a[i][j] == 'S') {           mi = i;           mj = j;         }       }     queue&lt;nod&gt; q;     q.push({mi, mj, 0});     bool is_t = false;     while (!q.empty()) {       nod t = q.front();       q.pop();       for (int i = 0; i &lt; 4; i++) {         if (t.x + dx[i] &lt; 1 || t.x + dx[i] &gt; r || t.y + dy[i] &lt; 1 ||             t.y + dy[i] &gt; c)           continue;         else if (a[t.x + dx[i]][t.y + dy[i]] == '#')           continue;         else if (a[t.x + dx[i]][t.y + dy[i]] == 'E') {           cout &lt;&lt; t.step + 1 &lt;&lt; endl;           is_t = true;           break;         } else if (a[t.x + dx[i]][t.y + dy[i]] == '.') {           q.push({t.x + dx[i], t.y + dy[i], t.step + 1});           a[t.x + dx[i]][t.y + dy[i]] = '#';         }       }       if (is_t)         break;     }     if (!is_t)       cout &lt;&lt; "oop!" &lt;&lt; endl;   }   return 0; }</pre>
这道题暂未补解析。