题解 | #学英语#

学英语

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

写了我一晚上的代码
看到评论区c语言解法比较少,本来程序主体对的 但一直有格式问题
好不容易调试好了

#include<stdio.h>
#include<string.h>
char str1[20][1000] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
char str2[20][1000] = {"ten","eleven","tweleve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
char str3[20][1000] = { "0","0","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety" };
char Temp[1000];
char result[1000];
void read(long  a[], int cnt)
{
    if (cnt == 1)
    {
        strcpy(Temp, str1[a[cnt - 1]]);
    }
    else if (cnt == 2 && a[cnt - 1] == 1)
    {
        int b = a[0] + 10 * a[1];
        strcpy(Temp, str2[b - 10]);
    }
    else if (cnt == 2 && a[cnt - 1] > 1)
    {
        strcpy(Temp, str3[a[cnt - 1]]);
        //strcat(Temp, " ");
        if (a[0] != 0)
        {
            strcat(Temp, " ");
            strcat(Temp, str1[a[0]]);
        }
    }
    else if (cnt == 3&&a[cnt-1]==0)
    {
        if(a[cnt-2]==1)
        {
            int b = a[0] + 10 * a[1];
            strcpy(Temp, str2[b - 10]);
        }
        else if(a[cnt-2]>1)
        {
            strcpy(Temp, str3[a[cnt - 2]]);
            //strcat(Temp, " ");
            if (a[0] != 0)
            {
                strcat(Temp, " ");
                strcat(Temp, str1[a[0]]);
            }
        }
    }
    else if(cnt == 3&&a[cnt-1]>=1)
    {
        strcpy(Temp,str1[a[cnt-1]]);
        strcat(Temp, " hundred");
        if(a[cnt-2]!=0||a[0]!=0)
        {
            strcat(Temp," and ");
            if(a[cnt-2]==1)
            {
                int b = a[0] + 10 * a[1];
                strcat(Temp, str2[b - 10]);
            }
            else if(a[cnt-2]>1)
            {
                strcat(Temp, str3[a[cnt - 2]]);
                //strcat(Temp, " ");
                if (a[0] != 0)
                {
                    strcat(Temp, " ");
                    strcat(Temp, str1[a[0]]);
                }
            }
            else if(a[cnt-2]==0)
            {
                strcat(Temp,str1[a[0]]);
            }
        }
    }
}

int main(void)
{
    long n;
    scanf("%ld", &n);
    long a[200]={0};
    long a2[3] = { 0 };
    long a3[3] = { 0 };
    long a4[3] = { 0 };
    int i = 0;
    int cnt = 0;
    


    while (n != 0)
    {
        cnt++;
        a[i++] = n % 10;
        n /= 10;
    }

    
    

    if (cnt <= 3)
    {
        read(a, cnt);
        strcpy(result, Temp);
    }
    else if (cnt > 3 && cnt <= 6)
    {
        for (int j = 3; j < cnt; j++)
        {
            a2[j - 3] = a[j];
        }
        read(a2, cnt - 3);
        strcpy(result, Temp);
        strcat(result, " thousand ");
        read(a, 3);
        strcat(result, Temp);
    }
    else if (cnt > 6 && cnt <= 9)
    {
        for (int j = 3; j < 6; j++)
        {
            a2[j - 3] = a[j];
        }
        for (int j = 6; j < cnt; j++)
        {
            a3[j - 6] = a[j];
        }
        read(a3, cnt - 6);
        strcpy(result, Temp);
        strcat(result, " million ");
        read(a2, 3);
        strcat(result, Temp);
        strcat(result, " thousand ");
        read(a, 3);
        strcat(result, Temp);
    }
    else if (cnt > 9 && cnt <= 12)
    {
        for (int j = 9; j < cnt; j++)
        {
            a4[j - 9] = a[j];
        }
        for (int j = 6; j < 9; j++)
        {
            a3[j - 6] = a[j];
        }
        for (int j = 3; j < 6; j++)
        {
            a2[j - 3] = a[j];
        }
        read(a4, 1);
        strcpy(result, Temp);
        strcat(result, " billion ");
        read(a3, 3);
        strcat(result, Temp);
        strcat(result, " million ");
        read(a2, 3);
        strcat(result, Temp);
        strcat(result, " thousand ");
        read(a, 3);
        strcat(result, Temp);

    }
    printf("%s",result);
    //printf("%d\n", cnt);
    /*for (i = 0; i < 3; i++)
    {
        printf("%ld", a[i]);
    }*/
    return 0;
}
全部评论

相关推荐

10-17 10:05
已编辑
北华大学 全栈开发
牛客872465272号:掉头发了哥
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务