| |

VerySource

 Forgot password?
 Register
Search
View: 988|Reply: 6

Seeking Algorithm: Numbers to Chinese

[Copy link]

2

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-9 17:00: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

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-1-28 04:00:02
| Show all posts
//52367.23
// Wu Wanzhang, San Bai Lu pick up Yuan Yuan corner three points

#include <stdio.h>
#include <string.h>

char RMB [10] [3] = {"Zero", "One", "贰", "Three", "Three", "Wu", "Lu", "柒", "捌", "玖"} ;
char value [13] [3] = {"zero", "pick up", "bai", "仟", "wan", "pick up", "hundred", "thousand", "billion", "pick up, "百", "仟", "万"};
char faction [2] [3] = {"角", "分"};

void StyleChange (const char * str)
{
  int i, j, k = 1, t = 0, s = 0; // k is used to indicate the position of the current operation (tens, hundreds ...)
  int n;
  char ValueSave [26] [3]; // Store integer part
  char FactionValue [2] [3]; // Store fractional part
 
  n = strlen (str);
  for (i = 0; i <n; i ++)
  {
    if (str [i] == '.')
    break;
  }
 
  if (i> 13)
  {
    printf ("Sorry, please enter a number below 1 trillion digits\n");
    return;
  }
  if (n-i> 3)
  {
    printf ("Sorry, please make sure 2uo1n after the decimal point");
    return;
  }
    strcpy (ValueSave [t ++], "Meta");
    if ((n-i> 1&&i == 1) || str [i-1]! = '0')
    strcpy (ValueSave [t ++], RMB [str [i-1]-'0']);
 
  for (j = i-2; j> = 0; j--) // Operation of integer part
  {
     if (str [j] == '0')
     {
      if (str [j + 1]! = '0')
      {
       strcpy (ValueSave [t ++], RMB [str [j]-'0']);
       k ++;
      }
      else
      {
        if (i> 4&&k == 8)
        {
           strcpy (ValueSave [t ++], value [k]);
        }
        if (i <= 8&&k == 4)
        {
           strcpy (ValueSave [t ++], value [k]);
        }
        k ++;
      }
      continue;
     }
     strcpy (ValueSave [t ++], value [k ++]);
     strcpy (ValueSave [t ++], RMB [str [j]-'0']);
  }
 
  k = 0; // operation of decimal part
  for (j = i + 1; j <n; j ++)
  {
     strcpy (FactionValue [s ++], RMB [str [j]-'0']);
     strcpy (FactionValue [s ++], faction [k ++]);
  }
 
  for (j = t-1; j> = 0; j--)
  printf ("% s", ValueSave [j]);
  for (j = 0; j <s; j ++)
  printf ("% s", FactionValue [j]);
  printf ("\n");
}

int main ()
{
   char s [17];
   int n;
   while (gets (s)! = NULL)
   {
   n = strlen (s);
   if (n> 16)
   {
    printf ("Sorry, please enter a number below 1 trillion digits\n");
    return -1;
   }
   StyleChange (s);
   }
   return 0;
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-10 08:45:01
| Show all posts
while(1)
===
What do you want to do? It doesn’t seem to exit the while loop, the time is over, isn’t it?
Reply

Use magic Report

0

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-8-10 09:15:01
| Show all posts
I wrote one before
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-10 15:00:01
| Show all posts
Steal and study slowly^^
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-10 16:15:01
| Show all posts
Why do you ask questions on ZJU? ?
LZ's while(1). . . No break
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-8-10 16:30:01
| Show all posts
People say, from the time between inputting the number and outputting the number, why are you chasing after the withdrawal?

See the question clearly.
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