CPE 一顆星選集 49 道必考題
題目連結:https://zerojudge.tw/ShowProblem?problemid=a741
內容
Bangla numbers 與十進制配對如下。
‘kuti’ (10000000), ‘lakh’ (100000), ‘hajar’ (1000), ‘shata’ (100)
給定一十進位數字 n (0 <= n < 10^15),輸出其對應的Bangla numbers。
範例輸入
23764
45897458973958
範例輸出
1. 23 hajar 7 shata 64
2. 45 lakh 89 hajar 7 shata 45 kuti 89 lakh 73 hajar 9 shata 58
想法
如果 n > 1 kuti,就把 n 分兩半,像是 45897458973958 -> 4589745 8973958,接著從’kuti’依序除到’shata’。
用long long 存 n。
程式碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #include <bits/stdc++.h> #define fastio ios_base::sync_with_stdio(false), cin.tie(nullptr) using namespace std;
int main(){ fastio; int mp[] = {10000000, 100000, 1000, 100}; string s[] = {"kuti", "lakh", "hajar", "shata"}; long long n, temp; int c = 0; while(cin >> n){ cout << ++c << "."; if(n == 0){ cout << " 0\n"; continue; } if(n / mp[0] != 0){ temp = n / mp[0]; for(int i = 0; i < 4; ++i){ if(temp / mp[i] != 0){ cout << " " << temp / mp[i] << " " << s[i]; temp %= mp[i]; } } if(temp != 0) cout << " " << temp; cout << " " << s[0]; n %= mp[0]; } for(int i = 1; i < 4; ++i){ if(n / mp[i] != 0){ cout << " " << n / mp[i] << " " << s[i]; n %= mp[i]; } } if(n != 0) cout << " " << n; cout << "\n"; } }
|