首页 › 试卷 › C/C++编程考级 › C/C++编程三级 › 青少年软件编程(C语言三级)等级考试试卷(2020年9月)

青少年软件编程(C语言三级)等级考试试卷(2020年9月) VIP

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

一、简答题

1. 简答题

铺砖

对于一个2行N列的走道。现在用1*2,2*2的砖去铺满。问有多少种不同的方式。

时间限制:3000

内存限制:131072

输入

整个测试有多组数据,请做到文件底结束。每行给出一个数字N,0 <= n <= 250

输出

如题

样例输入

2
8
12
100
200

样例输出

3
171
2731
845100400152152934331135470251
1071292029505993517027974728227441735014801995855195223534251
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">#include &lt;algorithm&gt; #include &lt;cstdio&gt; #include &lt;cstring&gt; #include &lt;iostream&gt; using namespace std; int a[1005][255] = {{0, 0}, {0, 1}, {0, 3}, {0, 5}}; int b[1005], c[1005], len[255] = {0, 1, 1, 1}, n; int main() {   for (int l = 4; l &lt;= 255; l++) {     memset(b, 0, sizeof(b));     memset(c, 0, sizeof(c));     int len3;     for (int i = len[l - 2]; i &gt;= 1; i--)       b[i] = a[l - 2][i];     for (int i = 1; i &lt;= len[l - 2]; i++) {       c[i] += b[i] * 2;       c[i + 1] = c[i] / 10;       c[i] %= 10;     }     len3 = len[l - 2] + 1;     while (c[len3] == 0 &amp;&amp; len3 &gt; 1)       len3--;     int x = 0;     len3 = 1;     while (len3 &lt;= len[l - 1] || len3 &lt;= len[l - 2]) {       a[l][len3] += a[l - 1][len3] + c[len3];       a[l][len3 + 1] = a[l][len3] / 10;       a[l][len3] %= 10;       len3++;     }     while (a[l][len3] == 0 &amp;&amp; len3 &gt; 1)       len3--;     len[l] = len3;   }   while (scanf("%d", &amp;n) != EOF) {     if (n == 0) {       cout &lt;&lt; "1" &lt;&lt; endl;       continue;     }     for (int i = len[n]; i &gt;= 1; i--)       cout &lt;&lt; a[n][i];     cout &lt;&lt; endl;   }   return 0; }</pre>
这道题暂未补解析。
2. 简答题

吃糖果2

现有n(50 > n > 0)个糖果,每天只能吃2个或者3个,请计算共有多少种不同的吃法吃完糖果。

时间限制:1000

内存限制:65536

输入

输入的每一行包括一组测试数据,即为糖果数n。最后一行为0,表示测试结束。

输出

每一行输出对应一行输入的结果,即为吃法的数目。


样例输入

1
2
3
4
12
0

样例输出

0
1
1
1
12
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">#include &lt;iostream&gt; using namespace std; int work(int x) {   if (x == 2 || x == 3) {     return 1;   }   if (x == 1) {     return 0;   }   return work(x - 2) + work(x - 3); } int main() {   int a = 1;   while (a == 1) {     int n;     cin &gt;&gt; n;     if (n == 0) {       return 0;     }     cout &lt;&lt; work(n) &lt;&lt; endl;   }   return 0; }</pre>
这道题暂未补解析。
3. 简答题

扩号匹配问题

在某个字符串(长度不超过100)中有左括号、右括号和大小写字母;规定(与常见的算数式子一样)任何一个左括号都从内到外与在它右边且距离最近的右括号匹配。写一个程序,找到无法匹配的左括号和右括号,输出原来字符串,并在下一行标出不能匹配的括号。不能匹配的左括号用"$"标注,不能匹配的右括号用"?"标注.

时间限制:3000

内存限制:65536

输入

输入包括多组数据,每组数据一行,包含一个字符串,只包含左右括号和大小写字母,字符串长度不超过100

输出

对每组输出数据,输出两行,第一行包含原始输入字符,第二行由"$","?"和空格组成,"$"和"?"表示与之对应的左括号和右括号不能匹配。

样例输入

