GESP 2023 题解

LGB3846 [GESP样题 一级] 闰年求和

#include <bits/stdc++.h>
using namespace std;

long long x, y, sum;

int main() {
    cin >> x >> y;
    // 注意不包含起始年份和终止年份
    for (int i = x + 1; i < y; i++) {
        // 判断闰年
        if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
            sum += i;
        }
    }
    cout << sum << endl;
    return 0;
}

[Copy](javascript:;)

LGB3847 [GESP样题 一级] 当天的第几秒

#include <bits/stdc++.h>
using namespace std;

long long hour, minute, second, total_second;
char type;

int main() {
    cin >> hour >> minute >> second >> type;
    total_second = hour * 60 * 60 + minute * 60 + second;  // 计算秒数
    if (type == 'P')                                       // 处理上下午
        total_second += 12 * 60 * 60;
    cout << total_second << endl;  // 输出秒数
    return 0;
}

[Copy](javascript:;)

LGB3844 [GESP样题 二级] 画正方形

#include <bits/stdc++.h>
using namespace std;

long long n;

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        // 循环遍历共 n 次
        for (int j = 0; j < n; j++)
            // 根据 i 和 j 的值计算索引,并输出对应字符
            cout << (char)('A' + (i + j) % 26);
        // 换行
        cout << endl;
    }
    return 0;
}

[Copy](javascript:;)

LGB3845 [GESP样题 二级] 勾股数

#include <bits/stdc++.h>
using namespace std;

long long c, n, cnt;

int main() {
    cin >> n;
    for (int a = 1; a <= n; a++) {    // 遍历a从1到n
        for (int b = a; b <= n; b++) {    // 遍历b从a到n
            c = sqrt(a * a + b * b);    // 计算c的值,即弦长
            if (a * a + b * b == c * c && c <= n) {    // 判断是否满足勾股定理且c不超过n
                cnt++;    // 符合条件则计数器cnt加1
            }
        }
    }
    cout << cnt << endl;
    return 0;
}

[Copy](javascript:;)

LGB3848 [GESP样题 三级] 逛商场

#include <bits/stdc++.h>
using namespace std;

const int N = 100 + 10;
long long arr[N];
long long n, x, cnt;

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> arr[i];
    }
    cin >> x;
    // 使用循环从1到n遍历所有元素
    for (int i = 1; i <= n; i++) {
        // 如果x大于等于当前元素arr[i]
        if (x >= arr[i]) {
            // 从x中减去当前元素arr[i]
            x -= arr[i];
            // 计数器加1
            cnt++;
        }
    }
    cout << cnt << endl;
    return 0;
}

[Copy](javascript:;)

LGB3849 [GESP样题 三级] 进制转换

#include <bits/stdc++.h>
using namespace std;

long long n, r;
string ans;

int main() {
    cin >> n >> r;
    while (n) {
        if (n % r > 10) { // 10进制以上
            ans += (n % r - 10) + 'A';
        } else { // 10进制及之内
            ans += n % r + '0';
        }
        n /= r;
    }
    reverse(ans.begin(), ans.end());
    cout << ans << endl;
    return 0;
}

[Copy](javascript:;)

LGB3834 [GESP202303 一级] 长方形面积

#include <bits/stdc++.h>
using namespace std;

long long area, cnt;

int main() {
    cin >> area;
    // 遍历从1到A的平方根之间的整数
    for (int i = 1; i * i <= area; i++) {
        // 如果A可以整除i
        if (area % i == 0) {
            // 将计数器C加1
            cnt++;
        }
    }
    cout << cnt << endl;
    return 0;
}

[Copy](javascript:;)

LGB3835 [GESP202303 一级] 每月天数

#include <bits/stdc++.h>
using namespace std;

long long year, month;

int main() {
    cin >> year >> month;
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
        cout << 31 << endl;
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        cout << 30 << endl;
    } else {
        // 判断闰年
        if ((year % 4 == 0 && year % 100!= 0) || year % 400 == 0) {
            cout << 29 << endl;
        } else {
            cout << 28 << endl;
        }
    }
    return 0;
}

