如內容有誤也請不吝賜教 # [題目連結](https://zerojudge.tw/ShowProblem?problemid=a741) # 題目意思 將一個數字把它用那些字串表示出來 ex: 23764 -> 23 個1000 + 7個 100 + 64 -> 23 hajar 7 shata 64 如果輸入的數字比 10000000 還大 ,ex: 25670000000 不能直接 2567 kuti , 2567 要再細分一次(在做一次那些過程) 數字要控制輸出4格 ex: ___6 , __23, _100 (要上傳到uva的話,這裡測資很鬆) "另外數字可以為 0 " ***-by vlva8166@gmail.com (風行)*** ![image](https://hackmd.io/_uploads/r18S0SpUle.png) # 想法 如果n=0直接輸出0 否則依照2 1 2 2(每個單位可輸出數字的最多位數)的順序把數字拆開,檢查是不是0,如果不是就輸出 注意:超過10000000的話是一定要輸出kuti,不能省略 Ex:輸入 460000409000 467000408900 467800456780 100000000000000 999999999999999 0 輸出 1. 46 hajar kuti 4 lakh 9 hajar 2. 46 hajar 7 shata kuti 4 lakh 8 hajar 9 shata 3. 46 hajar 7 shata 80 kuti 4 lakh 56 hajar 7 shata 80 4. 1 kuti kuti 5. 9 kuti 99 lakh 99 hajar 9 shata 99 kuti 99 lakh 99 hajar 9 shata 99 6. 0 ![image](https://hackmd.io/_uploads/HJkZwL6Ile.png) ***-by z3x56 (二信阿資)*** # 程式碼 以下3種方法的邏輯跟上面說的都是一樣的,只是用的方法不同而已 btw雖然題目說要對齊4格什麼的,但是我沒加實測也過了,可能是測資真的很鬆的關係 如果想要對齊的就把 ```cpp cout << t++ << "."; ``` 改成 ```cpp cout << setw(4) << t++ << "."; ``` ## 1.直接遞迴(42ms, 304KB) ```cpp=1 #include <bits/stdc++.h> using namespace std; void bangla(long long n); int main() { ios::sync_with_stdio(0); cin.tie(0); long long n; int t = 1; while (cin >> n) { cout << t++ << "."; if (n == 0) cout << " 0"; else bangla(n); cout << endl; } } void bangla(long long n) { if (n >= 10000000) { bangla(n / 10000000); cout << " kuti"; n %= 10000000; } if (n >= 100000) { cout << " " << n / 100000 << " lakh"; n %= 100000; } if (n >= 1000) { cout << " " << n / 1000 << " hajar"; n %= 1000; } if (n >= 100) { cout << " " << n / 100 << " shata"; n %= 100; } if (n > 0) { cout << " " << n; } } ``` ## 2.long long 處理 (50ms, 324KB) ```cpp=1 #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); string S[4]= { "shata",// 100 "hajar",// 1000 "lakh", // 100000 "kuti", // 10000000 }; int A[4]= {100,10,100,100}; int t=1; long long n; while(cin >> n) { if(n==0) { cout << t++ << ". "<< n <<endl; continue; } vector<int> v ; int a=0; while(n>0) { v.push_back(n%A[a]); n/=A[a]; a=(a+1)%4; } cout << t++ << "."; for (int i = v.size() - 1; i > 0; --i) { int idx = (i - 1) % 4; if (idx == 3) { cout << " "; if (v[i]) cout << v[i] << " "; cout << S[3]; continue; } if (v[i]) cout << " " << v[i] << " " << S[idx]; } if(v[0])cout << " " << v[0] ; cout << endl; } } ``` ## 3.string 處理 (48ms, 328KB) ```cpp=1 #include<bits/stdc++.h> using namespace std; void StrProcess(vector<int> &v,string str); int main() { ios::sync_with_stdio(0);cin.tie(0); string S[4]= { "shata",// 100 "hajar",// 1000 "lakh", // 100000 "kuti", // 10000000 }; int t=1; string n; while(cin >> n) { if(n=="0") { cout << t++ << ". "<< n <<endl; continue; } vector<int> v; StrProcess(v,n); cout << t++ << "."; for (int i = v.size() - 1; i > 0; --i) { int idx = (i - 1) % 4; if (idx == 3) { cout << " "; if (v[i]) cout << v[i] << " "; cout << S[3]; continue; } if (v[i]) cout << " " << v[i] << " " << S[idx]; cout << " " << v[i] << " " << S[(i-1)%4]; } if (v[0]) cout << " " << v[0]; cout << endl; } } void StrProcess(vector<int> &v,string str) { int A[4]= {2,1,2,2}; int t=0,temp=0; int total=0; for(int i=str.size()-1; i>=0; i--) { total+=(str[i]-'0')* (temp ? 10 : 1); temp++; if(temp==A[t] or i==0) { v.push_back(total); t=(t+1)%4; temp=0; total=0; } } } ``` ### 改良 經過GPT提點,副函式StrProcess可以改成這樣 (37ms, 336KB) ```cpp=40 void StrProcess(vector<int> &v, string str) { int A[4] = {2, 1, 2, 2}; int t = 0; for (int i = str.size(); i > 0;) { int len = A[t]; if (i - len < 0) len = i; v.push_back(stoi(str.substr(i - len, len))); i -= len; t = (t + 1) % 4; } } ``` ## 疑問(希望有人懂的話可以指點一下) 為什麼long long的純數字運算會比string的字串處理慢