a741 - 10101 - Bangla Numbers
題目描述
題目要求將一個非負整數轉換成孟加拉數字的文字表示形式。孟加拉數字使用 'kuti' (10000000), 'lakh' (100000), 'hajar' (1000), 'shata' (100) 作為單位。
解題思路
程式碼首先讀取輸入的數字,然後將其轉換為字串。為了方便處理,程式碼會將字串的長度調整為 7 的倍數,在字串前面填充 '0'。接著,程式碼以 7 個字符為一組,從字串中提取數字,並將其轉換為孟加拉數字的表示形式。每個 7 位數組表示 kuti, lakh, hajar, shata 的組合。最後,程式碼輸出轉換後的孟加拉數字字串。
複雜度分析
- 時間複雜度: O(n),其中 n 是輸入數字的位數。
- 空間複雜度: O(n),主要用於儲存輸入數字的字串表示形式。
程式碼
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
int main(){
cin.tie(0); ios::sync_with_stdio(false);
int ca=0;
string a;
while(cin >> a){
++ca;
int k,l,h,s,num,al=a.length(),start=0;
while(al%7!=0){
a='0'+a;
++al;
}
a="00"+a;
cout << setw(4) << ca << ".";
for(int i=0;i<al;i+=7){
k=(a[i]-'0')*10+(a[i+1]-'0');
if(k){
cout << " " << k << " kuti";
start=1;
}
else if(start){
cout << " kuti";
}
l=(a[i+2]-'0')*10+(a[i+3]-'0');
if(l){
cout << " " << l << " lakh";
start=1;
}
h=(a[i+4]-'0')*10+(a[i+5]-'0');
if(h){
cout << " " << h << " hajar";
start=1;
}
s=(a[i+6]-'0');
if(s){
cout << " " << s << " shata";
start=1;
}
}
num=(a[al]-'0')*10+a[al+1]-'0';
if(num||!start)
cout << " " << num;
cout << "\n";
}
}