算法題從初級到高級

大綱

入門

1001 害死人不償命的(3n+1)猜想

#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    int k = 0;
    while (n != 1) {
        if (n % 2 == 0) {
            n /= 2;
            k++;
        } else {
            n = 3 * n + 1;
        }
    }
    cout << k << endl;
    return 0;
}

1032 挖掘機技術哪家強 (20 分)

#include <iostream>
#include <unordered_map>

using namespace std;

int main() {
    int N;
    cin >> N;
    unordered_map<int, int> record;
    int school, score;
    for (int i = 0; i < N; ++i) {
        cin >> school >> score;
        record[school] += score;
    }
    school = 0, score = 0;
    for (pair<int, int> item: record) {
        if (item.second > score) {
            score = item.second;
            school = item.first;
        }
    }
    cout << school << " " << score << endl;
    return 0;
}

1934: 找x

#include <iostream>
#include <cstdio>

using namespace std;

const int MAX_N = 201;
int arr[MAX_N];

int main() {
    int n, x;
    while (cin >> n) {
        for (int i = 0; i < n; ++i)
            cin >> arr[i];
        cin >> x; // CLion下此處獲得 x 並不是用戶輸入的
        int j;
        for (j = 0; j < n; ++j) {
            if (arr[j] == x) {
                break;
            }
        }
        if (j < n)
            cout << j << endl;
        else
            cout << -1 << endl;
    }
    return 0;
}

注:此題在CLion運行可能會出錯,源於CLion的bug。

1036 跟奧巴馬一起編程 (15 分)

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    int N;
    char c;
    cin >> N >> c;
    for (int i = 0; i < N; ++i)
        cout << c;
    cout << endl;

    for (int i = 0; i < round(N / 2.0) - 2; ++i) { // 注意 2.0
        cout << c;
        for (int j = 0; j < N - 2; ++j)
            cout << " ";
        cout << c << endl;
    }

    for (int i = 0; i < N; ++i)
        cout << c;
    cout << endl;
    return 0;
}

1928: 日期差值

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int month[13][2] = {
        {0,  0}, {31, 31},
        {28, 29}, {31, 31},
        {30, 30}, {31, 31},
        {30, 30}, {31, 31},
        {31, 31}, {30, 30},
        {31, 31}, {30, 30},
        {31, 31}
};

int isLeap(int year) {
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        return 1;
    else
        return 0;
}

int main() {
    int time1, time2;
    while (cin >> time1 >> time2) {
        if (time1 > time2)
            swap(time1, time2);
        int y1 =time1/10000, m1 = time1%10000/100, d1 = time1%100;
        int y2 =time2/10000, m2 = time2%10000/100, d2 = time2%100;
        int ans = 1;
        while (y1 < y2 || m1 < m2 || d1 < d2) {
            d1++;
            if (d1 == month[m1][isLeap(y1)] + 1) {
                m1++;
                d1 = 1;
            }
            if (m1 == 13) {
                y1++;
                m1 = 1;
            }
            ans++;
        }
        cout << ans << endl;
    }
    return 0;
}

1022 D進制的A+B (20 分)

#include <iostream>
#include <cstdio>
#include <stack>

using namespace std;

int main() {
//    freopen("input.txt", "r", stdin);
    int A, B, D;
    cin >> A >> B >> D;
    int sum = A + B;

    stack<int> S;
    do {
        int r = sum % D;
        S.push(r);
        sum /= D;
    } while (sum);

    while (!S.empty()) {
        cout << S.top();
        S.pop();
    }
    cout << endl;
    return 0;
}

注:將 p 進制的 x 轉爲 10 進制

int decimal(int x, int p) {
    int res = 0, product = 1;
    while (x) {
        res += (x % 10) * product;
        x /= 10;
        product *= p;
    }
    return res;
}

5901: 【字符串】迴文串

#include <iostream>

using namespace std;

bool palindrome(string &str) {
    int l = 0, r = str.size() - 1;
    while (l < r) {
        if (str[l] != str[r])
            return false;
        l++;
        r--;
    }
    return true;
}

int main() {
    string str;
    cin >> str;
    if (palindrome(str))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    return 0;
}

1009 說反話 (20 分)

#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

vector<string> split(string &str) {
    vector<string> words;
    string s;
    for (int i = 0; i < str.size(); i++)
        if (str[i] == ' ') {
            if (s != "") {
                words.push_back(s);
                s = "";
            }
        } else {
            s += str[i];
        }
    if (s != "")
        words.push_back(s);
    return words;
}

int main() {
    // freopen("input.txt", "r", stdin);
    string str;
    getline(cin, str);
    vector<string> words = split(str);
    for (int i = words.size() - 1; i >= 0; i--) {
        cout << words[i];
        if (i)
            cout << " ";
    }
    return 0;
}

未完待續 …

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章