String-mainipulation10

题目描述[原题链接][https://leetcode-cn.com/problems/integer-to-english-words/]

将非负整数转换为其对应的英文表示。可以保证给定输入小于 2的31次方 - 1

示例 1:

输入: 123
输出: “One Hundred Twenty Three”

示例 2:

输入: 12345
输出: “Twelve Thousand Three Hundred Forty Five”

示例 3:

输入: 1234567
输出: “One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven”

示例 4:

输入: 1234567891
输出: “One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One”

算法描述

​ 考察英文水平><___

C++代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Solution {
public:
int hundred = 100, thousand = 1000, million = 1000000, billion = 1000000000;
unordered_map<int, string> numbers;

string numberToWords(int num) {
char number20[][30] = {"Zero", "One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
"Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen"};
char number2[][30] = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty",
"Seventy", "Eighty", "Ninety"};
for (int i = 0; i < 20; i ++ ) numbers[i] = number20[i];
for (int i = 20, j = 0; i < 100; i += 10, j ++ ) numbers[i] = number2[j];
numbers[hundred] = "Hundred", numbers[thousand] = "Thousand";
numbers[million] = "Million", numbers[billion] = "Billion";
string res;
for (int k = 1000000000; k >= 100; k /= 1000)
{
if (num >= k)
{
res += ' ' + get3(num / k) + ' ' + numbers[k];
num %= k;
}
}
if (num) res += ' ' + get3(num);
if (res.empty()) res = ' ' + numbers[0];
return res.substr(1);
}

string get3(int num)
{
string res;
if (num >= hundred)
{
res += ' ' + numbers[num / hundred] + ' ' + numbers[hundred];
num %= hundred;
}
if (num)
{
if (num < 20) res += ' ' + numbers[num];
else if (num % 10 == 0) res += ' ' + numbers[num];
else res += ' ' + numbers[num / 10 * 10] + ' ' + numbers[num % 10];
}
return res.substr(1);
}
};

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Solution {
public String numberToWords(int num) {
if(num == 0){
return "Zero";
}
String[] cardinal = {""," Thousand"," Million"," Billion"};
List<String> list = new ArrayList<>();
int count =0;
StringBuffer english = new StringBuffer() ;
while(num!=0){
list.add(hundredNumber(num%1000));
count ++;
num = num / 1000;
}
while(count>0){
english.append(list.get(count-1));
if(!list.get(count-1).equals("")){
english.append(cardinal[count-1]);
}
count --;
}
String col = english.toString();
return col.substring(1);
}

public static String hundredNumber(int num) {
String[] single = {" One"," Two"," Three",
" Four"," Five"," Six"," Seven",
" Eight"," Nine"};
String[] tenToTwenty = {" Eleven"," Twelve",
" Thirteen"," Fourteen"," Fifteen",
" Sixteen"," Seventeen"," Eighteen"," Nineteen"};
String[] tens = {" Ten"," Twenty"," Thirty"," Forty",
" Fifty"," Sixty"," Seventy",
" Eighty"," Ninety"};
String hun = " Hundred";

StringBuffer english = new StringBuffer() ;

int a = num % 10;
int b = num % 100;
int c = num % 1000;
if(c!=b && c!=a) {
english.append(single[c / 100-1]);
english.append(hun);
}

if(b!=a){
if(10 < b && b < 20){
english.append(tenToTwenty[b%10-1]);
}else {
english.append(tens[b/10-1]);
}
}

if(a != 0 ){
if(!(10 < b && b < 20)){
english.append(single[a-1]);
}
}
return english.toString();
}
}