程序员面试金典——解题总结: 9.17中等难题 17.7给定一个整数,打印该整数的英文描述(例如 "One Thousand,Two Hundred-Thirty Four")

来源:互联网 发布:php mysql web 编辑:程序博客网 时间:2024/05/12 13:16
#include <iostream>#include <stdio.h>#include <string>#include <vector>#include <sstream>#include <stack>using namespace std;/*问题:给定一个整数,打印该整数的英文描述(例如 "One Thousand,Two Hundred-Thirty Four")分析:等同于一个有规则的翻译。      3Three  34Thirty Four  234Two Hundred-Thirty Four【注意百和十位数之间用 “-”】  1234One Thousand,Two Hundred-Thirty Four【注意千和百位数之间用“,”】  51234Fifty One Thousand,Two Hundred-Thirty Four【注意万位数和千位数放在一起构成多少个千】  651234Six Hundred-Fifty One Thousand,Two Hundred-Thirty Four【十万位和万位数,千位数放在一起,构成百位数中间百位数和十位数用“-”连接】  7651234Seven Million,Six Hundred-Fifty One Thousand,Two Hundred-Thirty Four            【到百万,就需要把百万位单独拎出来和Million组织在一起,其余部分和几十万的处理相同】  87651234  Eighty Seven Million,Six Hundred-Fifty One Thousand,Two Hundred-Thirty Four 【千万位和百万位拎出来组成十位数并拼接Million】  987651234Nine Hundred-Eighty Seven Million,Six Hundred-Fifty One Thousand,Two Hundred-Thirty Four  【亿位,千万位,百万位拎出来组成百位数,组成后的百位后拼接 Hunderd- , 组成后的百十个位后拼接Million】  1987651234 One Billion,Nine Hundred-Eighty Seven Million,Six Hundred-Fifty One Thousand,Two Hundred-Thirty Four  【十亿位单独拎出来,后拼接Billion】    总结规律:将提供的数字,计算出总 的位数digitNum , 计算位数 digitNum / 3 = groupNum,对每个组以3位进行划分:  百位数:  十万数到千位数:以百位数的记数方式后拼接“Thousand”  亿数到百万数:以百位数的记数方式后拼接“Million”  十亿数及以上:后拼接"Billion"  groupNum = 0:  十位数:  百位数:单独提取出百位上数字拼接"Hudred-"输入:1234651234987651234输出:One Thousand,Two Hundred-Thirty FourSix Hundred-Fifty One Thousand,Two Hundred-Thirty FourNine Hundred-Eighty Seven Million,Six Hundred-Fifty One Thousand,Two Hundred-Thirty Four关键:1   总结规律:将提供的数字,计算出总 的位数digitNum , 计算位数 digitNum / 3 = groupNum,对每个组以3位进行划分:  百位数:  十万数到千位数:以百位数的记数方式后拼接“Thousand”  亿数到百万数:以百位数的记数方式后拼接“Million”  十亿数及以上:后拼接"Billion"2 string numbers[] = {"Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" };//10~19string teens[] = {"Ten" ,"Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen" };//20,30,..,90string tens[] = {"Twenty" , "Thirty" , "Fourty" , "Fifty" , "Sixty" , "Seventy" , "Eighty" , "Ninety"};string bigs[] = {"" , "Thousand" , "Million" , "Billion"};3while(num){//string bigs[] = {"" , "Thousand" , "Million" , "Billion"},如果该整数 > 1000 Billion,那么后续就直接把多余的部分翻译出来,再拼接Billionif(count >= 4){result = translateNum(num);resultVector.push_back(result);break;}remainNum = num % 1000;num /= 1000;result = translateNum(remainNum) + " " + bigs[count];resultStack.push(result);count++;}*/const int MAXSIZE = 10;//个位数string numbers[] = {"Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" };//10~19string teens[] = {"Ten" ,"Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen" };//20,30,..,90string tens[] = {"Twenty" , "Thirty" , "Fourty" , "Fifty" , "Sixty" , "Seventy" , "Eighty" , "Ninety"};string bigs[] = {"" , "Thousand" , "Million" , "Billion"};//获取某个数在某一位上的值,如果该位没有,返回0int getDigitValue(int num , int digitNum){int count = 0;int value = 0;do{count++;value = num % 10;num /= 10;if(count == digitNum){return value;}}while(num);//走到这里说明整数num没有digitNum位,直接返回0return 0;}//翻译<1000的数字string translateNum(int num){stringstream resultStream;if(num >= 1000){cout << num << "is wrong parameter , it must less than one thousand" << endl;return "";}//如果大于100,就提取出百位数int hundredDigit = 0;if(num >= 100){hundredDigit = getDigitValue(num, 3);resultStream << numbers[hundredDigit] << " Hundred-";num %= 100; //获取十位和个位}//如果该if(hundredDigit != 0){}//如果是个位数if(num < 10){resultStream << numbers[num];}//如果是11~19else if(10 <= num && num <= 19){int temp = num % 10;resultStream << numbers[temp -1];}//如果是》=20else if( 20 <= num && num <= 99 ){//确定十位数int value = getDigitValue(num , 2);resultStream << tens[value - 2] ;//个位数value = getDigitValue(num ,1);resultStream << " " << numbers[value];}else{cout << num << " is wrong" << endl;}return resultStream.str();}//将数字每次除以1000,并依次加上,"" , "Thousand" , "Million" , "Billion",注意如果除以Billion剩余的数比如超过1000,那么把 剩余的部分翻译好后 拼接Billionstring translate(int num){stringstream resultStream;stack<string> resultStack;if(num < 0){resultStack.push("Negative ") ;num *= (-1);}//对数字每次除以1000,然后翻译,先翻译低位,再翻译高位,最后int remainNum ;int count = 0;string result;vector<string> resultVector;while(num){//string bigs[] = {"" , "Thousand" , "Million" , "Billion"},如果该整数 > 1000 Billion,那么后续就直接把多余的部分翻译出来,再拼接Billionif(count >= 4){result = translateNum(num);resultVector.push_back(result);break;}remainNum = num % 1000;num /= 1000;result = translateNum(remainNum) + " " + bigs[count];resultStack.push(result);count++;}if(!resultVector.empty()){for(vector<string>::iterator it = resultVector.begin() ; it != resultVector.end() ; it++){resultStream << (*it) << " ";}resultStream << "Billion,";}//将结果输出string value;while(!resultStack.empty()){value = resultStack.top();resultStack.pop();resultStream << value << ",";}//删除最后一个逗号string realResult = resultStream.str();if(',' == realResult.at(realResult.length() - 1)){realResult = realResult.substr(0 , realResult.length() - 1);}return realResult;}void process(){int num;string result;while(cin >> num){result = translate(num);cout << result << endl;}}int main(int argc , char* argv[]){process();getchar();return 0;}

0 0