当前位置: 首页 > news >正文

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]);}}}
}

相关文章:

  • oracle12c到19c adg搭建(五)dg搭建后进行切换19c进行数据字典升级
  • Java面试题:对比继承Thread类和实现Runnable接口两种创建线程的方法,以及它们的优缺点
  • 通信系统概述
  • 示例:WPF中在没有MouseDoubleClick的控件中如何识别双击
  • SVG 参考手册
  • 小熊文件工具箱免费版
  • 每天写java到期末考试(6.22)--集合5--练习
  • Spring框架中哪些地方使用了反射
  • 第六节 LLava模型数据处理源码解读(input_ids/labels/attention_mask/image,下篇)
  • 数据结构-图的基本概念
  • 小程序项目业务逻辑回忆4
  • huggingface连不上的解决方案
  • oracle发送http请求
  • C++ 反转一个二进制串
  • cd 命令特殊路径符 mkdir命令
  • ABAP的include关键字,Java的import, C的include和C4C ABSL 的import比较
  • Fundebug计费标准解释:事件数是如何定义的?
  • Git初体验
  • java多线程
  • js面向对象
  • Python利用正则抓取网页内容保存到本地
  • Python学习之路16-使用API
  • Spring框架之我见(三)——IOC、AOP
  • Spring声明式事务管理之一:五大属性分析
  • 浮现式设计
  • 设计模式(12)迭代器模式(讲解+应用)
  • 问:在指定的JSON数据中(最外层是数组)根据指定条件拿到匹配到的结果
  • Spark2.4.0源码分析之WorldCount 默认shuffling并行度为200(九) ...
  • 选择阿里云数据库HBase版十大理由
  • #14vue3生成表单并跳转到外部地址的方式
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • (3)(3.2) MAVLink2数据包签名(安全)
  • (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
  • (第三期)书生大模型实战营——InternVL(冷笑话大师)部署微调实践
  • (翻译)terry crowley: 写给程序员
  • (深度全面解析)ChatGPT的重大更新给创业者带来了哪些红利机会
  • (文章复现)基于主从博弈的售电商多元零售套餐设计与多级市场购电策略
  • (续)使用Django搭建一个完整的项目(Centos7+Nginx)
  • (循环依赖问题)学习spring的第九天
  • (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)
  • (一一四)第九章编程练习
  • (转)Sublime Text3配置Lua运行环境
  • (转)德国人的记事本
  • **《Linux/Unix系统编程手册》读书笔记24章**
  • .NET CF命令行调试器MDbg入门(一)
  • .NET CLR基本术语
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • .NET MVC第五章、模型绑定获取表单数据
  • .net 获取某一天 在当月是 第几周 函数
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .NET 中使用 Mutex 进行跨越进程边界的同步
  • .NET(C#) Internals: as a developer, .net framework in my eyes
  • .NET的数据绑定
  • .net生成的类,跨工程调用显示注释
  • @NoArgsConstructor和@AllArgsConstructor,@Builder