LintCode 1305: Integer to English Words

1305. Integer to English Words

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 2^31 - 1.

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"

Input test data (one parameter per line)How to understand a testcase?

解法1:

参考的网上思路。我觉得这个解法很清晰。

注意:
1) 不要用1e+9代替1000000000,或用1e+6代替1000000。因为C++把1e+9这样的科学计数法当成double型,所以num % 1e+9会有问题。
2) 最后的result前面会多个空格,要消掉。
3)         } else if (num >= 20) {
            return " " + tens[num / 10] + helper(num % 10);

不可写成return " " + tens[num / 10] + " " + digits[num % 10];
否则680901192会生成
"Six Hundred Eighty Zero Million Nine Hundred One Thousand One Hundred Ninety Two",多了一个Zero。

class Solution {
public:
    /**
     * @param num: a non-negative integer
     * @return: english words representation
     */
    string numberToWords(int num) {
        if (num == 0) return "Zero";
        string result = helper(num);
        
        return result.substr(1); //skip the beginning " "
    }
    
private:
    vector<string> digits = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    vector<string> tens = {"Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    
    string helper(int num) {
        if (num >= 1000000000) { //Billion 
            return helper(num / 1000000000) + " Billion" + helper(num % 1000000000);
        } else if (num >= 1000000) { //Million
            return helper(num / 1000000) + " Million" + helper(num % 1000000);
        } else if (num >= 1000) { //Thousand
            return helper(num / 1000) + " Thousand" + helper(num % 1000);
        } else if (num >= 100) { //Hundred
            return helper(num / 100) + " Hundred" + helper(num % 100);
        } else if (num >= 20) {
            return " " + tens[num / 10] + helper(num % 10);
        } else if (num >= 1) {
            return " "+ digits[num];
        } else {
            return "";
        }
    }
};

 

### std::to_integer 的用法与相关信息 `std::to_integer` 是 C++20 引入的一个模板函数,位于头文件 `<bit>` 中。它的主要功能是将一个枚举类型(`enum` 或 `enum class`)安全地转换为对应的整数类型。这种转换确保了类型安全,并且避免了潜在的未定义行为。 以下是 `std::to_integer` 的基本用法和注意事项: #### 1. 头文件包含 在使用 `std::to_integer` 之前,需要包含 `<bit>` 头文件[^5]。 ```cpp #include <bit> ``` #### 2. 基本语法 `std::to_integer` 的定义如下: ```cpp template<class T, class Enum> constexpr T to_integer(Enum e) noexcept; ``` - **参数**:`Enum e` 是要转换的枚举值。 - **返回值**:返回一个与枚举值相对应的整数值,类型为 `T`。 #### 3. 示例代码 以下是一个简单的例子,展示如何使用 `std::to_integer` 将枚举类型转换为整数类型[^5]。 ```cpp #include <iostream> #include <bit> // 定义一个强类型枚举 enum class Color { Red, Green, Blue }; int main() { Color c = Color::Green; // 使用 std::to_integer 将枚举转换为整数 int colorValue = std::to_integer<int>(c); std::cout << "The integer value of Color::Green is: " << colorValue << std::endl; return 0; } ``` #### 4. 注意事项 - **类型匹配**:`std::to_integer` 的返回类型必须能够容纳枚举类型的底层值。如果目标类型无法表示枚举值,则会导致未定义行为[^6]。 - **强类型枚举支持**:`std::to_integer` 支持 `enum` 和 `enum class` 类型的转换。 - **无异常抛出**:该函数是 `noexcept` 的,因此不会抛出任何异常。 #### 5. 优势 相比于直接使用静态强制类型转换(如 `static_cast`),`std::to_integer` 提供了更高的安全性。它明确表达了将枚举值转换为整数的意图,并减少了潜在的错误风险[^6]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值