1 条题解
-
0
C++ :
#include <iostream> #include<map> #include<string> #include<utility> using namespace std; map<int, string> m = { make_pair(1,"one"), make_pair(2,"two"), make_pair(3,"three"), make_pair(4,"four"), make_pair(5,"five"), make_pair(6,"six"), make_pair(7,"seven"), make_pair(8,"eight"), make_pair(9,"nine"), make_pair(10,"ten"), make_pair(11,"eleven"), make_pair(12,"nine"), make_pair(13,"thirteen"), make_pair(14,"fourteen"), make_pair(15,"fifteen"), make_pair(16,"sixteen"), make_pair(17,"seventeen"), make_pair(18,"eighteen"), make_pair(19,"nineteen"), make_pair(20,"twenty"), make_pair(30,"thirty"), make_pair(40,"forty"), make_pair(50,"fifty"), make_pair(60,"sixty"), make_pair(70,"seventy"), make_pair(80,"eighty"), make_pair(90,"ninety") }; string speak(unsigned long long n) { string s; bool b; if (n / 100) { s += ' ' + m[n / 100] + " hundred"; n %= 100; b = true; } if (n / 10 > 1) { if (b) s += " and"; s += ' ' + m[n / 10 * 10]; n %= 10; } if (n / 10 == 1) { if (b) s += " and"; s += ' ' + m[n]; n %= 1; } if (n) { s += ' ' + m[n]; n %= 1; } if (s.empty()) s += "zero"; else s.erase(0, 1); return s; } int main() { unsigned long long n; cin >> n; string s; if (n / 1000000000) s += (speak(n / 1000000000) + " billion "); n %= 1000000000; if (n / 1000000) s += (speak(n / 1000000) + " million "); n %= 1000000; if (n / 1000) s += (speak(n / 1000) + " thousand "); n %= 1000; s += speak(n); cout << s << endl; return 0; }Python :
def two(x): if x[0]=='0': return dic[int(x[1])] elif x[0]=='1': return dic_teen[int(x[1])] else: return dic_ty[int(x[0])]+" "+dic[int(x[1])] def three(x): if x=="": return "" else: if x[0]=='0': return two(x[1:]) else: return dic[int(x[0])]+"hundred and "+two(x[1:])+" " a=list(input())[::-1] dic={1:'one ',2:'two ',3:'three ',4:'four ',5:'five ',6:'six ',7:'seven ',8:'eight ',9:'nine ',0:""} dic_teen={0:'ten',1:'eleven',2:'twelve',3:'thirteen',4:'fourteen',5:'fifteen',6:'sixteen',7:'seventeen',8:'eighteen',9:'nineteen'} dic_ty={2:'twenty',3:'thirty',4:'forty',5:'fifty',6:'sixty',7:'seventy',8:'eighty',9:'ninety'} tail={1:'trillion ',2:'billion ',3:'million ',4:'thousand ',5:''} while len(a)<15: a.append("0") a=a[::-1] num="" time=0 num_list=[] for i in a: num+=i time+=1 if time==3: num_list.append(num) time=0 num="" out="" time=1 for i in num_list: if three(i)!="": out+=three(i)+tail[time] time+=1 print (out)
- 1
信息
- ID
- 121
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者