2020C++等级考试二级真题题解

2024-06-22 00:20

本文主要是介绍2020C++等级考试二级真题题解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 202012数组指定部分逆序重放c++
 

#include <iostream>
using namespace std;
int main() {int a[110];int n, k;cin >> n >> k;for (int i = 0; i < n; i++) {cin >> a[i];}for (int i = 0; i < k / 2; i++) {swap(a[i], a[k - 1 - i]);}for (int i = 0; i < n; i++) {cout << a[i] << " ";}return 0;
}

202012简单密码c++
 

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {char a[220];gets(a);for (int i = 0; i < strlen(a); i++) {if (a[i] >= 'A' && a[i] <= 'E') {a[i] = a[i] + 21;} else if (a[i] >= 'F' && a[i] <= 'Z') {a[i] = a[i] - 5;}}cout << a;return 0;
}

 202012错误探测c++
 

#include <iostream>
using namespace std;void shuru();
void tongji1();
int a[110][110] = { 0 };
int n;int main() {shuru();tongji1();int cnt1 = 0;  //奇数列的个数int x = 0;for (int i = 1; i <= n; i++)  //统计奇数列有几个{if (a[0][i] % 2 != 0) {cnt1++;x = i;}}int cnt2 = 0;  //奇数行的个数int y = 0;for (int i = 1; i <= n; i++)  //统计奇数行有几个{if (a[i][0] % 2 != 0) {cnt2++;y = i;}}if (cnt1 == 0 && cnt2 == 0) {cout << "OK" << endl;} else if (cnt1 == 1 && cnt2 == 1) {cout << y << " " << x << endl;} else if (cnt1 > 1 || cnt2 > 1) {cout << "Corrupt" << endl;}return 0;
}
void shuru() {cin >> n;for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {cin >> a[i][j];}}
}
void tongji1() {for (int i = 1; i <= n; i++)  //行{for (int j = 1; j <= n; j++)  //列{if (a[i][j] == 1) {a[i][0]++;  //对应行头(0)位置++a[0][j]++;  //对应列头(0)位置++}}}
}

 202012奇数单增序列c++
 

#include <iostream>
using namespace std;
int a[510];
int la = 0;
int main() {int n;cin >> n;for (int i = 0; i < n; i++) {int s;cin >> s;if (s % 2 == 1) {a[la] = s;la++;}}for (int i = 0; i < la - 1; i++) {for (int j = 0; j < la - 1 - i; j++) {if (a[j] > a[j + 1]) {swap(a[j], a[j + 1]);}}}for (int i = 0; i < la; i++) {if (i != la - 1)  cout << a[i] << ",";else cout << a[i];}return 0;
}

 202012话题焦点人物c++
 

#include <iostream>
using namespace std;
int a[10010];
int b[10010][30];
int cnt[110];
int main() {int n;cin >> n;for (int i = 0; i < n; i++) {cin >> a[i];cin >> b[i][0];for (int j = 1; j <= b[i][0]; j++) {cin >> b[i][j];cnt[b[i][j]]++;}}int ma = -1;int mai = -1;for (int i = 0; i <= 100; i++) {if (cnt[i] > 0) {if (ma < cnt[i]) {ma = cnt[i];mai = i;}}}cout << mai << endl;int cnt2[10010] = { 0 };for (int i = 0; i < n; i++) {for (int j = 1; j <= b[i][0]; j++) {if (b[i][j] == mai) {cnt2[a[i]]++;}}}for (int i = 0; i <= 10000; i++) {if (cnt2[i] > 0) {cout << i << " ";}}return 0;
}

 

 202009单词倒排c++

#include <iostream>
#include <string>
using namespace std;
string a[1010];
int la = 0;
void addstring(string);
void sortstring();
int main() {string s;getline(cin, s);int ls = s.size();int cnt = 0;for (int i = 0; i < ls + 1; i++) {if (s[i] == ' ' || s[i] == '\0') {if (cnt != 0) {string tmp = s.substr(i - cnt, cnt);a[la++] = tmp;}cnt = 0;} else if (s[i] != ' ') {cnt++;}}for (int i = la - 1; i >= 0; i--) {cout << a[i] << " ";}return 0;
}


 

202009细菌的繁殖与扩散c++
 

