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

1 篇文章 0 订阅
1 篇文章 0 订阅

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

  • 15
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值