欢迎使用CSDN-markdown编辑器

题目

Given an integer N, count the total number of digit 0 appearing in all positive integers less than or equal to N.
Input

The input consists of multiple test cases. The first line contains an integer TT, indicating the number of test cases.(1≤T≤1000)(1≤T≤1000)
Each case contains only one integer NN.(1≤N≤108)(1≤N≤108)
Output

For each case output only one number.
Sample Input

3
1
10
910817
Sample Output

0
1
453979

题目大意

输入一个数N,算小于N的正整数中有多少个0.

解法

每一位分别去数,当前位不等于0,则该位0的个数是高位数乘以当前位的权重;当前位为0,则0的个数是高位数-1乘以当前位权重再加上低位数加一。

以910817为例
百位上0的个数为910*100
千位上0的个数位(91-1)*1000+817+1

代码


#include <iostream>

using namespace std;

int main()
{
    int T;
    cin >> T;
    while(T>0){
        int n,k=1,ans=0;
        int current = 0, low = 0, high = 0;
        cin >> n;
        while ((n / k) != 0){
            current = (n / k) % 10;
            high = (n / k) / 10;
            low = n - (n / k) * k;
            if (current > 0){
                ans = ans + high * k;
            } else {
                ans = ans + (high - 1) * k + low + 1;
            }
            k = k * 10;
        }
        cout << ans << endl;
        T--;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值