基础练习 数的读法

问题描述
  Tom教授正在给研究生讲授一门关于基因的课程,有一件事情让他颇为头疼:一条染色体上有成千上万个碱基对,它们从0开始编号,到几百万,几千万,甚至上亿。
  比如说,在对学生讲解第1234567009号位置上的碱基时,光看着数字是很难准确的念出来的。
  所以,他迫切地需要一个系统,然后当他输入12 3456 7009时,会给出相应的念法:
  十二亿三千四百五十六万七千零九
  用汉语拼音表示为
  shi er yi san qian si bai wu shi liu wan qi qian ling jiu
  这样他只需要照着念就可以了。
  你的任务是帮他设计这样一个系统:给定一个阿拉伯数字串,你帮他按照中文读写的规范转为汉语拼音字串,相邻的两个音节用一个空格符格开。
  注意必须严格按照规范,比如说“10010”读作“yi wan ling yi shi”而不是“yi wan ling shi”,“100000”读作“shi wan”而不是“yi shi wan”,“2000”读作“er qian”而不是“liang qian”。
输入格式
  有一个数字串,数值大小不超过2,000,000,000。
输出格式
  是一个由小写英文字母,逗号和空格组成的字符串,表示该数的英文读法。
样例输入
1234567009
样例输出
shi er yi san qian si bai wu shi liu wan qi qian ling jiu
 
