首页 › 试卷 › C/C++编程考级 › C/C++编程七级 › 青少年软件编程(C/C++)7级等级考试真题试卷(2024年3月)

青少年软件编程(C/C++)7级等级考试真题试卷(2024年3月) VIP

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

一、简答题

1. 简答题

走出迷宫(2024-3-7级)

当你站在一个迷宫里的时候,往往会被错综复杂的道路弄得失去方向感,如果你能得到迷宫地图,事情就会变得非常简单。

假设你已经得到了一个n*m的迷宫的图纸,请你找出从起点到出口的最短路。

时间限制:1000

内存限制:65536

输入

第一行是两个整数n和m(1<=n,m<=100),表示迷宫的行数和列数。 接下来n行,每行一个长为m的字符串,表示整个迷宫的布局。字符'.'表示空地,'#'表示墙,'S'表示起点,'T'表示出口。

输出

输出从起点到出口最少需要走的步数。


样例输入

3 3
S#T
.#.
...

样例输出

6
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">#include &lt;bits/stdc++.h&gt; using namespace std; int n, m, p[10008], q[10008], ans[108][108], qx, qy, zx, zy; char a[108][108]; int xx[5] = {0, 1, -1, 0, 0}; int yy[5] = {0, 0, 0, 1, -1}; void bfs() {   a[qx][qy] = '#';   ans[qx][qy] = 0;   int head = 1;   int tail = 1;   p[1] = qx;   q[1] = qy;   while (head &lt;= tail) {     for (int i = 1; i &lt;= 4; i++) {       int h = p[head] + xx[i];       int l = q[head] + yy[i];       if (a[h][l] != '#' &amp;&amp; h &gt;= 1 &amp;&amp; l &gt;= 1 &amp;&amp; h &lt;= n &amp;&amp; l &lt;= m) {         a[h][l] = '#';         tail++;         p[tail] = h;         q[tail] = l;         ans[h][l] = ans[p[head]][q[head]] + 1;         if (h == zx &amp;&amp; l == zy) {           cout &lt;&lt; ans[h][l] &lt;&lt; endl;           return;         }       }     }     head++;   } } int main() {   cin &gt;&gt; n &gt;&gt; m;   for (int i = 1; i &lt;= n; i++) {     for (int j = 1; j &lt;= m; j++) {       cin &gt;&gt; a[i][j];       if (a[i][j] == 'S') {         qx = i;         qy = j;       }       if (a[i][j] == 'T') {         zx = i;         zy = j;       }     }   }   bfs();   return 0; }</pre>
这道题暂未补解析。
2. 简答题

有多少种二叉树(2024-3-7级)

输入n(1<n<13),求n个结点的二叉树有多少种形态

时间限制:1000

内存限制:65536

输入

整数n

输出

答案


样例输入

3

样例输出

5
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">#include &lt;iostream&gt; using namespace std; long long cat(int n) {   if (n == 0)     return 1;   long long sum(0);   for (int i = 0; i &lt; n; i++)     sum += cat(i) * cat(n - 1 - i);   return sum; } int main() {   int n;   cin &gt;&gt; n;   cout &lt;&lt; cat(n);   return 0; }</pre>
这道题暂未补解析。
3. 简答题

迷宫问题(2024-3-7级)

定义一个二维数组:

int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,

};

它表示一个迷宫, 其中的 1 表示墙壁, 0 表示可以走的路, 只能横着走或竖着走, 不能斜着走, 要求编程序找出从左上角到右下角的最短路线。

时间限制: 1000

内存限制: 65536

输入

一个 5 × 5 的二维数组, 表示一个迷宫。 数据保证有唯一解。

输出

左上角到右下角的最短路径, 格式如样例所示。


样例输入

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

样例输出

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">/*#include &lt;iostream&gt;*/ #include &lt;bits/stdc++.h&gt; using namespace std; const int idata = 50 + 5; int maps[idata][idata]; bool judge[idata][idata]; int change[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int nx, ny; int x[idata], y[idata]; int show[30]; bool flag; void print(int tail) {   if (show[tail])     print(show[tail]);   cout &lt;&lt; "(" &lt;&lt; x[tail] &lt;&lt; ", " &lt;&lt; y[tail] &lt;&lt; ")" &lt;&lt; endl; } int main() {   int head, tail;   register int i, j;   for (i = 0; i &lt; 5; i++) {     for (j = 0; j &lt; 5; j++) {       cin &gt;&gt; maps[i][j];       if (maps[i][j] == 1)         judge[i][j] = 1;     }   }   head = 0;   tail = 1;   judge[0][0] = 1;   show[tail] = head;   while (tail &gt; head) {     head++;     for (i = 0; i &lt; 4; i++) {       nx = x[head] + change[i][0];       ny = y[head] + change[i][1];       if (nx &gt;= 0 &amp;&amp; nx &lt; 5 &amp;&amp; ny &gt;= 0 &amp;&amp; ny &lt; 5 &amp;&amp; !judge[nx][ny]) {         tail++;         x[tail] = nx;         y[tail] = ny;         judge[nx][ny] = 1;         show[tail] = head;         if (nx == 4 &amp;&amp; ny == 4) {           flag = 1;           print(tail);           break;         }       }     }     if (flag)       break;   }   return 0; }</pre><p></p>
这道题暂未补解析。
4. 简答题

快速堆猪(2024-3-7级)

小明有很多猪,他喜欢玩叠猪游戏,就是将猪一头头叠起来。猪叠上去后,还可以把顶上的猪拿下来。小明知道每头猪的重量,而且他还随时想知道叠在那里的猪最轻的是多少斤。

时间限制:1000

内存限制:65536

输入

有三种输入 1)push n n是整数(0<=0 <=20000),表示叠上一头重量是n斤的新猪 2)pop 表示将猪堆顶的猪赶走。如果猪堆没猪,就啥也不干 3)min 表示问现在猪堆里最轻的猪多重。如果猪堆没猪,就啥也不干 输入总数不超过100000条

输出

对每个min输入,输出答案。如果猪堆没猪,就啥也不干


样例输入

pop
min
push 5
push 2
push 3
min
push 4
min

样例输出

2
2
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">#include &lt;iostream&gt; using namespace std; typedef long long LL; int h[2005]; int size_; void up(int u){ while(u/2&amp;&amp;h[u/2]&gt;h[u]){ swap(h[u/2],h[u]); u/=2; } } void down(int u){ int t=u; if(u*2&lt;=size_&amp;&amp;h[u*2]&lt;h[u]) t=u*2; if(u*2+1&lt;=size_&amp;&amp;h[u*2+1]&lt;h[u]) t=u*2+1; if(u!=t){ swap(h[u],h[t]);down(t); } } int main() { char t[10]; string s; int n; while(scanf("%s",t)!=EOF){ s=string(t); if(s=="pop"){ if(size_==0) continue; h[1]=h[size_]; size_--; down(1); } else if(s=="min"){ if(size_==0) continue; cout&lt;&lt;h[1]&lt;&lt;endl; } else if(s=="push"){ cin&gt;&gt;n; size_++; h[size_]=n; up(size_); } } return 0; }</pre><p><br/></p>
这道题暂未补解析。