题解 | #学英语#

学英语

http://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc

#include <stdio.h>

static char base[][10] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
static char baseTen[][8] = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
static char unit[][10] = { "", " thousand", " million" };
int main(int argc, char** argv)
{
    int input, data[3];
    while (scanf("%d", &input) != EOF) {
        unsigned char flag = 0;
        for (int i = 0; i < 3; i++) {
            data[i] = input % 1000;
            if (data[i]) flag |= 0x1 << i;
            input /= 1000;
        }

        for (int i = 2; i >= 0; i--) {
            if (data[i] / 100) { printf("%s hundred", base[data[i] / 100 - 1]); data[i] %= 100; if (data[i] % 100) printf(" and "); }
            if (data[i] >= 20) { printf("%s", baseTen[data[i] / 10 - 2]); data[i] %= 10; if(data[i]) printf(" "); }
            if (data[i]) printf("%s", base[data[i] - 1]);
            if (flag & (0x1 << i)) { printf("%s", unit[i]); flag &= ~(0x1 << i); if (flag) printf(" ");}
        }
    }
    return 0;
}
全部评论
写得太棒了!
点赞 回复 分享
发布于 2022-04-24 20:50
太厉害了,逻辑清晰
点赞 回复 分享
发布于 2022-07-12 22:55

相关推荐

伟大的烤冷面被普调:暨大✌🏻就是强
点赞 评论 收藏
分享
11-01 20:03
已编辑
门头沟学院 算法工程师
Amazarashi66:这种也是幸存者偏差了,拿不到这个价的才是大多数
点赞 评论 收藏
分享
9 3 评论
分享
牛客网
牛客企业服务