您的位置:首页 > 财经 > 金融 > 2020C++等级考试二级真题题解

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

2024/12/23 11:10:16 来源:https://blog.csdn.net/x100109/article/details/139870599  浏览:    关键词: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]);}}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com