UVA10101 Bangla Numbers【进制+递归】

Bangla numbers normally use ’kuti’ (10000000), ’lakh’ (100000), ’hajar’ (1000), ’shata’ (100) while expanding and converting to text. You are going to write a program to convert a given number to text with them.
Input
The input file may contain several test cases. Each case will contain a non-negative number ≤ 999999999999999.
Output
For each case of input, you have to output a line starting with the case number with four digits adjustment followed by the converted text.
Sample Input
23764
45897458973958
Sample Output

  1. 23 hajar 7 shata 64
  2. 45 lakh 89 hajar 7 shata 45 kuti 89 lakh 73 hajar 9 shata 58

问题链接UVA10101 Bangla Numbers
问题简述:(略)
问题分析
    进制转换问题,但是需要考虑前导0的处理,后若干位均为0的处理。
    给出的代码充分考虑的程序的通用性,递归的运用恰到好处。
程序说明:(略)
参考链接:(略)
题记:程序通用性是程序员的最高追求!

AC的C++语言程序如下:

/* UVA10101 Bangla Numbers */

#include <bits/stdc++.h>

using namespace std;

string key[] = {"kuti", "lakh", "hajar", "shata"};
long long weight[] = {10000000LL, 100000LL, 1000LL, 100LL};
const int N = sizeof(weight) / sizeof(long long);

void output(long long n)
{
    bool flag = true;
    for(int i = 0; i < N; i++) {
        if(n >= weight[i]) {
            flag = false;
            output(n / weight[i]);
            printf(" %s", key[i].c_str());
            long long right = n % weight[i];
            if(right) output(right);
            break;
        }
    }
    if(flag)
        printf(" %lld", n);
}

int main()
{
    int caseno = 0;
    long long n;
    while(~scanf("%lld", &n)) {
        printf("%4d.", ++caseno);

        if(n == 0)
            printf(" 0");
        else
            output(n);
        printf("\n");
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值