蓝桥杯BASIC-20 基础练习 数的读法

蓝桥杯BASIC-20 基础练习 数的读法

问题描述
  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

  我本来觉得可以找规律的,但是太多零的问题导致很多特殊条件出现,为了避免乱套所以按照位数写,从上到下写,后来交题肯定有错误,再对着错误数据一点点改,所以过了测试,但是不确保程序对所有数据都是正确的!!(因为我是照着测试点改的,实在不想一个个排查了)

代码:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    char s[14];
    cin >> s;
    int len = strlen(s);
    char a[20][20] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
    char b[20][20] = {"", "", "shi", "bai", "qian", "wan", "shi", "bai", "qian", "yi", "shi"};
    int i, j, k;
    //注释掉的不用看,是一开始的憨憨思想。
    //cout << s << endl;
    //cout << s[0];
    //cout << "len=" << len << endl;
    /*if(len == 1){
        int t = s[0]-'0';
        cout << a[t] << endl;
    }
    if(len == 2){
        int t1 = s[0]-'0';
        int t2 = s[1]-'0';
        if((s[0]-'0') == 1)
            cout << "shi" << " " << a[t2] << endl;
        else
            cout << a[t1] << " " << b[len] << " " << a[t2] << endl;
    }
    if(len == 3){
        int t1 = s[0]-'0';
        int t2 = s[1]-'0';
        int t3 = s[2]-'0';
        if((s[0]-'0') == 1)
            cout << "shi" << " " << a[t2] << endl;
        else
            cout << a[t1] << " " << b[len] << " " << a[t2] << endl;
    return 0;*/
    if(len == 10){
        int t = s[0]-'0';
        if(t == 1)
            cout << b[2] << " ";
        else
            cout << a[t] << " " << "shi" << " ";
    }
    if(len >= 9){
        int t = s[len-9]-'0';
        if(t == 0)
            cout << "yi" << " ";
        else
            cout << a[t] << " " << "yi" << " ";
    }
    if(len >= 8){
        int t = s[len-8]-'0';
        if(t==0)
            cout << a[0] << " ";
        else
            cout << a[t] << " " << "qian" << " ";
    }
    if(len >= 7){
        int t = s[len-7]-'0';
        if(t==0){
            if(s[len-8]!='0')
                cout << a[0] << " ";
        }
        else
            cout << a[t] << " " << "bai" << " ";
    }
    if(len >= 6){
        int t = s[len-6]-'0';
        if(t==0){
             if(s[len-7]!='0')
                cout << a[0] << " ";
        }
        else{
            if(len>6 && t==1)
                cout << "yi shi ";
            else if(len==6 && t==1)
                cout << "shi ";
            else
                cout << a[t] << " " << "shi" << " ";
        }
    }
    if(len >= 5){
        int t = s[len-5]-'0';
        if(t==0)
            cout << "wan ";
        else
            cout << a[t] << " " << "wan" << " ";
    }
    if(len >= 4){
        int t = s[len-4]-'0';
        if(t==0){
            if(s[len-1]!='0')
                cout << a[0] << " ";
        }
        else
            cout << a[t] << " " << "qian" << " ";
    }
    if(len >= 3){
        int t = s[len-3]-'0';
        if(t==0){
            if(s[len-4]!='0' && s[len-1]!='0')
                cout << a[0] << " ";
        }
        else
            cout << a[t] << " " << "bai" << " ";
    }
    if(len >= 2){
        int t = s[len-2]-'0';
        if(t==0){
             if(s[len-3]!='0' && s[len-1]!='0')
                cout << a[0] << " ";
        }
        else{
            if(len>3 && t==1)
                cout << "yi shi ";
            else if(len==3 && t==1)
                cout << "shi ";
            else
                cout << a[t] << " " << "shi" << " ";
        }
    }
    if(len >= 1){
        int t = s[len-1]-'0';
        /*if(t==0){
             if(s[len-2]!='0')
                cout << a[0];
        }*/
        if(t==0){
            if(len==1)
                cout << a[0];
        }
        else
            cout << a[t];
    }
    return 0;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值