BOJ 469. 暑假作业题

题意:给出一个数,输出中文读法

思路:巨坑的模拟题。我的方法是分治法。因为最后处理的其实是万以内的数。所以,我们先判断是否大于亿,在判断是否大于万,最后只对万以内的数处理。

          其中有一个问题是:是否有多余的0需要读。

          处理方法是这样的:1.对于千位的处理:传入一个标识变量,表示千以前是否有数。如果有数且千位为0且整个千位数不为0,那就必须要多读一个0.

                                          2.对于其他位的处理:首先要读出一个不是0的数,如果此数后一位为0且后面还有不为0的数,那接下来就要多读一个0;

          其他细节请看代码。

坑:只有0需要读0

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
 
using namespace std;
 
const long long E = 100000000LL;
const long long W = 10000LL;
char c[4] = {0,'S','B','Q'};
 
void solve(long long num,bool signal){
    if(num / E > 0){
        solve(num / E,false);
        putchar('E');
        solve(num % E,true);
    }
    else if(num / W > 0){
        solve(num / W,signal);
        putchar('W');
        solve(num % W,true);
    }
    else{
        int a[4]={0};
        int cnt = 0;
        for(int i = 0 ; num;++i, num/=10){
            a[i] = num % 10;
            if(a[i] != 0) cnt++;
        }
        if(cnt != 0 && a[3] == 0 && signal)
            putchar('0');
        for(int i = 3; i > 0; i--){
            if(a[i] != 0){
                putchar(a[i] + '0'),putchar(c[i]),cnt--;
                if(cnt != 0 && a[i-1]==0)
                putchar('0');
            }
        }
        if(a[0] != 0)
            putchar(a[0] + '0');
    }
}
 
int main(void){
    //freopen("input.txt","r",stdin);
    int n;
    scanf("%d", &n);
    while(n--){
        long long num;
        scanf("%lld", &num);
        if(num==0)
            puts("0");
        else{
            solve(num,false);
            puts("");
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值