| |

VerySource

 Forgot password?
 Register
Search
View: 748|Reply: 2

Seeking algorithm: number to Chinese ----------- Wang masters move!

[Copy link]

2

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 United States

Post time: 2020-1-9 15:20:01
| Show all posts |Read mode
Title: Chinese Numbers
     1000 ms time limit
     Memory limit 32768 Kbytes

     Problem Description
     Enter an integer and output it in Chinese.
     Enter
     The input contains multiple lines, one integer per line (<= 2000000000).
     Output
     Represent the input integers in Chinese, one output per line.
     Input sample
     12345
     87654321
     10001
 
     Sample output
     Twelve thousand three hundred forty five
     Eight hundred seven hundred sixty five thousand four hundred thirty two one
     Ten thousand and one
My algorithm:
#include <stdio.h>
main ()
{
    char * ch [11];
    ch [0] = "\0"; ch [1] = "一"; ch [2] = "二"; ch [3] = "三"; ch [4] = "四";
    ch [5] = "五"; ch [6] = "六"; ch [7] = "七"; ch [8] = "八"; ch [9] = "九"; ch [10] = "zero";
    char * dw [6];
    dw [2] = "十"; dw [3] = "百"; dw [0] = "千"; dw [5] = "万"; dw [4] = "billion"; dw [1] = "\0";
    char number [11]; int last, n, m, i, j, have;
    while (1)
    {
        scanf ("% s", number);
        last = 1; have = 0;
        for (i = 0; i <11&&number [i]! = '\0'; i ++);
        for (j = 0; j <i; j ++)
        {
                n = number [j] -48;
                m = i-j;
                if (have == 0&&m> 4&&m <9&&n) have = 1;
                if (last == 0&&n) printf (ch [10]);
                printf (ch [n]); if (n) printf (dw [m% 4]);
                if (m% 4 == 1)
                {
                       if (m == 5&&have) printf (dw [5]);
                       if (m == 9) printf (dw [4]);
                }
                last = n;
                                                                                                                 
        }
        printf ("\n");
    }
}

As a result, the defendant's time was exceeded (the time limit is 1000 milliseconds), and I hope that the master can make a move. Thank you.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-26 19:15:01
| Show all posts
The while(1) statement has no break;
Endless loop.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-26 20:30:01
| Show all posts
ch[1]= "一"; Is this also OK?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list