【題解】ZeroJudge d318: 11185 – Ternary

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d318
【解題想法】

  • N = 0 時,雖不需計算,但要記得輸出一個 0。
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int N;
    while (cin >> N){
        if (N < 0) break;
        if (N == 0) cout << "0\n";
        string ans = "";
        while (N){
            ans += '0' + (N % 3);
            N /= 3;
        }
        reverse(ans.begin(), ans.end());
        cout << ans << "\n";
    }
}
分享本文 Share with friends