Title 1469: reading of numbers

subject

Professor Tom is teaching postgraduates a course on genes. One thing gives him a headache: there are thousands of base pairs on a chromosome. They number from 0 to millions, tens of millions, or even hundreds of millions.

For example, when explaining the base at position 1234567009 to students, it is difficult to read the numbers accurately just by looking at them.

Therefore, he urgently needs a system, and then when he enters 12 3456 7009, he will give the corresponding reading method:

one billion two hundred and thirty-four million five hundred and sixty-seven thousand and nine

Expressed in Chinese Pinyin as

shi er yi san qian si bai wu shi liu wan qi qian ling jiu

So he just needs to read it.

Your task is to help him design such a system: given an Arabic numeral string, you help him convert it into a Chinese Pinyin string according to the Chinese reading and writing norms, and the two adjacent syllables are separated by a space character.

Note that the specifications must be strictly followed. For example, "10010" reads "yi wan ling yi shi" instead of "yi wan ling shi", "100000" reads "shi wan" instead of "yi shi wan", and "2000" reads "er qian" instead of "liang qian".

input
There is a number string with a value size of no more than 2000000000.

output
Is a string composed of lowercase English letters, commas and spaces, indicating the English pronunciation of the number.

sample input
1234567009

sample output
shi er yi san qian si bai wu shi liu wan qi qian ling jiu

Problem solving ideas

Read in the data, then fill in "0" until 12 bits, so that you can "read" in groups of 4 bits. In this operation, you need to pay attention to some special reading methods given in the example:

  1. Consecutive zeros in the middle of non-zero numbers can only be read once;
  2. The tens in the number are read as n shi, and the following 0 cannot be displayed;
  3. If all are 0 after non-zero, 0 will not be read out;
  4. 12 do not output Yi (I) when reading;
  5. The 0 of the leader filled in front of a non-zero number cannot be read.

code

#include<stdio.h>
#include<string.h>
#include<math.h>
char number[10][5] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
char w[5][5] = {"","ge","shi","bai","qian"};
char W[3][4] = {"yi","wan",""};
int has_printed = 0;
int before = 0;//Indicates whether the four high digits in front of this 0 have been read out

void Read(int a[4],int k){
    int i,j,temp = 0,f=0,num=0;
    int isprinted=0, isFirst=1;
    int danwei = 0;
    for (i=0;i<4;i++)
        temp+=pow(10,3-i)*a[i];
    if (temp==0)
        return ;
    for (i=0;i<4;i++)
    {
        if (a[i]==0 && isFirst==1)
        {
            if (f==0 && before==1)
            {
                if (has_printed==1)
                    printf(" ");
                printf("ling");
                f = 1;
            }
            continue;//The leading 0 is not read
        }
        else
        {
            isFirst = 0;//The first non-0 has already appeared
            if (a[i]==0 && a[i-1]==0)//Don't read 0 twice in a row
                continue;
            if (i==2 && a[i]==1)
            {
                if (has_printed==1)
                    printf(" ");
                printf("shi");
                has_printed = 1;
            }
            else if (a[i]==0)//If a non-zero number is followed by 0, do not read 0
            {
                for (j=i+1;j<4;j++)
                {
                    if (a[i]!=a[j])
                    {
                        if (has_printed==1)
                            printf(" ");
                        printf("ling");
                        break;
                    }
                }  
                if (j==4)
                    break;//If it is followed by 0, there is no need to output
            }
            else
            {
                if (has_printed==1)
                    printf(" ");
                printf("%s",number[a[i]]);
                has_printed = 1;
                if (a[i]!=0 && i!=3)//Bits do not need to output "bits"
                    printf(" %s",w[4-i]);
            }
            has_printed = 1;
            danwei = 1;
        }
    }
    if (danwei==1)
    {
        before = 1;
        printf(" %s",W[k-1]);
    }
}

int main()
{
	char a[11];
	char b[13] = {'0','0','0','0','0','0','0','0','0','0','0','0','0'};
	int c[4];
	scanf("%s",a);
	int i,k,lena = strlen(a);
	for (i=12-lena;i<12;i++)
	    b[i] = a[i-12+lena];
	b[12] = '\0';
	
	for (k=1;k<=3;k++)
	{
	    for (i=0;i<4;i++)
	        c[i] = b[i+4*(k-1)]-48;
        Read(c,k);
	}
	return 0;
}

Keywords: C

Added by alcibar on Sun, 06 Mar 2022 03:11:29 +0200