写了半天的菜鸡做法,建议看下面大佬做法
  1 #include<iostream>
  2 #include<string>
  3 #include<cstring>
  4 using namespace std;
  5 
  6 string pinyin[11] ={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
  7 
  8 string fun2(string str)  //两位数读法 
  9 {
 10     int len = str.length();
 11     string s = "";
 12     if(str[1] == '0')
 13         {
 14             if(str[0] == '1')   //10 ---- shi
 15             s = "shi";
 16             else     // 20,30,40
 17             s = s + (pinyin[str[0] - '0']) + " shi";
 18         }
 19         else
 20         {
 21         if(str[0] == '1')
 22         {
 23             s = "shi " + (pinyin[str[1] - '0']);
 24         }
 25         else
 26         {
 27             s = pinyin[str[0] - '0'] + " shi " + pinyin[str[1] - '0'];
 28         }
 29         }
 30         return s;
 31 }
 32 
 33 string fun3(string str)  //三位数读法 
 34 {
 35     int len = str.length();
 36     string s = "";
 37     if(str[1] == '0' && str[2] == '0' && str[0] != '0')
 38         {
 39             s = pinyin[str[0] - '0'] + " bai";
 40         }
 41         else if(str[0] != '0' && str[1] != '0' && str[2] == '0')
 42         {
 43             s = pinyin[str[0] - '0'] + " bai " + pinyin[str[1] - '0'] + " shi";
 44         }
 45         else if(str[0] != '0' && str[1] == '0' && str[2] != '0')
 46         {
 47             s = pinyin[str[0] - '0'] + " bai " + pinyin[str[2] - '0'];
 48         }
 49         else
 50         {
 51             s = pinyin[str[0] - '0'] + " bai " + pinyin[str[1] - '0'] + " shi " + pinyin[str[2] - '0'];
 52         }
 53         return s;
 54 }
 55 
 56 string fun4(string str)
 57 {
 58     int len = str.length();
 59     string s = "";
 60     if(len == 1)
 61     {
 62         s = pinyin[str[0] - '0'];
 63     }
 64     else if(len == 2)
 65     {
 66         s = fun2(str);
 67     }
 68     else if(len == 3)  //三位数 
 69     {
 70         s= fun3(str);
 71     }
 72     else  //四位数 
 73     {
 74         if(str[0] != '0' && str[1] == '0' && str[2] == '0' && str[3] == '0')
 75         {
 76             s = pinyin[str[0] - '0'] + " qian";
 77         }
 78         else if(str[0] != '0' && str[1] == '0' && str[2] == '0' && str[3] != '0')
 79         {
 80             s = pinyin[str[0] - '0'] + " qian ling " + pinyin[str[3] - '0'];
 81         }
 82         else if(str[0] != '0' && str[1] == '0' && str[2] == '1' && str[3] == '0')
 83         {
 84             s = pinyin[str[0] - '0'] + " qian ling yi shi";
 85         }
 86         else if(str[0] != '0' && str[1] == '0' && str[2] == '1' && str[3] != '0')
 87         {
 88             s = pinyin[str[0] - '0'] + " qian ling yi shi " + pinyin[str[3] - '0'];
 89         }
 90         else if(str[0] != '0' && str[1] == '0' && str[2] != '0' && str[2] != '1' && str[3] != '0')
 91         {
 92             string ss = "";
 93             ss = ss + str[2] + str[3]; 
 94             s = pinyin[str[0] - '0'] + " qian " + fun2(ss);
 95         }
 96         else
 97         {
 98             string sss = "";
 99             sss = sss + str[1] + str[2] + str[3];
100             s = pinyin[str[0] - '0'] + " qian " + fun3(sss);
101         }
102     }
103     return  s;
104 }
105 
106 string quling(int x,int y,string str)  //去掉前导零
107 {
108     string s = "";
109     int i = x;
110     while(str[i] == '0')
111     i++;
112     for(int j = i; j <= y;j++)
113     s += str[j];
114     return s;
115  } 
116  
117  void wanji(int l,string str)
118  {
119      string s1 = "",s2 = "";
120              s1 = quling(l - 4,l - 1,str);
121              s2 = quling(0,l - 5,str);
122              //cout << "s1 = " << s1 << endl;
123              //cout << "s2 = " << s2 << endl;
124              if(s1.length() == 0)
125              {
126                  cout << fun4(s2) << " wan" << endl; 
127              }
128              else if(s1.length() > 0 && s1.length() < 4)
129              {
130                  cout << fun4(s2) << " wan ling " << fun4(s1) << endl;
131              }
132              else
133              {
134                  cout << fun4(s2) << " wan " << fun4(s1) << endl;
135              }
136  }
137 
138 int main()
139 {
140     
141     string str;
142     while(cin >> str)
143     {
144         //从低位到高位每四位一个数 + 数量级
145          int len = str.length();  //数字长度
146          int n = len / 4 + 1;  //几段 
147          if(len > 8)
148          {
149              string s1 = "",s2 = "",s3 = "";
150              s1 = quling(len - 4,len - 1,str);
151              s2 = quling(len - 8,len - 5,str);
152              s3 = quling(0,len - 9,str);
153              cout << fun4(s3) << " yi ";
154              if(s2.length() == 0)
155              cout << "ling wan ";
156              else if(s2.length() > 0 && s2.length() < 4)
157              cout << "ling " << fun4(s2) << " wan ";
158              else
159              cout << fun4(s2) << " wan ";
160              
161              if(s1.length() > 0 && s1.length() < 4)
162              cout << " ling " << fun4(s1) << endl;
163              else if(s1.length() == 4)
164              cout << fun4(s1) << endl;
165          }
166          else if(len > 4 && len < 9)  //万级 
167          {
168              
169              wanji(len,str);
170          /*    string s1 = "",s2 = "";
171              s1 = quling(len - 4,len - 1,str);
172              s2 = quling(0,len - 5,str);
173              //cout << "s1 = " << s1 << endl;
174              //cout << "s2 = " << s2 << endl;
175              if(s1.length() == 0)
176              {
177                  cout << fun4(s2) << " wan" << endl; 
178              }
179              else if(s1.length() > 0 && s1.length() < 4)
180              {
181                  cout << fun4(s2) << " wan ling " << fun4(s1) << endl;
182              }
183              else
184              {
185                  cout << fun4(s2) << " wan " << fun4(s1) << endl;
186              }*/
187          }
188          else  //小于等于四位数 
189          {
190              cout << fun4(str) << endl;
191          }
192      } 
193     return 0;
194 }
  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 string num[11]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu","shi"}; 
  4 string big[3]={"shi","bai","qian"};
  5 string bb[2]={"wan","yi"};
  6 string change(string a,int m,int Max)
  7 {
  8     string str="";
  9     int w=-1;
 10     int n=0,i,le=a.length();
 11     bool use=false;
 12     for(i=0;i<le;i++)
 13     n=n*10+(a[i]-48);
 14     if(n==0&&m==1)
 15     return "";
 16     if(n/1000>0)
 17     {
 18         str+=num[n/1000];
 19         str+=' ';
 20         str+=big[2];
 21         str+=' ';
 22         n=n-n/1000*1000;
 23         w=2;
 24     }
 25     if(w!=2&&m<Max&&!use)
 26     {
 27         str+="ling ";
 28         use=true;
 29     }
 30     if(n/100>0)
 31     {
 32         str+=num[n/100];
 33         str+=' ';
 34         str+=big[1];
 35         str+=' ';
 36         n=n-n/100*100;
 37         w=1;
 38         use=false;
 39     }
 40     if(w!=1&&!use&&m<Max)
 41     {
 42         
 43         str+="ling ";
 44         use=true;
 45     }
 46     if(n/10>0)
 47     {
 48         if(w!=1&&n/10==1&&m!=1)
 49         {
 50             str+=big[0];
 51             str+=' ';
 52             n=n-n/10*10;
 53             w=0;
 54         }
 55         else
 56         {
 57             str+=num[n/10];
 58             str+=' ';
 59             str+=big[0];
 60             str+=' ';
 61             n=n-n/10*10;
 62             w=0;
 63         }
 64     }
 65     if(n!=0)
 66     {
 67         str+=num[n];
 68         str+=' ';
 69     }
 70     if(m>1)
 71     {
 72         str+=bb[m-2];
 73         str+=' ';
 74     }
 75     return str;
 76 }
 77 int main()
 78 {
 79     string str,ed="";
 80     int m,n,t,star=0;
 81     cin>>str;
 82     int len=str.length();
 83     t=m=len/4+1;
 84     n=len%4;
 85     if(n==0)
 86     t=m=m-1;
 87     while(t)
 88     {
 89         if(t==m)
 90         {
 91             if(n!=0)
 92             {
 93                 ed+=change(str.substr(star,n),t,m);
 94                 star+=n;
 95             }
 96             else
 97             {
 98                 ed+=change(str.substr(star,4),t,m);
 99                 star+=4;
100             }
101             
102             
103         }
104         else
105         {
106             ed+=change(str.substr(star,4),t,m);
107             star+=4;
108         }
109         t--;
110     }
111     cout<<ed<<endl;
112     return 0;
113 }

 

posted @ 2020-04-03 14:38  树下一朵云  阅读(518)  评论(0编辑  收藏  举报