每日编程-20170315

it2022-05-06  10

题目描述:Jessi初学英语,为了快速读出一串数字,编写程序将数字转换成英文:如22:twenty two,123:one hundred and twenty three。说明:数字为正整数,长度不超过九位,不考虑小数,转化结果为英文小写;输出格式为twenty two;非法数据请返回“error”;关键字提示:and,billion,million,thousand,hundred。

输入描述:输入一个long型整数

输出描述:输出相应的英文写法

输入例子:2356

输出例子:two thousand three hundred and fifty six

解答:

主要在于分析,英文数字是每3位一组的,

1-3位是几百几十几,

4-6位是几百几十几k,

6-9位是几百几十几million,

如果长度不超过9,用不上billion

所以使用一个函数输出几百几十几,4-6加上thousand,6-9加上billion就好

11-19有对应的单词,存起来就好。

#include <iostream> #include <string> #include <vector> using namespace std; vector<vector<string>> dict{ { "one", "two", "three" , "four" , "five" , "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen" , "fourteen" , "fifteen" , "sixteen", "seventeen", "eighteen", "nineteen"},  //dict[0]有19个元素 { "twenty", "tirty", "fourty" , "fifty" , "sixty" , "senevnty" , "eighty" , "ninety"}}; vector<string> answer; void three0(int n) { string s; if ((n % 100 > 20))  //后两位20以上的要使用dict[0]和dict[1]组合 { answer.push_back(dict[0][(n % 10) - 1 ]); answer.push_back(dict[1][((n / 10) % 10) - 2]); } else { answer.push_back(dict[0][n - 1]);  //后两位20以下的,直接查dict[0] } if (n > 99) { answer.push_back("hundred and"); answer.push_back(dict[0][(n / 100) % 10 -1]); } } int main() { long number; cin >> number; if (number > 999999999)  //检查 { cout << "error" << endl; return 0; } three0(number); if (number/1000) { number /= 1000; answer.push_back("thousand"); three0(number); } if (number / 1000) { number /= 1000; answer.push_back("million"); three0(number); } for (auto end = answer.end(); end != answer.begin();) { --end; cout << *end << " "; } cout << endl; }

 上一个版本错误百出,还在那洋洋得意,让大神教训了

这个版本目前还没有试出来Bug,我回头再想想

1 #include <iostream> 2 #include <string> 3 #include <vector> 4 using namespace std; 5 vector<vector<string>> dict{ 6 { "one", "two", "three" , "four" , "five" , "six", "seven", "eight", "nine", 7 "ten", "eleven", "twelve", "thirteen" , "fourteen" , "fifteen" , "sixteen", "seventeen", 8 "eighteen", "nineteen"}, 9 { "twenty", "tirty", "fourty" , "fifty" , "sixty" , "senevnty" , "eighty" , "ninety"}}; 10 vector<string> answer; 11 long number; 12 void three0(int n) { 13 n %= 1000; 14 if (n != 0) 15 { 16 if ((n % 100 > 20) && ((n % 100) != 0)) 17 { 18 answer.push_back(dict[0][(n % 10) - 1]); 19 answer.push_back(dict[1][((n / 10) % 10) - 2]); 20 if (number > 999){ answer.push_back("and"); } 21 } 22 else 23 { 24 if ((n % 100) != 0) 25 { 26 answer.push_back(dict[0][(n % 100) - 1]); 27 if (number > 999) { answer.push_back("and"); } 28 } 29 } 30 if (n > 99) 31 { 32 answer.push_back("hundred"); 33 answer.push_back(dict[0][(n / 100) % 10 - 1]); 34 } 35 } 36 } 37 int main() { 38 cin >> number; 39 if (number > 999999999) 40 { 41 cout << "error" << endl; 42 return 0; 43 } 44 if ((number% 1000) != 0) 45 { 46 three0(number); 47 } 48 number /= 1000; 49 if ((number% 1000) != 0) 50 { 51 answer.push_back("thousand"); 52 three0(number); 53 } 54 if (number / 1000) 55 { 56 number /= 1000; 57 answer.push_back("million"); 58 three0(number); 59 } 60 for (auto end = answer.end(); end != answer.begin();) 61 { 62 --end; 63 cout << *end << " "; 64 } 65 cout << endl; 66 }

 

转载于:https://www.cnblogs.com/linhaowei0389/p/6556163.html

相关资源:Linux高性能服务器编程-高清-pdf

最新回复(0)