[编程题] 页码统计

题目来源:牛客网

[编程题] 页码统计
牛牛新买了一本算法书,算法书一共有n页,页码从1到n。牛牛于是想了一个算法题目:在这本算法书页码中0~9每个数字分别出现了多少次?
输入描述 :
输入包括一个整数n(1 ≤ n ≤ 1, 000, 000, 000)

输出描述 :
输出包括一行10个整数,即0~9这些数字在页码中出现的次数,以空格分隔。行末无空格。

输入例子 :
999

输出例子 :
189 300 300 300 300 300 300 300 300 300

#include <stdio.h>
#include <iostream>
using namespace std;

int sum(int n1, int n)
{
    int icount = 0;
    long ifactor = 1;
    int ilowernum = 0;
    int icurrnum = 0;
    int ihighnum = 0;

    while (n / ifactor != 0){
        ilowernum = n - (n / ifactor)*ifactor;
        icurrnum = (n / ifactor) % 10;
        ihighnum = n / (ifactor * 10);
        if (icurrnum < n1)
        {
            icount += ihighnum*ifactor;
        }
        else if (icurrnum == n1)
        {
            icount += ihighnum*ifactor + ilowernum + 1;
        }
        else if (icurrnum>n1)
        {
            icount += (ihighnum + 1)*ifactor;
        }
        ifactor *= 10;
    }
    return icount;
}

int sum0(int n)
{
    int icount = 0;
    long ifactor = 1;
    int ilowernum = 0;
    int icurrnum = 0;
    int ihighnum = 0;

    while (n / ifactor != 0){
        ilowernum = n - (n / ifactor)*ifactor;
        icurrnum = (n / ifactor) % 10;
        ihighnum = n / (ifactor * 10);
        if (ihighnum > 0)
        {
            if (icurrnum == 0)
                icount += (ihighnum - 1)*ifactor + ilowernum + 1;
            else
                icount += ihighnum*ifactor;
            ifactor *= 10;
        }
        else
            break;
    }
    return icount;
}

int main(){
    int n = 0;
    cin >> n;
    cout << sum0(n);
    for (int i = 1; i < 10; i++)
    {
        cout << " " << sum(i, n);
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值