7月27日补题

这篇博客探讨了三道C++编程题目,涉及数组处理、条件判断和计数问题。第一题处理字符串数组的最大长度及输出;第二题解决拼图块匹配,通过因数分解检查条件;第三题关注数组元素的递增计数,处理0值的特殊情况。文章重点在于错误处理和优化算法。
摘要由CSDN通过智能技术生成

G题

水题

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>

using namespace std;

struct meal {
    int n;
    string s[60];
}a[100];

int main() {
    int t;
    int max = 0, index = 0;
    cin >> t;
    for (int i = 0; i < t; i ++) {
        int n;
        cin >> n;
        if (n > max)
            max = n, index = i;
        a[i].n = n;
        for (int j = 0; j < n; j ++) {
            cin >> a[i].s[j];
        }
    }
    cout << max << endl;
    for (int j = 0; j < max; j ++) {
        cout <<a[index].s[j] << endl;
    }
}

I题

I题是给出角、边、内部的块,看能不能恰好拼成一个拼图。自己在写这个题时是将内部的块因数分解,看是否存在一种情况使因数和的两倍等于边块的和,有就是符合题意。但自己这样做产生了一个问题就是当内部块为0的情况自己没有考虑到,所以自己wa了四次。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

ll cnt = 0;

struct num {
    ll a, b;
}a[100000010];

void get_d(ll x) {
    for (ll i = 1; i <= x / i; i ++ )
        if (x % i == 0)
        {
            a[cnt].a = i;
            a[cnt].b = x / i;
            cnt ++;
        }
}

int main() {
    ll jiao, bian, nei;
    cin >> jiao >> bian >> nei;
    if (jiao != 4 || bian % 2 != 0) {
        cout << "impossible" << endl;
        return 0;
    }
    ll chang = 0, kuan = 0;
    get_d(nei);
    for (ll i = 0; i < cnt; i ++) {
        chang = max(a[i].a, a[i].b);
        kuan = min(a[i].a, a[i].b);
        if (chang * 2 + kuan * 2 == bian) {
            cout << chang + 2 << ' ' << kuan + 2 << endl;
            return 0;
        }
    }
    if (nei == 0 && bian % 2 == 0){
        cout << bian / 2 + 2 << ' ' << '2' << endl;
        return 0;
    }
    cout << "impossible" << endl;
}

C题

这个题自己在做的时候对0没有处理好,看到样例中最后出现0,就在计数的时候把0也记上了,最后导致自己在判断“ambiguous”的时候总是出错;这个题输出过题数时从0开始输出比较难,第一次做的时候我是从0输出,先把结果存在数组里再倒着输出,后来发现没有必要,直接倒着输出就行了。

#include <iostream>
#include <cstdio>

using namespace std;

const int N = 1e6;

int a[N];

int main() {
    int n, m;
    cin >> n >> m;
    int cnt =  1;
    for (int i = 0; i < n; i ++) {
        scanf("%d", &a[i]);
        if (i >= 1 && a[i] < a[i - 1] && a[i] != 0) {
            cnt ++;
        }
    }
    if (cnt != m && a[0] != 0) {
//         cout << cnt;
        puts("ambiguous");
        return 0;
    }
    for (int i = 0; i < n; i ++) {
        if (a[0] == 0) {
            m = 0;
        }
        if (i == 0)
            cout << m << endl;
        else {
            if (a[i] < a[i - 1])
                printf("%d\n", --m);
            else
                printf("%d\n", m);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值