PTA 520鑽石爭霸賽 2021


7-1 自動編程

簽到題

#include<bits/stdc++.h>

typedef long long ll;
const int maxm = 1e5 + 5;
const int infmax = INT_MAX;
const int infmin = INT_MIN;
using namespace std;

int main() {
    int n;
    cin >> n;
    printf("print(%d)", n);
    return 0;
}

 

7-2 加油沖鴨

簽到

#include<bits/stdc++.h>

typedef long long ll;
const int maxm = 1e5 + 5;
const int infmax = INT_MAX;
const int infmin = INT_MIN;
using namespace std;

int main() {
    int n, m, s;
    cin >> n >> m >> s;
    int p = n - m * s;
    if (p > n / 2) {
        printf("hai sheng %d mi! jia you ya!\n", p);
    } else {
        printf("hai sheng %d mi! chong ya!\n", p);
    }
    return 0;
}

 

7-3 520的表白

簽到

#include<bits/stdc++.h>

typedef long long ll;
const int maxm = 1e5 + 5;
const int infmax = INT_MAX;
const int infmin = INT_MIN;
using namespace std;

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < 520; i++)
        cout << n << "\n";
    return 0;
}

 

7-4 奇葩樓層

遍歷一遍樓層,帶忌諱的數字不記數就行

#include<bits/stdc++.h>

typedef long long ll;
const int maxm = 1e5 + 5;
const int infmax = INT_MAX;
const int infmin = INT_MIN;
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        int j = i, f = 1;
        while (j) {
            int k = j % 10;
            j /= 10;
            if (k == m) {
                f = 0;
                break;
            }
        }
        if (f) sum++;
    }
    cout << sum << "\n";
    return 0;
}

 

7-5 大勾股定理

平方和數太大,找規律,可以發現n = 1時,3開始;n = 2時,10開始, n = 3時,21開始。

1 * 3 = 3;

2 * 5 = 10;

3 * 7 = 21;

從而猜出n * (2 * n + 1)。

15分題不會搞很復雜的數列的。

#include<bits/stdc++.h>

typedef long long ll;
const int maxm = 1e5 + 5;
const int infmax = INT_MAX;
const int infmin = INT_MIN;
using namespace std;

int main() {
    int n;
    cin >> n;
    int k = n * (2 * n + 1);
    for (int i = k; i <= k + n; i++) {
        if (i == k) cout << i << "^2";
        else cout << " + " << i << "^2";
    }
    cout << " =\n";
    int j = k + n + 1, m = 0;
    while (m < n) {
        if (m == 0) cout << j << "^2";
        else cout << " + " << j << "^2";
        j++;
        m++;
    }
    return 0;
}

 

7-6 矩陣列平移

模擬,注意寫的時候不要把行和列寫岔了就沒啥問題

#include<bits/stdc++.h>

typedef long long ll;
const int maxm = 1e5 + 5;
const int infmax = INT_MAX;
const int infmin = INT_MIN;
using namespace std;

int a[110][110];

int main() {
    int n, k, x;
    cin >> n >> k >> x;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            cin >> a[i][j];
    int m = 1;
    for (int i = 2; i <= n; i += 2) {
        for (int j = n; j > m; j--)
            a[j][i] = a[j - m][i];
        for (int j = 1; j <= m; j++)
            a[j][i] = x;
        m++;
        if (m > k) m = 1;
    }

    for (int i = 1; i <= n; i++) {
        int sum = 0, j = 1;
        while (j <= n) sum += a[i][j], j++;
        if (i == 1)
            cout << sum;
        else cout << ' ' << sum;
    }
    return 0;
}

 

7-7 約會大作戰

題解在代碼理,時間緊代碼寫的很不好,巨巨可以用結構體整理以下

#include<bits/stdc++.h>

typedef long long ll;
const int maxm = 1e5 + 5;
const int infmax = INT_MAX;
const int infmin = INT_MIN;
using namespace std;

int a[110][110], b[110][110], su1[110], su2[110];
int st1[110], st2[110], max1[110], max2[110];
int pre1[110], pre12[110];
int pre2[110], pre22[110];

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 0; i < 110; i++) {
        max1[i] = max2[i] = -110;//存該組第i位前2的最大好感度,初始化寫最小
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++)
            cin >> a[i][j];//1組第i位對2組第j位的好感度
    }

    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++)
            cin >> b[i][j];//2組第i位對1組第j位的好感度
    }

    int x, y, f = 0;
    while (q--) {
        cin >> x >> y;
        st1[x]++;
        st2[y]++;//判斷是否拒絕2次
        if (st1[x] > 2 && st2[y] > 2) {//都已經拒絕2次
            if (a[x][y] > max1[x] && b[y][x] > max2[y]) {//判斷是否大於拒絕前2次人的好感度
                if (!su1[x] && !su2[y]) {//su是表示有木有約會成功過,1就是約過了
                    su1[x] = su2[y] = 1;
                    cout << x << ' ' << y << "\n";
                    f = 1;//判斷有木有人約會成功過
                }
            }
        }

        pre1[x] = pre12[x];
        pre12[x] = a[x][y];
        //1組第x個人的拒絕的前2個的好感度,這個時候有新的好感度,所以把第2個好感度
        //覆蓋到第一個,第二個好感度等於新的拒絕的好感度

        pre2[y] = pre22[y];//與上同理
        pre22[y] = b[y][x];

        max1[x] = max(pre1[x], pre12[x]);//存該組第x位拒絕前2位的最大好感度
        max2[y] = max(pre2[y], pre22[y]);//同理
    }
    if (!f) cout << "PTA is my only love\n";//沒有人約會
    return 0;
}

 

7-8 浪漫側影

左右視其實就是二叉樹各層數的第一個和最后一個,所以根據中后序遍歷下前序,聲明個層數向量存每層的樹節點就行,最后根據左右視輸出每層的第一個和最后一個節點

#include<bits/stdc++.h>

typedef long long ll;
const int maxm = 1e5 + 5;
const int infmax = INT_MAX;
const int infmin = INT_MIN;
using namespace std;

int in[250], post[250], maxdepth = 0;
vector<int> v[250];

void pre(int root, int start, int end, int depth) {
    if (start > end)
        return;
    maxdepth = max(maxdepth, depth);
    int i = start;
    while (i < end && in[i] != post[root]) i++;
    v[depth].push_back(post[root]);
    pre(root - 1 - (end - i), start, i - 1, depth + 1);
    pre(root - 1, i + 1, end, depth + 1);
}


int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> in[i];
    for (int i = 1; i <= n; i++) cin >> post[i];

    pre(n, 1, n, 1);
    cout << "R:";
    for (int i = 1; i <= maxdepth; i++) cout << ' ' << v[i].back();
    cout << "\n";

    cout << "L:";
    for (int i = 1; i <= maxdepth; i++) cout << ' ' << v[i].front();
    cout << "\n";
    return 0;
}

 

over,代碼寫的有點爛,巨巨們輕噴。


免責聲明!

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



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