首页 > 编程语言 >2020C++等级考试二级真题题解

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

时间:2024-06-21 21:58:01浏览次数:13  
标签:cnt string 真题 int 题解 cin C++ ++ include

 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复制到b
        aCopyTob();
        // 2.a死亡,b复制到a
        bCopyToa();
    }
    // 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]);
            }
        }
    }
}

标签:cnt,string,真题,int,题解,cin,C++,++,include
From: https://blog.csdn.net/x100109/article/details/139870599

相关文章

  • C++系统相关操作1 - 调用命令行并获取返回值
    1.关键词2.sysutil.h3.sysutil.cpp3.1.system_util_unix.cpp3.2.system_util_win.cpp4.测试代码5.运行结果6.源码地址1.关键词关键词:C++系统调用systempopen跨平台应用场景:希望直接调用操作系统的某些命令,并获取命令的返回值。2.sysutil.h#pragm......
  • C++系统相关操作2 - 获取系统环境变量
    1.关键词2.sysutil.h3.sysutil.cpp4.测试代码5.运行结果6.源码地址1.关键词C++系统调用环境变量getenv跨平台2.sysutil.h#pragmaonce#include<cstdint>#include<string>namespacecutl{/***@briefGetanenvironmentvariable.......
  • 题解:P10641 BZOJ3252 攻略
    我让cz搬这道题,cz给搬了,于是来写个题解(考虑一个朴素的贪心:每次选择一个到根路径价值和最大的叶子,将价值和累加进答案,并把这条链价值清零。这个贪心的正确性显然(可以交换法证明),很容易用数据结构维护做到\(O(n\logn)\)。但是这样太不优美了,而且数据结构比较难写,于是考虑一个......
  • C++核心编程运算符的重载
    C++核心编程运算符的重载文章目录C++核心编程运算符的重载1.“+”运算符的重载1.1作为成员函数重载1.2作为全局函数重载2."<<"运算符重载2.1为什么需要重载左移运算符2.2如何重载左移运算符2.3注意事项3."++"运算符重载3.1前置递增运算符重载3.2后置递增运算符重载......
  • 2022年大作业参考报告-使用C++语言开发小学生成绩管理系统、中学生成绩管理系统、大学
    背景:目录第一章需求分析   21.1   问题描述   26.1   功能需求   26.2   开发环境   26.3   开发过程   2第二章概要设计   32.1   总体设计   32.2   类的定义   32.3   接口设计   52.4  ......
  • opencv入门-小白的学习笔记c++(1)
    注:以下是根据链接https://blog.csdn.net/Cream_Cicilian/article/details/105427752的小白学习过程。1加载、修改、保存图像1.1加载图像1.1.1加载图像cv::imread用于从文件中读取图像数据并将其存储到一个cv::Mat对象中,其中第一个参数表示图像文件名称第二个参数,表......
  • 0基础学C++ | 第03天 | 基础知识 |算术运算符 | 赋值运算符 | 比较运算符 | 逻辑运算
    前言前面已经讲了,数据类型以及求数据类型所占的空间0基础学C++|第02天|基础知识|sizeof关键字|浮点型|字符型|转义字符|字符串|布尔类型|数据的输入-CSDN博客,现在讲运算符算术运算符 作用:用于处理四则运算#include<iostream>usingnamespacestd;in......
  • String(C++)
    文章目录前言文档介绍经典题目讲解HJ1字符串最后一个单词的长度模拟实现框架构造函数析构函数迭代器c_str()赋值size()capacity()reserveempty()[]访问front/backpush_backappendoperator+=insert一个字符insert一个字符串eraseswapfind一个字符find一个字符串substr(......
  • Windows C++ 应用软件开发从入门到精通详解
    目录1、引言2、IDE开发环境介绍2.1、VisualStudio 2.2、QTCreator3、Windows平台实用小工具介绍3.1、代码编辑器VSCode3.2、代码查看编辑器SourceInsight3.3、文本编辑器Notepad++3.4、文件搜索工具Everything4、C++语言特性4.1、熟悉泛型编程4.2、了解......
  • 【C++】priority_queue的模拟实现与仿函数
    文章目录1.优先级队列的介绍与使用1.1介绍1.2使用2.模拟实现2.1push2.2pop2.3top、empty、size2.4迭代区间构造3.仿函数1.优先级队列的介绍与使用1.1介绍优先级队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的。......