删除数字能被10的k次方整除

Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k.

In the given number of n Polycarp wants to remove the least number of digits to get a number that is divisible by 10k. For example, if k = 3, in the number 30020 it is enough to delete a single digit (2). In this case, the result is 3000 that is divisible by 103 = 1000.

Write a program that prints the minimum number of digits to be deleted from the given integer number n, so that the result is divisible by 10k. The result should not start with the unnecessary leading zero (i.e., zero can start only the number 0, which is required to be written as exactly one digit).

It is guaranteed that the answer exists.

Input

The only line of the input contains two integer numbers n and k (0 ≤ n ≤ 2 000 000 000, 1 ≤ k ≤ 9).

It is guaranteed that the answer exists. All numbers in the input are written in traditional notation of integers, that is, without any extra leading zeros.

Output

Print w — the required minimal number of digits to erase. After removing the appropriate w digits from the number n, the result should have a value that is divisible by 10k. The result can start with digit 0 in the single case (the result is zero and written by exactly the only digit 0).

Example
Input
30020 3
Output
1
Input
100 9
Output
2
Input
10203049 2
Output

3

source:点击打开链接

题意:给你一个数你,一个数k,问删去n中的多少个数字后能被10的k次方整除。

代码:

#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
    string s;
    int t;
    while(cin>>s>>t)
    {
        int ans=0;
        int n = s.length();
        for (int i = 0; i < n; i++)
        {
            if (s[i] == '0')
                ans++;
        }
        if (ans >= t)
        {
            int f = 0;
            int num0 = 0;
            for (int i = n - 1; i >= 0; i--)
            {
                if (s[i] == '0')
                {
                    num0++;
                    if (num0 == t)
                        break;
                }
                else
                {
                    f++;
                }
            }
            printf("%d\n",f);
        }
        else
            printf("%d\n",n-1);
    }
    return 0;
}


要统计一个数字范围内能被6整除且能被8整除数字,我们可以按照以下步骤进行: 1. **确定能被6和8整除的数的特征**: - 一个数能被6整除,意味着它能被2和3整除。 - 一个数能被8整除,意味着它能被2的3次方整除。 2. **找到能被6和8整除的最小公倍数**: - 6的质因数分解为:2 * 3 - 8的质因数分解为:2^3 - 因此,6和8的最小公倍数为:2^3 * 3 = 24 3. **统计范围内的数**: - 遍历从1到输入数字的所有整数,检查每个数是否能被24整除。 - 统计能被24整除的数的个数。 以下是一个Python代码示例,演示如何实现这个功能: ```python def count_numbers_divisible_by_6_and_8(limit): count = 0 for number in range(1, limit + 1): if number % 24 == 0: count += 1 return count # 输入一个数字 input_number = int(input("请输入一个数字: ")) result = count_numbers_divisible_by_6_and_8(input_number) print(f"在1到{input_number}之间,能被6和8整除数字有: {result}个") # 优化后的代码 def count_numbers_divisible_by_6_and_8_optimized(limit): return limit // 24 input_number = int(input("请输入一个数字: ")) result = count_numbers_divisible_by_6_and_8_optimized(input_number) print(f"在1到{input_number}之间,能被6和8整除数字有: {result}个") ``` 在这个示例中,我们定义了一个函数`count_numbers_divisible_by_6_and_8`,它遍历从1到输入数字的所有整数,并统计能被24整除的数的个数。我们还提供了一个优化后的版本`count_numbers_divisible_by_6_and_8_optimized`,通过直接计算输入数字除以24的商来统计结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值