原题链接:蓝桥杯基础练习VIP-数的读法
小弟不才 花了一个下午 写了将近120行代码 才AC 这题写起来 比较恶心 也比较复杂 所以好像没什么人去做 但我耐着性子AC了,所以希望大家能有恒心 有毅力 不要害怕出错 每一次出错就会是一点进步 至于题目的解法 没什么好说的 亿 万 千 百 十 1 1 2 3 4 5 6 7 8 9 考虑一些特殊情况 1、110000 读作 十一万 而不是一十一万 2、1010 读作一千零一十 而不是一千零十 3、我们要考虑在特殊位上的数字 比如说 十万位 当十万位前面没有数的时候 它读十 而不是 一十 4、考虑0的读法 如果0 在万位、个位、以及亿位的时候都不需要读 5、如果有两个连续的0则只读前面一个 1001 读左一千零一 而不是一千零零一 6、11读作十一 而不是一十一,所以考虑十位上的数字,并且它前面没有数字的时候读十一 7、万位上的数字如果它前面的十万,百万,千万位都是0,那么“万”字就不用输出 8、答案都在代码里。 9、写的不好 求轻喷
参考代码:
#include<iostream>
#include<cstring>
using namespace std;
char a[20][10]={{"ling"},{"yi"},{"er"},{"san"},{"si"},{"wu"},{"liu"},{"qi"},{"ba"},{"jiu"}};
char b[20][10]={{" "},{"shi"},{"bai"},{"qian"},{"wan"},{" "},{" "},{" "},{"yi"},{" "},{" "}};
void f(string str)
{
string str1;
int j=0;
for(int i=str.length()-1;i>=0;i--)
{
str1[j++]=str[i];
}
str1[j]='*';
for(int i=j-1;i>=0;i--)
{
if(i>=1)
{
if(i>=8&&i<=9)
{
if(i==9)
{
if(str1[i]-'0'==1)
cout<<b[10-i]<<" ";
else
{
cout<<a[str1[i]-'0']<<" ";
cout<<b[10-i]<<" ";
}
}
else
{
if(str1[i]-'0'!=0)
cout<<a[str1[i]-'0']<<" ";
if(b[i]!=" "&&str1[i]-'0'!=0)
cout<<b[i]<<" ";
else
cout<<b[8]<<" ";
}
}
else if(i>4&&i<=7)
{
if(i==5)
{
if(str1[i]-'0'==1&&str1[i+1]=='*')
cout<<b[1]<<" ";
else
{
if(str1[i]-'0'!=0||str1[i]-'0'==0&&str1[i-1]-'0'!=0)
cout<<a[str1[i]-'0']<<" ";
if(str1[i]-'0'!=0)
cout<<b[1]<<" ";
}
}
else
{
if(str1[i]-'0'!=0||str1[i]-'0'==0&&str1[i-1]-'0'!=0)
cout<<a[str1[i]-'0']<<" ";
if(b[i-4]!=" "&&str1[i]-'0'!=0)
cout<<b[i-4]<<" ";
}
}
else if(i==4)
{
if(str1[i]-'0'!=0)
cout<<a[str1[i]-'0']<<" ";
if(str1[i]-'0'==0&&str1[i+1]-'0'==0&&str1[i+2]-'0'==0&&str1[i+3]-'0'==0)
cout<<"";
else
cout<<b[4]<<" ";
}
else
{
if(i==1)
{
if(str1[i]-'0'==1&&str1[i+1]=='*')
cout<<b[1]<<" ";
else
{
if(str1[i]-'0'!=0||str1[i]-'0'==0&&str1[i-1]-'0'!=0)
cout<<a[str1[i]-'0']<<" ";
if(str1[i]-'0'!=0)
cout<<b[1]<<" ";
}
}
else
{
if(str1[i]-'0'!=0||str1[i]-'0'==0&&str1[i-1]-'0'!=0)
cout<<a[str1[i]-'0']<<" ";
if(b[i]!=" "&&str1[i]-'0'!=0)
cout<<b[i]<<" ";
}
}
}
else
{
if(str1[i]-'0'!=0)
cout<<a[str1[i]-'0'];
}
}
cout<<endl;
}
int main()
{
string str;
while(cin>>str)
{
f(str);
}
return 0;
}0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复