#include <iostream>
using namespace std;
int a[11][11] = { 0 };
int b[11][11] = { 0 };
int m, d;
void aCopyTob();
void bCopyToa();
void showa();
void showb();
int main() {cin >> m >> d;a[5][5] = m;for (int k = 0; k < d; k++) {// 1.a复制到baCopyTob();// 2.a死亡,b复制到abCopyToa();}// 3.显示showa();return 0;
}
void aCopyTob() {//右下左上 右下  左下 右上 左上int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };for (int i = 1; i <= 9; i++) {for (int j = 1; j <= 9; j++) {if (a[i][j] != 0) {for (int k = 0; k < 8; k++) {int tx = i + dx[k];int ty = j + dy[k];if (tx >= 1 && tx <= 9 && ty >= 1 && ty <= 9) {b[tx][ty] = b[tx][ty] + a[i][j] * 1;}}b[i][j] = b[i][j] + a[i][j] * 2;}}}
}
void bCopyToa() {for (int i = 1; i <= 9; i++) {for (int j = 1; j <= 9; j++) {a[i][j] = b[i][j];b[i][j] = 0;}}
}
void showa() {for (int i = 1; i <= 9; i++) {for (int j = 1; j <= 9; j++) {cout << a[i][j] << " ";}cout << endl;}
}
void showb() {for (int i = 1; i <= 9; i++) {for (int j = 1; j <= 9; j++) {cout << b[i][j] << " ";}cout << endl;}
}

 202009高精度加法c++

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
string myadd(string, string);
int main() {string a, b;cin >> a >> b;cout << myadd(a, b);return 0;
}
string myadd(string s1, string s2) {int a[1000] = { 0 };int la = s1.size();for (int i = 0; i < la; i++) {a[la - 1 - i] = s1[i] - 48;}int b[1000] = { 0 };int lb = s2.size();for (int i = 0; i < lb; i++) {b[lb - 1 - i] = s2[i] - 48;}for (int i = 0; i < max(la, lb); i++) {a[i] = a[i] + b[i];if (a[i] >= 10) {a[i + 1] = a[i + 1] + 1;a[i] = a[i] - 10;}}int p = -1;for (int i = max(la, lb) + 1; i >= 0; i--) {if (a[i] != 0) {p = i;break;}}string s = "";for (int i = 0; i <= p; i++) {s = char(a[i] + 48) + s;}return s;
}


 

202009合影效果c++
 

#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
int main() {char a[50][20];float b[50];char cache[50];int n;cin >> n;for (int i = 0; i < n; i++) {cin >> a[i] >> b[i];}for (int i = 0; i < n - 1; i++) {for (int j = 0; j < n - 1 - i; j++) {if (a[j][0] == 'f' && a[j + 1][0] == 'm') {strcpy(cache, a[j]);strcpy(a[j], a[j + 1]);strcpy(a[j + 1], cache);swap(b[j], b[j + 1]);} else if (a[j][0] == 'm' && a[j + 1][0] == 'm' && b[j] > b[j + 1]) {strcpy(cache, a[j]);strcpy(a[j], a[j + 1]);strcpy(a[j + 1], cache);swap(b[j], b[j + 1]);} else if (a[j][0] == 'f' && a[j + 1][0] == 'f' && b[j] < b[j + 1]) {strcpy(cache, a[j]);strcpy(a[j], a[j + 1]);strcpy(a[j + 1], cache);swap(b[j], b[j + 1]);}}}for (int i = 0; i < n; i++) {cout << fixed << setprecision(2) << b[i] << " ";}return 0;
}

202009循环数c++
 

#include <iostream>
#include <string>
using namespace std;
string cheng(string, int);int main() {// cout<<cheng("5046766555",2)<<endl;string a;cin >> a;string na = a + a;// cout<<na<<endl;int len = a.size();for (int i = 1; i <= len; i++) {string s = cheng(a, i);// cout<<s<<endl;if (na.find(s) == string::npos) {cout << 0;return 0;}}cout << 1;return 0;
}
string cheng(string n, int a) {int na[1000] = { 0 };int l = n.size();for (int i = 0; i < l; i++) {na[i] = n[l - 1 - i] - 48;}for (int i = 0; i < l; i++) {na[i] = na[i] * a;}for (int i = 0; i < l + 70; i++) {if (na[i] >= 10) {na[i + 1] = na[i + 1] + na[i] / 10;na[i] = na[i] % 10;}}int p = 0;for (int i = l + 70; i >= 0; i--) {if (na[i] != 0) {p = i;break;}}string r = "";for (int i = 0; i <= p; i++) {r = (char)(na[i] + 48) + r;}return r;
}

202006计算矩阵边缘元素之和c++
 

#include <iostream>
#include <string>
using namespace std;int main() {int sum = 0;int m, n;cin >> n >> m;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {int a;cin >> a;if (i == 0 || j == 0 || i == n - 1 || j == m - 1) {sum = sum + a;}}}cout << sum;return 0;
}

