AOJ0017

Caesar Cipher | Aizu Online Judge

#include <iostream>

using namespace std;

int main() {
    string s;
    while (getline(cin, s)) {
        while (true) {
            for (int i = 0; i < s.length(); ++i) {
                if (s[i] == ' ' || s[i] == '.') {
                    continue;
                }
                if (s[i] == 'z') {
                    s[i] -= 26;
                }
                ++s[i];
            }

            if (s.find("the") != string::npos || s.find("this") != string::npos || s.find("that") != string::npos) {
                cout << s << endl;
                break;
            }
        }

    }

    return 0;
}

競技プログラミングに関するメモ

C++

[C++] STLの型の使い分け - Qiita

競技プログラミングの入力メモ - 2冊の本を3等分

【C++】入出力のメモ - 緑茶思考ブログ

Python

Python3で競技プログラミングめも - くれなゐの雑記

Pythonで競技プログラミングする時に知っておきたいtips(入出力編) - Qiita

AOJ0016

Treasure Hunt | Aizu Online Judge

C++ だと,円周率ってどこに定義されているんだろう?

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    const double PI = static_cast<double>(acos(-1.0));

    pair<double, double> pos = {0.0, 0.0};
    int angle = 90;

    int d, a;
    while (1) {
        cin >> d;
        cin.ignore();
        cin >> a;

        if (d == 0 && a == 0) {
            break;
        }

        pos.first += cos(static_cast<double>(angle) / 180.0 * PI) * d;
        pos.second += sin(static_cast<double>(angle) / 180.0 * PI) * d;

        angle += a;
    }

    cout << -static_cast<int>(pos.first) << endl << static_cast<int>(pos.second) << endl;

    return 0;
}

AOJ0015

National Budget | Aizu Online Judge

boost::multiprecision::cpp_int 的なものを実装する必要あり.

「入力される整数は 100 桁を超えない」ということに注意する.

#include <iostream>

using namespace std;

struct big_int {
    static const int CAPACITY = 80;
    int digits[CAPACITY] = {0};

    big_int() {}
    big_int(const string &s) {
        if (s.length() > this->size()) {
            throw overflow_error("overflow");
        }
        for (int i = 0; i < s.length(); ++i) {
            // convert the character to integer
            digits[i] = (s[s.length() - 1 - i] - '0') % 10;
        }
    }

    static size_t size() {
        return CAPACITY;
    }

    big_int operator+(const big_int &rhs) {
        big_int r;
        bool carry = 0;
        for (int i = 0; i < r.size(); ++i) {
            int t = this->digits[i] + rhs.digits[i] + carry;
            carry = (t / 10) > 0;
            r.digits[i] = t % 10;
            if (carry == 1 && i == r.size() - 1) {
                throw overflow_error("overflow");
            }
        }
        return r;
    }
};

ostream& operator<<(ostream &o, const big_int &a) {
    bool skip = true;
    for (size_t i = 0; i < a.size(); ++i) {
        if (a.digits[a.size() - 1 - i] != 0 || i == a.size() - 1) {
            skip = false;
        }
        if (!skip) {
            o << a.digits[a.size() - 1 - i];
        }
    }
    return o;
}

int main() {
    int n;
    cin >> n;

    for (int i = 0; i < n; ++i) {
        try {
            string str_a, str_b;
            cin >> str_a >> str_b;
            big_int a(str_a), b(str_b);
            cout << a + b << endl;
        } catch (const overflow_error& e) {
            cout << e.what() << endl;
        }
    }

    return 0;
}