-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass07_problem01.cpp
87 lines (76 loc) · 1.66 KB
/
class07_problem01.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class PROBLEM01
{
public:
//求得数字的长度
vector<string> S{ "W","Q","B","S","L" };
vector<string> S_19 = { "One ", "Two ", "Three ", "Four ", "Five ", "Six ",
"Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ",
"Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ",
"Eighteen ", "Nineteen " };
vector<string> S_0 = { "Twenty ", "Thirty ", "Forty ", "Fifty ",
"Sixty ", "Seventy ", "Eighty ", "Ninety " };
string str_res = "";
int pown(int N, int L)
{
int res = 1;
int temp = N;
while (L != 0)
{
if (L & 1 == 1)
{
res *= temp;
temp = temp * temp;
}
else
temp = temp * temp;
L = L >> 1;
}
return res;
}
//求出每位的数字,并添加单位变成字符串
string get_string(int N)
{
if (N <= 19)
return S_19[N-1];
else if (N / 10 / 10 == 0)
return S_0[N / 10 % 10 - 2];
//其他情况
int idx = 4;
int L = 0;//数字的长度
while (N / pown(10, idx) == 0)
{
idx--;
}
L = idx;
while (L >= 0)
{
int cur = N / pown(10, L);
char flag = 'L';
if (cur == 0 && str_res[str_res.size() - 1] == flag)
goto FFF;
else if (cur == N)
str_res += to_string(cur);
else if(cur==0)
str_res += S[4];
else
str_res += to_string(cur) + S[4 - L];
FFF:
N = N % pown(10,L);
L = L - 1;
}
return str_res;
}
};
int main()
{
int N = 19120;
PROBLEM01 P;
string str = P.get_string(N);
string new_str(str.begin(), str.end() - 1);
cout << ((str[str.size()-1] == '0') ? new_str : str )<< endl;;
return 0;
}