Weird Rounding (Codeforces-779B)

题目链接:

http://codeforces.com/problemset/problem/779/B

B. Weird Rounding
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 3000that 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 0001 ≤ 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).

Examples
input
30020 3
output
1
input
100 9
output
2
input
10203049 2
output
3

题目大意:

输入一个n和k(没有前导0),判断去掉n的几位数字后能够被10^k整除。比如第一组测试数据:30020  3,去掉以‘2’后n变成30000,能够被10^3整除。即去掉一位数字后技能满足题意,所以输出‘1’。另外注意:题目保证有答案。

解题思路:

需要分几种情况:

记 len 为数字n的位数

1:len=1,而题目说一定有解,说明n一定等于0,因为0能够被任何数整除。不用去掉数字,输出0即可;
2:n的位数<=k,那么只有把n变为0,才满足题意,输出 len-1 (即只剩数字0);
3:n直接能够被10^k整除,输出0;
4:去掉数字后才能内10^k整除,从字符串的最后一位开始,先把第一个不为‘0’的变为‘0’(这里注意:题目说是去掉数字,而我是:把非0数字改为0,并且让k++。效果一样),然后判断是否满足题意,不满足的话继续改非0数字为0......

具体怎么实现请看代码:

代码:

#include<iostream>
using namespace std;
int sum0(string s,int len)   //计算并返回n从最后一位开始‘0’的个数
{
    int sum=0;
    for(int i=len-1;i>=0;i--)
    {
        if(s[i]=='0')
            sum++;
        else
            break;
    }
    return sum;
}
int main()
{
    string s;
    int k;
    while(cin>>s>>k)
    {
        int len=s.size();
        if(len==1)   //题目说保证有解,说明n的位数为1时,它必定是‘0’,而0能够被任何数整除,所以不用去掉任何数字,输出0
            cout<<0<<endl;
        else if(len<=k)   //n的位数<=k,说明只有把n变成0才满足题意,而结果没有前导0,所以输出  长度-1  (即剩下一个0即可)
            cout<<len-1<<endl;
        else if(sum0(s,len)>=k)   //如果从n的最后一位起,‘0’的个数>=k,说明能够被10^k整除
            cout<<0<<endl;
        else:
        {
            int sum=0;
            while(1)
            {
                for(int i=len-1;i>=0;i--)   //从最后一位起,先把一个非0的数字变成0(这里注意,题目是说去掉数字,而我非0数字变为0,同时让k++,达到了省时的目的,效果一样)
                {
                    if(s[i]!='0')
                    {
                        s[i]='0';
                        sum++;
                        k++;   //把非0的数字变为0,另外k++,效果一样
                        break;
                    }
                }
                if(sum0(s,len)>=k)   //说明改变后的数字满足题意
                {
                    cout<<sum<<endl;
                    break;
                }
                if(len==k)   //说明只有把这个数字变为0,才能够被10^k整除
                {
                    cout<<len-1<<endl;
                    break;
                }
            }
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值