列表

详情


HJ42. 学英语

描述

Jessi初学英语,为了快速读出一串数字,编写程序将数字转换成英文:

具体规则如下:
1.在英语读法中三位数字看成一整体,后面再加一个计数单位。从最右边往左数,三位一单位,例如12,345 等
2.每三位数后记得带上计数单位 分别是thousand, million, billion.
3.公式:百万以下千以上的数 X thousand X, 10亿以下百万以上的数:X million X thousand X, 10 亿以上的数:X billion X million X thousand X. 每个X分别代表三位数或两位数或一位数。
4.在英式英语中百位数和十位数之间要加and,美式英语中则会省略,我们这个题目采用加上and,百分位为零的话,这道题目我们省略and

下面再看几个数字例句:
22: twenty two
100:  one hundred
145:  one hundred and forty five
1,234:  one thousand two hundred and thirty four
8,088:  eight thousand (and) eighty eight (注:这个and可加可不加,这个题目我们选择不加)
486,669:  four hundred and eighty six thousand six hundred and sixty nine
1,652,510:  one million six hundred and fifty two thousand five hundred and ten

说明:
数字为正整数,不考虑小数,转化结果为英文小写;
保证输入的数据合法
关键字提示:and,billion,million,thousand,hundred。

数据范围:


输入描述

输入一个long型整数

输出描述

输出相应的英文写法

示例1

输入:

22

输出:

twenty two

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C 解法, 执行用时: 2ms, 内存消耗: 288KB, 提交时间: 2019-10-06

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
void calc(unsigned int n, int *a)
{
    a[0]=n/(1000*1000*1000);
    a[1]=(n/(1000*1000))%1000;
    a[2]=(n/1000)%1000;
    a[3]=n%1000;
    //printf("%u %d %d %d %d\n", n, a[0], a[1], a[2], a[3]);
}
 
void print(int a)
{
    char *x[]={"", "one", "two", "three",
               "four", "five", "six",
               "seven", "eight", "nine",
               "ten", "eleven", "twelve",
              "thirteen", "fourteen", "fifteen",
              "sixteen", "seventeen", "eighteen",
              "nineteen"};
    char *y[]={"", "", "twenty", "thirty",
              "forty", "fifty", "sixty", "seventy",
              "eighty", "ninety"};
    unsigned flag=0;
    if(a>=100)
    {
        printf("%s hundred", x[a/100]);
        flag=1;
    }
    a%=100;
    if(a>=20)
    {
        if(flag)
        {
            printf(" and ");
        }
        printf("%s", y[a/10]);
        a%=10;
        if(a>0)
        {
            printf(" %s", x[a]);
        }
    }
    else if(a>0)
    {
        if(flag)
        {
            printf(" and ");
        }
        printf("%s", x[a]);
    }
}
 
int main()
{
    unsigned int n;
    while(scanf("%u", &n)!=EOF)
    {
        unsigned flag=0;
        int d[4]={0};
        calc(n ,d);
        if(d[0]!=0)
        {
            print(d[0]);
            printf(" billion");
            flag=1;
        }
        if(d[1]!=0)
        {
            if(flag)
            {
                printf(" ");
            }
            print(d[1]);
            printf(" million");
            flag=1;
        }
        if(d[2]!=0)
        {
            if(flag)
            {
                printf(" ");
            }
            print(d[2]);
            printf(" thousand");
            flag=1;
        }
        if(d[3]!=0)
        {
            if(flag)
            {
                printf(" ");
            }
            print(d[3]);
        }
        printf("\n");
    }
    return 0;
}

C 解法, 执行用时: 2ms, 内存消耗: 296KB, 提交时间: 2021-10-01

#include<stdio.h>
#include<string.h>
void calc(long n,int *a){
    a[3] = n/(1000*1000*1000);//billion
    a[2] = (n/(1000*1000))%1000;//million
    a[1] = (n/1000)%1000;//thousand
    a[0] = n%1000;
};

char *x[20] = {
    "", "one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"
};
char *y[10] = {
    "","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"
};
char *z[5] = {
    "hundred","thousand","million","billion"
};

void output_int(int a){
    
    int f = 0;
    if(a >= 100){
        printf("%s %s",x[a/100],z[0]);
        f = 1;
    }
    a %= 100;
    
    if(a >= 20){
        if(f){
            printf(" and ");
        }
        
        printf("%s",y[a/10]);
        a %= 10;
        if(a > 0){
            printf(" %s",x[a]);
        }
    }else if(a > 0){
        if(f){
            printf(" and ");
        }
        if(a > 0){
            printf("%s",x[a]);
        }
    }
    
    
}

int main(){
    long n;
    int a[4],f;
    while (scanf("%lu",&n)!=EOF) {
        f = 0;
        memset(a,0,sizeof(a));
        calc(n, a);
        if(a[3]){
            output_int(a[3]);
            printf(" %s",z[3]);
            f = 1;
        }
        
        if(a[2]){
            if(f){
                printf(" ");
            }
            output_int(a[2]);
            printf(" %s",z[2]);
            f = 1;
        }
        
        if(a[1]){
            if(f){
                printf(" ");
            }
            output_int(a[1]);
            printf(" %s", z[1]);
            f = 1;
        }
        
        if(a[0]){
            if(f){
                printf(" ");
            }
            output_int(a[0]);
        }
        
        printf("\n");
        
    }
}

上一题