Time limit: 3 seconds Space limit: 32768K heat index: 325 subject Knowledge Point: Programming basic Algorithm Knowledge Video explanation topic description
There is a non-negative integer, write an algorithm that prints the English description of the integer.
Given an int x, return a string that describes the integer in English. Test Sample:
1234
Back: "One thousand,two hundred thirty Four"
Train of thought: This problem is not difficult, but very troublesome, want 1.1 points to think clearly can write
The code is as follows:
Class ToString {public:string tostring (int x) {string base[20] = {"", "one", "two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", " Thirteen "," fourteen "," fifteen "," Sixteen "," Seventeen ", "Eighteen", "Nineteen"}; //0~19 string tyNum[10] = {"", "
"," Twenty "," Thirty "," Forty "," Fifty "," Sixty "," Seventy "," Eighty "," Ninety "};
string Bignum[4] = {"", "Thousand", "Million", "Billion"};
string Res;
int k=0; //The X per 3-digit number into a group for encoding while (x>0) {
int val = x%1000; //encodes every 3-digit number
string temp; if (base[val/100]!= "") {
temp+=base[val/100]; temp+= "Hundred
"; } if (VAL%100<20) { //number less than 20 separate code
if (!temp.empty ()) temp+= ""; temp+=base[val
%100]; }else{
if (!temp.empty ()) temp+= ""; temp+=tynum[
VAL%100/10]; if (base[val%10 ]!= "") {
if (!temp.empty ()) temp+= "";
temp+=base[val%10]; } &NBSP;&NBSP;&NBSP} if (!temp.empty () &&bignum[k]!= "") {
temp+= ""; temp+=bignum[k
]; if (!res.empty (
) temp+= ","; }
res = Temp+res;
k++;
x/= 1000;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} return Res; }
};
Do not understand can add my QQ group: 261035036 (IT programmer interview Treasure
Group) Welcome your arrival Oh, see the blog to point Footprints Bai, thank you ~ ~