202006最长最短单词c++
 

#include <iostream>
#include <string.h>
using namespace std;
int main() {char a[20000];cin.getline(a, 20000);int cnt = 0;int maxCnt = 0;int minCnt = 301;int maxSp = 0;int minSp = 301;int len = strlen(a);for (int i = 0; i < len + 1; i++) {if (a[i] != ' ' && a[i] != ',') {cnt++;} else if (cnt > 0) {if (maxCnt < cnt) {maxCnt = cnt;maxSp = i - cnt;}if (minCnt > cnt) {minCnt = cnt;minSp = i - cnt;}cnt = 0;}}for (int i = maxSp; i < maxCnt + maxSp; i++) {cout << a[i];}cout << endl;for (int i = minSp; i < minCnt + minSp; i++) {cout << a[i];}return 0;
}

 202006啤酒厂选址c++
 

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int a[10020];  //正方向距离
int c[10010];  //啤酒
int n;
int main() {system("chcp 65001");cin >> n;for (int i = 1; i <= n; i++) {cin >> c[i - 1] >> a[i];}for (int i = 1; i <= n; i++)  //利用前缀和{a[i] = a[i] + a[i - 1];}int mi = 999999999;int mii = -1;for (int i = 0; i < n; i++) {int sum = 0;for (int j = 0; j < n; j++) {int t1 = abs(a[j] - a[i]);int t2 = abs(a[n] - t1);int juli = min(t1, t2);sum = sum + juli * c[j];}if (sum < mi) {mi = sum;mii = i;}}cout << mii << "," << mi << endl;return 0;
}

202006统计误差范围内的数c++
 

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int a[100];
int n, x, c;
int main() {cin >> n;for (int i = 0; i < n; i++) {cin >> a[i];}cin >> x >> c;int cnt = 0;for (int i = 0; i < n; i++) {if (abs(a[i] - x) <= c) {cnt++;}}cout << cnt;return 0;
}

202006单词排序c++

#include <iostream>
#include <string>
using namespace std;
string a[1010];
int la = 0;
void addstring(string);
void sortstring();
int main() {string s;getline(cin, s);int ls = s.size();int cnt = 0;for (int i = 0; i < ls + 1; i++) {if (s[i] == ' ' || s[i] == '\0') {if (cnt != 0) {string tmp = s.substr(i - cnt, cnt);addstring(tmp);}cnt = 0;} else if (s[i] != ' ') {cnt++;}}sortstring();for (int i = 0; i < la; i++) {cout << a[i] << endl;}return 0;
}
void addstring(string t) {for (int i = 0; i < la; i++) {if (a[i] == t)return;}a[la++] = t;
}
void sortstring() {for (int i = 0; i < la - 1; i++) {for (int j = 0; j < la - 1 - i; j++) {if (a[j] > a[j + 1]) {swap(a[j], a[j + 1]);}}}
}

这篇关于2020C++等级考试二级真题题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1082753

相关文章

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

C++ scoped_ptr 和 unique_ptr对比分析

《C++scoped_ptr和unique_ptr对比分析》本文介绍了C++中的`scoped_ptr`和`unique_ptr`,详细比较了它们的特性、使用场景以及现代C++推荐的使用`uni... 目录1. scoped_ptr基本特性主要特点2. unique_ptr基本用法3. 主要区别对比4. u

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js

C++ 多态性实战之何时使用 virtual 和 override的问题解析

《C++多态性实战之何时使用virtual和override的问题解析》在面向对象编程中,多态是一个核心概念,很多开发者在遇到override编译错误时,不清楚是否需要将基类函数声明为virt... 目录C++ 多态性实战:何时使用 virtual 和 override?引言问题场景判断是否需要多态的三个关

C++简单日志系统实现代码示例

《C++简单日志系统实现代码示例》日志系统是成熟软件中的一个重要组成部分,其记录软件的使用和运行行为,方便事后进行故障分析、数据统计等,:本文主要介绍C++简单日志系统实现的相关资料,文中通过代码... 目录前言Util.hppLevel.hppLogMsg.hppFormat.hppSink.hppBuf