2025.7.27

· · 个人记录

T1 b3839

错因:忘记写输出。

正确答案:

#include<bits/stdc++.h>
using namespace std;
long long n, sum;
int main(){
    cin >> n;
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= i; j++){//括号中的计算
            sum += j;
        }
    }
    cout << sum;
}

T2 p1029

错因:没有判断ai与bi为零的情况。

正确答案:

#include<bits/stdc++.h>
using namespace std;
long long n, m, k, cnt, c[30005];
struct as{
    long long a, b, sh;
}z[30005];
int main(){
    cin >> k >> m >> n;
    for (int i = 1; i <= m; i++){
        cin >> z[i].a >> z[i].b;
        if (z[i].a != 0){
            z[i].sh = k / z[i].a * z[i].b;//计算可以造成的伤害。
        }
        else{//ai为零的特殊情况。
            z[i].sh = INT_MAX;
        }
    }
    for (int i = 1; i <= m; i++){
        if (z[i].sh >= n){
            c[++cnt] = i;//统计可以击杀boss的序号。
        }
    }
    if (cnt == 0){
        cout << -1;
        return 0;
    }
    sort(c + 1, c + cnt + 1);//从小到大排序。
    for (int i = 1; i <= cnt; i++){
        cout << c[i] << " ";//输出
    }
}

T3 b3844

错因:没有考虑当输出大于z时的情况,没有加上特判。

正确答案:

#include<bits/stdc++.h>好
using namespace std;
long long n, m, k, cnt, c[3005];
char s = 'A', str;
int main(){
    cin >> n;
    for (int i = 1; i <= n; i++){
        str = s + 1;//保存好下一列的开头。
        if(str > 'Z'){
            str = 'A';
        }
        for (int j = 1; j <= n; j++){
            cout << s;
            s++;
            if (s > 'Z'){//特判
                s = 'A';
            }
        }
        cout << endl;
        s = str;将s设为下一行的开头。
    }
}

T4 p1029

错因:定义成了双重循环,导致用时超时。

正确答案:

#include<bits/stdc++.h>
using namespace std;
long long x, y, sum;
long long lcm(long long x, long long y){
    return x * y / __gcd(x, y);//最小公倍数公式
}
int main(){
    cin >> x >> y;
    if (__gcd(x, y) == 1){//判断是否互质。
        cout << 2;
        return 0;
    }
    for (int p = x; p <= y; p++){
        int q = x * y / p;//算出q
        if (lcm(p, q) == y && __gcd(p, q) == x){
            sum++;//累加个数。
        }
    }
    cout << sum;//输出
}

T5 p4379

错因:定义成了双重循环,导致用时超时。

思路:让耐心大的牛站前面,耐心小的站后面时就有概率不排。

正确答案:

#include<bits/stdc++.h>
using namespace std;
int n, a[1000005], sum;
bool cmp(int x, int y){
    return x > y;//排序函数
}
int main(){
    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> a[i];//输入
    }
    sort(a + 1, a + n + 1, cmp);//排序
    for (int i = 1; i <= n; i++){
        if (a[i] >= sum){
            sum++;//累加
        }
    }
    cout << sum;//输出
    return 0;
}