[Copy](javascript:;)

LGB3836 [GESP202303 二级] 百鸡问题

#include <bits/stdc++.h>
using namespace std;

long long x, y, z, n, m, cnt;

int main() {
    cin >> x >> y >> z >> n >> m;

    // 遍历 公鸡 i 的取值范围(注意约束条件)
    for (int i = 0; i <= m && i <= n / x; i++) {
        // 遍历 母鸡 j 的取值范围(注意约束条件)
        for (int j = 0; j <= m - i && j <= (n - i * x) / y; j++) {
            // 小鸡的数量 为 m - i - j
            if (x * i + y * j + 1.0 / z * (m - i - j) == n) {
                cnt++; // 满足条件,计数加一
            }
        }
    }
    cout << cnt << endl;

    return 0;
}

[Copy](javascript:;)

LGB3837 [GESP202303 二级] 画三角形

#include <bits/stdc++.h>
using namespace std;

long long n, cnt;

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) { // 输出 n 行
        for (int j = 1; j <= i; j++) { // 三角形输出
            cout << char('A' + (cnt++)) ; // 顺序输出字母
            if (cnt == 26) cnt = 0; // 输出到 Z 后,从 A 开始输出
        }
        cout << endl;
    }
    return 0;
}

[Copy](javascript:;)

LGB3838 [GESP202306 一级] 时间规划

#include <bits/stdc++.h>
using namespace std;

int main() {
    int hours1, minutes1, hours2, minutes2;
    cin >> hours1 >> minutes1 >> hours2 >> minutes2;
    
    if (hours1 == hours2) {
        cout << minutes2 - minutes1 << endl;
    } else {
        cout << (hours2 - hours1) * 60 + minutes2 - minutes1 << endl;
    } 

    return 0;
}

[Copy](javascript:;)

LGB3839 [GESP202306 一级] 累计相加

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        sum += (i * (i + 1) / 2);
    }
    cout << sum << endl;
    return 0;
}

[Copy](javascript:;)

LGB3840 [GESP202306 二级] 找素数)

#include <bits/stdc++.h>
using namespace std;

int main() {
    int A, B, C = 0;
    cin >> A >> B;
    for (int i = A; i <= B; i++) {
        bool flag = true;
        int ii = sqrt(i);
        for (int j = 2; j <= ii; j++) {
            if (i % j == 0) {
                flag = false;
                break;
            }
        }
        if (flag) C++;
    }
    cout << C << endl;
    return 0;
}

[Copy](javascript:;)

LGB3841 [GESP202306 二级] 自幂数判断

#include <bits/stdc++.h>
using namespace std;

long long n, num, num_copy, cnt, sum;

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> num;
        cnt = 0;
        sum = 0;
        num_copy = num;
        // 计算数字位数
        while (num_copy) {
            num_copy /= 10;
            cnt++;
        }
        // 统计各位数字的幂次之和
        num_copy = num;
        for (int j = 1; j <= cnt; j++) {
            sum += pow(num_copy % 10, cnt);
            num_copy /= 10;
        }
        // 判断是否为自幂数
        if (sum == num) {
            cout << "T" << endl;
        } else {
            cout << "F" << endl;
        }
    }
    return 0;
}

[Copy](javascript:;)

LGB3842 [GESP202306 三级] 春游

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1000 + 10;
long long arr[MAXN];
long long N, M, num;
bool flag;

int main() {
    cin >> N >> M;
    for (int i = 0; i < M; i++) { // 编号从0开始
        cin >> num;
        arr[num]++; // 统计出现次数
    }
    for (int i = 0; i < N; i++) { // 编号从0开始
        if (arr[i] == 0) { // 若出现次数为0
            cout << i << " ";
            flag = true;
        }
    }
    if (!flag) { // 若没有次数为0的情况
        cout << N << endl;
    } else {
        cout << endl;
    }
    return 0;
}

