【題解】ZeroJudge a741: 10101 – Bangla Numbers

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

  • 想定義數字和名稱,方便處理
    • a[] = {10000000, 100000, 1000, 100};
    • s[] = {“kuti”, “lakh”, “hajar”, “shata”};
  • 測資可能大於10000000,需循環處理。(Line 20~23)
  • 測資可能為 0 (Line-41)。
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
int a[] = {10000000, 100000, 1000, 100};
string s[] = {"kuti", "lakh", "hajar", "shata"};
string ans;

string num2str(ll x){
    string s = "";
    while (x){
        s += '0' + (x % 10);
        x /= 10;
    }
    reverse(s.begin(), s.end());
    return s;
}

void solve(ll x){
    if (x >= a[0]){
        solve(x / a[0]);
        ans += " " + s[0];
        x %= (int)a[0];
    }
    for (int i = 0; i < 4; i++){
        if (x / a[i] > 0){
            ans += " " + num2str(x / a[i]) + " " + s[i];
            x %= a[i];
        }
    }
    if (x > 0) ans += " " + num2str(x);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll N;
    int Case = 1;
    while (cin >> N){
        cout << "   " << Case++ << ".";
        if (N == 0) cout << " 0\n";
        else {
            ans = "";
            solve(N);
            cout << ans << "\n";
        }
    }
    return 0;
}
分享本文 Share with friends