B1082 Read Number in Chinese (25分)
- Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fu first if it is negative.
For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu. Note: zero (ling) must be handled correctly according to the Chinese tradition. For example, 100800 is yi Shi Wan ling ba Bai.
我的思路:
所有输入的字符串往前补0补到九位,按9位处理
按各部分全零非全零分,有2 * 2 * 2种,然后读上“万”,“千百十个”整体拿出来处理,
//亿位有非0数,万位不足四位时前面不能读0
//“千百十个”不会处理,我就把所有情况罗列出来了。。。🤦“万”的个位可以读0,后面的部分个位不能读0,每个部分的所有前导0都只读一次。这样的思路还是枚举法,很容易少考虑到一些情况,如果实战真用这种方法,那心态估计早就崩了吧。。。。。。
网上的思路:
学习的代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string num[10] = { "ling","yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };
string c[6] = { "Ge","Shi", "Bai", "Qian", "Yi", "Wan" };
int J[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
vector<string> res;
int main() {
int n;
cin >> n;
if (n == 0) {
cout << "ling";
return 0;
}
if (n < 0) {
cout << "Fu ";
n = -n;
}
int part[3];
part[0]= n / 100000000;
part[1]= (n % 100000000) / 10000;
part[2] = n % 10000;
bool zero = false; //是否在非零数字前输出合适的ling
int printCnt = 0; //用于维护单词前没有空格,之后输入的单词都在前面加一个空格。
for (int i = 0; i < 3; i++) {
int temp = part[i]; //三个部分,每部分内部的命名规则都一样,都是X千X百X十X
for (int j = 3; j >= 0; j--) {
int curPos = 8 - i * 4 + j; //当前数字的位置
if (curPos >= 9) continue; //最多九位数
int cur = (temp / J[j]) % 10;//取出当前数字
if (cur != 0) {
if (zero) {
printCnt++ == 0 ? cout<<"ling" : cout<<" ling";
zero = false;
}
if (j == 0)
printCnt++ == 0 ? cout << num[cur] : cout << ' ' << num[cur]; //在个位,直接输出
else
printCnt++ == 0 ? cout << num[cur] << ' ' << c[j] : cout << ' ' << num[cur] << ' ' << c[j]; //在其他位,还要输出十百千
} else {
if (!zero && j != 0 && n / J[curPos] >= 10) zero = true; //注意100020这样的情况
}
}
if (i != 2 && part[i]>0) cout << ' ' << c[i + 4]; //处理完每部分之后,最后输出单位,Yi/Wan
}
return 0;
}
“千百十个”需要改进优化!
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
typedef long long ll;
const int MAX=1005;
const int INF=0x3f3f3f3f;
const int mod=1000000007;
char mp[8][10]={"Yi","Qian","Bai","Shi"};
char num[10][10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
void du(string s,int flag)
{
int i=0;
while(s[i]=='0')//已剔除所有0的情况
{
i++;
}
if(i!=0)
{
if(flag==0)
cout<<"ling"<<" ";
if(i==1)
{
cout<<num[s[1]-'0']<<" "<<mp[2];
if(s[2]=='0'&&s[3]!=0)
{
cout<<" "<<"ling"<<" "<<num[s[3]-'0'];
}
else if(s[2]!='0'&&s[3]=='0')
{
cout<<" "<<num[s[2]-'0']<<" "<<mp[3];
}
else if(s[2]!='0'&&s[3]!='0')
{
cout<<" "<<num[s[2]-'0']<<" "<<mp[3]<<" "<<num[s[3]-'0'];
}
}
else if(i==2)
{
if(s[3]=='0')
cout<<num[s[2]-'0']<<" "<<mp[3];
else
cout<<num[s[2]-'0']<<" "<<mp[3]<<" "<<num[s[3]-'0'];
}
else if(i==3)
cout<<num[s[3]-'0'];
}
else
{
if(s[3]!='0')
{
if(s[1]=='0'&&s[2]=='0')//1001型
{
cout<<num[s[0]-'0']<<" "<<mp[1]<<" "<<"ling";
}
else//1011型
{
for(int j=0;j<3;j++)
{
if(j!=0)
{
cout<<" ";
}
if(s[j]=='0')
cout<<"ling";
else
cout<<num[s[j]-'0']<<" "<<mp[j+1];
}
}
cout<<" "<<num[s[3]-'0'];
}
else if(s[3]=='0')
{
cout<<num[s[0]-'0']<<" "<<mp[1];
if(s[1]=='0'&&s[2]!=0)
{
cout<<" "<<"ling"<<" "<<num[s[2]-'0']<<" "<<mp[3];
}
else if(s[1]!='0'&&s[2]!='0')
{
cout<<" "<<num[s[1]-'0']<<" "<<mp[2]<<" "<<num[s[2]-'0']<<" "<<mp[3];
}
else if(s[1]!='0'&&s[2]=='0')
{
cout<<" "<<num[s[1]-'0']<<" "<<mp[2];
}
}
}
}
int main()
{
string s;
cin>>s;
if(s[0]=='-')
{
cout<<"Fu ";
s.erase(s.begin());
}
while(s.size()<9)
{
s='0'+s;
}
if(s=="000000000")
cout<<"ling";
else
{
int flagwan=0,flagqian=0;
int check=0;
if(s[0]!='0')
{
cout<<num[s[0]-'0']<<" "<<"Yi";
check=1;
}
for(int i=1;i<=4;i++)
{
if(s[i]!='0')
flagwan=1;//万位有数字
}
for(int i=5;i<=8;i++)
{
if(s[i]!='0')
flagqian=1;//千位有数字
}
if(flagwan==1&&flagqian==1)
{
if(check==1)
{
cout<<" ";
du(s.substr(1,4),0);
}
else
du(s.substr(1,4),1);
cout<<" Wan ";
du(s.substr(5,8),0);
}
else if(flagwan==1&&flagqian==0)
{
if(check==1)
{
cout<<" ";
du(s.substr(1,4),0);
}
else
du(s.substr(1,4),1);
cout<<" Wan ";
}
else if(flagwan==0&&flagqian==1)
du(s.substr(5,8),0);
}
return 0;
}