((ABCD(x)
)(rttyy())sss)(

样例输出

((ABCD(x)
$$
)(rttyy())sss)(
?            ?$
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">//解法1:递归 #include &lt;bits/stdc++.h&gt; using namespace std; #define N 105 char s[N], sign[N]; int si, len; void find()//找下一个右括号 如果找到,si指向右括号 如果没找到,做标记  {     int lp = si;//当前左括号的下标      si++;     while(si &lt; len)     {         if(s[si] == ')')             return;         else if(s[si] == '(')             find();         si++;     }     sign[lp] = '$';//没有找到右括号,左括号的位置标记为$  } int main() {  while(cin &gt;&gt; s)  {      len = strlen(s);   for(int i = 0; i &lt; len; ++i)//初始化sign字符串为空格              sign[i] = ' ';         sign[len] = '\0';         si = 0;         while(si &lt; len)         {             if(s[si] == ')')//右括号的位置标记为?                  sign[si] = '?';             else if(s[si] == '(')                 find();             si++;         }         cout &lt;&lt; s &lt;&lt; endl &lt;&lt; sign &lt;&lt; endl;  }  return 0; }</pre><pre class="brush:cpp;toolbar:false">//解法2 // 就近原则匹配:栈 // 遍历字符串 // 遇到左括号,将其下标入栈 // 右括号则先判断栈内元素情况 // 1.空,则将其下表记录到数组中 // 2.非空,则栈顶元素弹出(匹配到了) // 将字符串便遍历后,将栈内元素记录入另一个数组 // 用两数组内的对应下标分别改成对应字符 #include &lt;bits/stdc++.h&gt; using namespace std; const int N = 1e5 + 10; string s; char ans[N]; // 第二排的输出数组 stack&lt;int&gt; st; int a[N], cnt = 1; // 未匹配的右括号的下标存储 int b[N], cnn = 1; // 未匹配的左括号的下标存储 int main() {   while (getline(cin, s)) { // 输入不结束     memset(ans, 0, sizeof(ans));     memset(a, 0, sizeof(a));     memset(b, 0, sizeof(b));     cnt = 1;     cnn = 1; // 数据初始化     for (int i = 0; i &lt; s.size(); i++) {       if (s[i] == '(') {         st.push(i); // 左括号统一入栈       }       if (s[i] == ')') {         if (!st.empty()) {           st.pop(); // 若栈非空,则可以匹配         } else {           a[cnt++] = i; // 否则无法匹配,记录         }       }     }     if (!st.empty())       while (!st.empty()) {         b[cnn++] = st.top();         st.pop();       } // 栈内数据(下标)的出栈     for (int i = 1; i &lt; cnn; i++) {       ans[b[i]] = '$'; // 在ans内标记     }     for (int i = 1; i &lt; cnt; i++) {       ans[a[i]] = '?'; // 在ans内标记     }     cout &lt;&lt; s &lt;&lt; endl;     for (int i = 0; i &lt; s.size(); i++) {       if (ans[i] == 0)         cout &lt;&lt; " ";       if (ans[i] == '$')         cout &lt;&lt; "$";       if (ans[i] == '?')         cout &lt;&lt; "?"; // 格式转化     }     cout &lt;&lt; endl;   }   return 0; }</pre><p><br/></p>
这道题暂未补解析。
4. 简答题

质数的和与积

两个质数的和是S,它们的积最大是多少?

时间限制:10000

内存限制:65536

输入

一个不大于10000的正整数S,为两个质数的和。

输出

一个整数,为两个质数的最大乘积。数据保证有解。

样例输入

50

样例输出

589
查看答案与解析
答案:<pre class="brush:cpp;toolbar:false">#include &lt;cstdio&gt; #include &lt;cstring&gt; #include &lt;iostream&gt; #include &lt;math.h&gt; #include &lt;string&gt; using namespace std; int prime(int a) {   int i;   if (a == 1)     return 0;   for (i = 2; i &lt;= sqrt(a); i++)     if (a % i == 0)       return 0;   return 1; } int main() {   int j, s, x = 0, y;   scanf("%d", &amp;s);   y = s;   for (j = 2; j &lt;= s / 2; j++) {     if (prime(j) == 1 &amp;&amp; prime(s - j) == 1) {       if (j &gt; x &amp;&amp; (s - j) &lt; y) {         x = j;         y = s - j;       }     }   }   printf("%d", x * y);   return 0; }</pre>
这道题暂未补解析。
5. 简答题

因子问题

任给两个正整数N、M,求一个最小的正整数a,使得a和(M-a)都是N的因子。

时间限制:10000

内存限制:65536

输入

包括两个整数N、M。N不超过1,000,000。

输出

输出一个整数a,表示结果。如果某个案例中满足条件的正整数不存在,则在对应行输出-1

样例输入

35 10

样例输出

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