Codeforces Round #402 (Div. 2) B題
Polycarp is crazy about round numbers. He especially likes the numbers divisible by10k.
In the given number of n Polycarp wants to remove the least number of digits to get a number that is divisible by10k. For example, ifk = 3, in the number 30020 it is enough to delete a single digit (2). In this case, the result is3000
Write a program that prints the minimum number of digits to be deleted from the given integer numbern, 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 number0, which is required to be written as exactly one digit).
It is guaranteed that the answer exists.
InputThe 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.
OutputPrint w
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次方整除,一定保證解的存在。
解法:直接從後向前掃描,記錄掃描到幾個0,不是0的話答案就要加一,0的個數達到k個就可以終止,如果掃描完不足k個零那麼答案就是原長度減一
程式碼:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[15];
int k;
int main()
{
scanf("%s %d",str+1,&k);
int len=strlen(str+1);
if(len<k+1){
printf("%d\n",len-1);
}else if(len==k+1){
int flag=0;
for(int i=len;i>=2;i--){
if(str[i]!='0') flag=1;
}
if(flag)printf("%d\n",len-1);
else printf("0\n");
}else{
int cnt0=0;
int ans=0;
for(int i=len;i>=1;i--){
if(str[i]=='0')cnt0++;
else ans++;
if(cnt0==k)break;
}
printf("%d\n",ans);
}
return 0;
}