[Copy](javascript:;)

LGB3843 [GESP202306 三级] 密码合规

#include <bits/stdc++.h>
using namespace std;

string line; // 输入的整行字符串
string str;  // 分解的单个密码字符串

// 检查密码是否合规
bool check(string str) {
    // 判断密码长度是否合规
    if (str.size() < 6 || str.size() > 12)
        return false;
    bool hasUpper = false, hasLower = false, hasDigit = false, hasSymbol = false;
    for (int i = 0; str[i] != '\0'; i++) {
        if ('A' <= str[i] && str[i] <= 'Z') {
            hasUpper = true;
        } else if ('a' <= str[i] && str[i] <= 'z') {
            hasLower = true;
        } else if ('0' <= str[i] && str[i] <= '9') {
            hasDigit = true;
        } else if (str[i] == '!' || str[i] == '@' || str[i] == '#' ||
                   str[i] == '$') {
            hasSymbol = true;
        } else
            return false;
    }
    // 判断密码是否包含符号
    if (!hasSymbol)
        return false;
    // 判断密码是否包含至少2种字符
    if (hasUpper + hasLower + hasDigit < 2)
        return false;
    // 合规
    return true;
}
int main() {
    cin >> line;
    // 按逗号对输入进行切分,并依次判断
    for (int i = 0; i < int(line.size()); i++) {
        if (line[i] == ',') { // 遇到 , 进行合规判断
            if (str.size() > 0) {
                // 如果合规,输出字符串
                if (check(str))
                    cout << str << endl;
                str.clear();
            }
        } else { // 否则,继续拼接字符串
            str.push_back(line[i]);
        }
    }
    return 0;
}

[Copy](javascript:;)

LGB3850 [GESP202306 四级] 幸运数

#include <bits/stdc++.h>
using namespace std;

long long n, num, sum;

// 奇数位转换
long long trans(long long digit) {
    return (digit * 7 - 1) % 9 + 1;
}

bool judge(long long num) {
    sum = 0;
    for (int i = 1; num > 0; i++, num /= 10) {
        long long digit = num % 10;
        if (i % 2 == 0) // 偶数位不变
            sum += digit;
        else // 奇数位转换
            sum += trans(digit);
    }
    return sum % 8 == 0;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> num;
        if (judge(num)) { // 判断幸运数
            cout << "T" << endl;
        } else {
            cout << "F" << endl;
        }
    }
    return 0;
}

[Copy](javascript:;)

2 条评论

  • 段佳君 (duanjiajun) LV 10 @ 11 个月前

    [ ](javascript:;)[](javascript:;)

    [GESP202303 一级] 每月天数

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        int a, b;
        cin >> a >> b;
        if (b == 1 || b == 3 || b == 5 || b == 7 || b == 8 || b == 10 || b == 12) {
            cout << 31 << endl;
        } else if (b == 4 || b == 6 || b == 9 || b == 11) {
            cout << 30 << endl;
        } else {
            if ((a % 400 == 0) || (a % 4 == 0 && a % 100 != 0)) {
                cout << 29 << endl;
            } else {
                cout << 28 << endl;
            }
        }
        return 0;
    }
    

    [Copy](javascript:;)

  • 段佳君 (duanjiajun) LV 10 @ 1 年前

    [ ](javascript:;)[](javascript:;)

    [GESP样题 一级] 当天的第几秒

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        int h, m, s;
        char time;
        cin >> h >> m >> s >> time;
        int ans = (h * 60 * 60) + m * 60 + s;
        if (time == 'P') {
            ans += 12 * 60 * 60;
        }
        cout << ans << endl;
        return 0;
    }
    

    [Copy](javascript:;)

    [GESP202303 一级] 长方形面积

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        long long a, cnt = 0;
        cin >> a;
        for (int i = 1; i <= sqrt(a); i++) {
            if (a % i == 0) {
                cnt++;
            }
        }
        cout << cnt << endl;
        return 0;
    }