首页 > 学院 > 开发设计 > 正文

Codeforces 779B-Weird Rounding

2019-11-06 09:16:29
字体:
来源:转载
供稿:网友

Weird Roundingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard 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 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).

Examplesinput
30020 3output
1input
100 9output
2input
10203049 2output
3Note

In the example 2 you can remove two digits: 1 and any 0. The result is number 0 which is divisible by any number.

题意:给出数字n和数字k,要求去掉n中最少的数字,使得10^k能被得到的数整除

解题思路:若要10^k能被整除,那么这个数要么是0要么至少有k个零,又因为去掉的数字个数要尽量少,因此数字n从右往左若有大于等于k个的零,那么只要去掉从右往左数第k个零之前的所有非零数,否则就要去一直去掉数,直至只剩一个零

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longint main(){    LL n;    int k,kk,nn,a[20];    while(~scanf("%lld %d",&n,&k))    {        kk=k,nn=n;        int cnt=0,sum=0,ans=0;        while(nn) a[cnt++]=nn%10,nn/=10;        if(n==0) cnt=1;        for(int i=0;i<cnt;i++)        {            if(!a[i]) sum++;            else ans++;            if(sum==kk) break;        }        if(sum==kk) printf("%d/n",ans);        else printf("%d/n",cnt-1);    }    return 0;}


上一篇:二叉树的非递归遍历

下一篇:监督学习

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表