PTA2021 跨年挑戰賽部分題解


7-1 壓歲錢

不用說

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e9;
const int maxm = 1e5 + 5;
const ll inf = 2147483647;
using namespace std;

int main() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int sum = a + b + c + d;
    cout << sum;
    return 0;
}

 

7-2 射擊成績

微米轉毫米按環判斷。

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e9;
const int maxm = 1e5 + 5;
const ll inf = 2147483647;
using namespace std;

int main() {
    double n;
    cin >> n;
    if(n <= 11500 / 2)
        cout<<10;
    else if(n <= 27500 / 2)
        cout<<9;
    else if(n <= 43500 / 2)
        cout<<8;
    else if(n <= 59500 / 2)
        cout<<7;
    else if(n <= 75500 / 2)
        cout<<6;
    else if(n <= 91500 / 2)
        cout<<5;
    else if(n <= 107500 / 2)
        cout<<4;
    else if(n <= 123500 / 2)
        cout<<3;
    else if(n <= 139500 / 2)
        cout<<2;
    else if(n <= 155500 / 2)
        cout<<1;
    else cout<<0;
    return 0;
}

 

7-3 Cassels方程

不用說

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e9;
const int maxm = 1e5 + 5;
const ll inf = 2147483647;
using namespace std;

int main() {
    int n;
    cin >> n;
    while (n--) {
        int x, y, z;
        cin >> x >> y >> z;
        if (x * x + y * y + z * z != 3 * x * y * z)
            cout << "No" << endl;
        else cout << "Yes" << endl;
    }
    return 0;
}

 

7-4 相生相克

根據題意相生相克的數字和判斷,也可以直接從金到土的數字看另一個數字是啥判斷相生還是相克,因為相生相克都是一對一的。

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e9;
const int maxm = 1e5 + 5;
const ll inf = 2147483647;
using namespace std;

void judeg(int a, int b) {
    int sum = a + b;
    if (sum == 3)
        cout << "1 ke 2\n";
    else if (sum == 7) {
        if (a == 2 || a == 5)
            cout << "2 ke 5\n";
        else cout << "3 ke 4\n";
    } else if (sum == 8)
        cout << "5 ke 3\n";
    else if (sum == 5) {
        if (a == 4 || a == 1)
            cout << "4 ke 1\n";
        else cout << "3 sheng 2\n";
    } else if (sum == 6) {
        if (a == 2 || a == 4) {
            cout << "2 sheng 4\n";
        } else cout << "5 sheng 1\n";
    } else if (sum == 9)
        cout << "4 sheng 5\n";
    else if (a == 1 || a == 3)
        cout << "1 sheng 3\n";
}

int main() {
    int n;
    cin >> n;
    while (n--) {
        int x, y;
        cin >> x >> y;
        judeg(x, y);
    }
    return 0;
}

 

7-5 7-6太菜了沒過

 

7-5 整除階乘

對於每個數,直接把n * n + 1對n!的各乘因子求余,最后判斷n * n + 1是否變成1來輸出結果,用f做是否有結果標記,如果沒有就輸出None。來自:csdn

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e9;
const int maxm = 1e5 + 5;
const ll inf = 2147483647;
using namespace std;

int main() {
    int n, m, f = 0;
    cin >> n >> m;
    for (int i = n; i <= m; i++) {
        int sum = i * i + 1;
        for (int j = 2; j <= i; j++) {
            if (sum >= j && sum % j == 0)
                sum /= j;
            else if (sum < j && j % sum == 0)
                sum = 1;
        }
        if (sum == 1) {
            cout << i << endl;
            f = 1;
        }
    }
    if (!f)cout << "None";
    return 0;
}

 

7-7 打PTA 

先判斷最后一個字符是否是?,不是直接輸出enen,是就從下標2開始判斷此下標是否是字符A和前面兩個字符是否是T和P,是的話就把flag f設為真,f默認為假,再根據f的真假輸出結果。

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e9;
const int maxm = 1e5 + 5;
const ll inf = 2147483647;
using namespace std;

int main() {
    int n;
    cin >> n;
    for (int j = 0; j < n; j++) {
        string s;
        if (j == 0)
            getchar();
        getline(cin, s);
        int t = s.length();
        int f = 0;
        if (s[t - 1] != '?') {
            cout << "enen\n";
        } else {
            for (int i = 2; i < t; i++) {
                if (s[i] == 'A' && s[i - 1] == 'T' && s[i - 2] == 'P') {
                    f = 1;
                    break;
                }
            }
            if (f)
                cout << "Yes!\n";
            else cout << "No.\n";
        }
    }
    return 0;
}

 

7-8 完美對稱

從頭到尾開始判斷區間是否對稱,不對稱頭就順移到下一位直到找到對稱區間,當頭等於第一位時就直接完整輸出,不是時倒序輸出頭前面區間。

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e9;
const int maxm = 1e5 + 5;
const ll inf = 2147483647;
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> v(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> v[i];
    }
    for (int i = 1; i <= n; i++) {
        int k = n, j = i, f = 1;
        while (k >= j) {//這里判斷是否對稱
            if (v[k] != v[j]) {
                f = 0;
                break;
            }
            k--;
            j++;
        }
        if (f && i != 1) {
            cout << v[i - 1];
            for (int p = i - 2; p >= 1; p--) {
                cout << ' ' << v[p];
            }
            break;
        } else if (f) {
            cout << v[1];
            for (int p = 2; p <= n; p++) {
                cout << ' ' << v[p];
            }
            break;
        }
    }
    return 0;
}

 


免責聲明!

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



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