标签:
I changed the integer into string. If the length is not multiple of 3, append "0" ahead. Then, deal three digits by chunks.
#include <iostream> #include <string> #include <vector> using namespace std; /* Convert a non-negative integer to its English words representation. Given input is guaranteed to be less then 2 ^ 31 - 1. For example: 123 ---> "One Hundred Twenty Three" 12345 --> "Twelve Thousand Three Hundred Forty Five" 1234567 --> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" Seems the best way is to cut the number by 3-digits. 1234567 --> 1, 234, 567 */ vector<string> digit_1{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Tweleve", "Thirteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; vector<string> digit_2{"Twenty", "Thrity", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety", "Hundred"}; vector<string> digit_3{"", "Thousand", "Million", "Billion"}; string threeDigits(string tmp) { string res = ""; int n = tmp.size(); if(n == 1) return digit_1[tmp[2] - '0']; else if(n == 2){ if(tmp[1] >= '2') return res = digit_2[tmp[1] - '0' - 2] + " " + digit_1[tmp[2] - '0']; else { int number = (tmp[1] - '0') * 10 + tmp[2] - '0'; return res = digit_1[number]; } } else { if(tmp[0] != '0') { res = res + digit_1[tmp[0] - '0'] + " " + "Hundred"; if(tmp[1] >= '2') res = res + " " + digit_2[tmp[1] - '0' - 2] + " " + digit_1[tmp[2] - '0']; else { int number = (tmp[1] - '0') * 10 + tmp[2] - '0'; res += " " + digit_1[number]; } } else { if(tmp[1] >= '2') res = res + digit_2[tmp[1] - '0' - 2] + " " + digit_1[tmp[2] - '0']; else { int number = (tmp[1] - '0') * 10 + tmp[2] - '0'; res += " " + digit_1[number]; } } } return res; } string numberToWords(int num) { string tmp = to_string(num); int i = 0; int count = 0; if(tmp.size() % 3) count = 3 - (tmp.size() % 3); string res = ""; for(int k = 0; k < count; ++k) { tmp = "0" + tmp; } int digits = tmp.size() / 3; count = tmp.size() / 3; while(i + 3 <= tmp.size()) { string a = threeDigits(tmp.substr(i, 3)) + " " + digit_3[--count]; i = i + 3; res = res + " " + a; } return res; } int main(void) { string res = numberToWords(1200); cout << res << endl; }
LeetCode 273. Integer to English Words
标签:
原文地址:http://blog.csdn.net/github_34333284/article/details/51360235