上海市計算機學會競賽平台2020年4月月賽(丙組題目)題解


比賽鏈接:https://iai.sh.cn/contest/4

T1 競選班長

題目鏈接:https://iai.sh.cn/problem/24

解題思路:

簡單if條件判斷。

示例程序:

#include <bits/stdc++.h>
using namespace std;
int a, b, c, d, e;
int main() {
    cin >> a >> b >> c >> d;
    if (a >= 90) e ++;
    if (b >= 90) e ++;
    if (c >= 90) e ++;
    puts(e >= 2 && d >= 85 ? "Qualified" : "Not qualified");
    return 0;
}

T2 永恆的生命游戲

題目鏈接:https://iai.sh.cn/problem/42

解題思路:

簡單二維字符數組模擬。

示例程序:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int n, m;
char s[maxn][maxn];
bool in_map(int x, int y) {
    return x >= 0 && x < n && y >= 0 && y < m;
}
int cal(int x, int y) {
    int cnt = 0;
    for (int i = x-1; i <= x+1; i ++) {
        for (int j = y-1; j <= y+1; j ++) {
            if (i == x && j == y || !in_map(i, j)) continue;
            if (s[i][j] == '*') cnt ++;
        }
    }
    return cnt;
}
bool check(int x, int y) {
    int cnt = cal(x, y);
    return (s[x][y]=='*' && cnt>=2 && cnt<=3 || s[x][y]=='.' && cnt!=3);
}
bool solve() {
    for (int i = 0; i < n; i ++)
        for (int j = 0; j < m; j ++)
            if (!check(i, j))
                return false;
    return true;
}
int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i ++) cin >> s[i];
    puts(solve() ? "Still life" : "Other");
    return 0;
}

T3 直線運輸

題目鏈接:https://iai.sh.cn/problem/33

解題思路:

遞推,前綴和。設 \(sum_i = \sum\limits_{j=1}^i a_j\),則答案為 \(\sum\limits_{i=1}^n sum_i\)

示例程序:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, a[maxn];
long long s, ans;
int main() {
    cin >> n;
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
        s += a[i];
        ans += abs(s);
    }
    cout << ans << endl;
    return 0;
}

T4 數字驗證

題目鏈接:https://iai.sh.cn/problem/26

解題思路:

字符串模擬。判斷一個字符串是否是一個實數的合法表示。

示例程序:

#include <bits/stdc++.h>
using namespace std;
char s[505];
int main() {
    cin >> s;
    int p = 0;
    if (s[p] == '+' || s[p] == '-') p ++;
    int q = -1;
    int n = strlen(s+p);
    if (n == 1 && s[p] == '.') {
        puts("Invalid");
        return 0;
    }
    for (int i = p; s[i]; i ++) {
        if (s[i] == '.') {
            q = i;
            break;
        }
    }
    for (int i = p; s[i]; i ++) {
        if (i != q && !isdigit(s[i])) {
            puts("Invalid");
            return 0;
        }
    }
    puts("Valid");
    return 0;
}

T5 排隊安排

題目鏈接:https://iai.sh.cn/problem/30

解題思路:

貪心。排序。模擬。
開一個變量 \(c\) 表示當前人數,如果 $ \le a_i$,說明第 \(i\) 個人是保持在隊列里面的。

注意:第 \(i\) 個人前面的人數不算他前面離開的人(只算還在的人)。

示例程序:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
int n, a[maxn], c;

int main() {
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i];
    sort(a, a+n);
    for (int i = 0; i < n; i ++) {
        if (c <= a[i]) c ++;
    }
    cout << c << endl;
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM