-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path273.cpp
34 lines (31 loc) · 1.44 KB
/
273.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
//
// 273.cpp
// leetcode
//
// Created by R Z on 2018/7/22.
// Copyright © 2018年 R Z. All rights reserved.
//
#include <stdio.h>
#include <string>
using namespace std;
class Solution {
public:
static string numberToWords(int n) {
if(n == 0) return "Zero";
else return int_string(n).substr(1);
}
private:
static const char * const below_20[];
static const char * const below_100[];
static string int_string(int n) {
if(n >= 1000000000) return int_string(n / 1000000000) + " Billion" + int_string(n - 1000000000 * (n / 1000000000));
else if(n >= 1000000) return int_string(n / 1000000) + " Million" + int_string(n - 1000000 * (n / 1000000));
else if(n >= 1000) return int_string(n / 1000) + " Thousand" + int_string(n - 1000 * (n / 1000));
else if(n >= 100) return int_string(n / 100) + " Hundred" + int_string(n - 100 * (n / 100));
else if(n >= 20) return string(" ") + below_100[n / 10 - 2] + int_string(n - 10 * (n / 10));
else if(n >= 1) return string(" ") + below_20[n - 1];
else return "";
}
};
const char * const Solution::below_20[] = {"One", "Two", "Three", "Four","Five","Six","Seven","Eight","Nine","Ten", "Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
const char * const Solution::below_100[] = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};