Codeforces Round #402 (Div. 2)---B. Weird Rounding

  

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, ifk = 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 by10k. 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).

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

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

题意:给你一个数A,然后给你一个数字K,判断能不能通过删除数A中的一些数字使得 A能整除10 的k次幂....

这个题目我其实稍微想了一些时间,最后想通了,能整除10的k次幂的数,这个数肯定末尾有k个0啊。。。(一开始自己还想xjb枚举)

想到这里就好办了...记录每一位的数字是否为0 然后从后往前删...删到末尾k个0

要注意的是,有些特殊情况--比如输入0或数A比10的k次幂更小(这种情况不管怎么删都没有用,直接输出length-1就好了)

我用的是一个string来保存输入的数A,因为这样方便我记录每一位是否为0,以便后续的删除判断

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<string>
 5 #include<stdlib.h>
 6 using namespace std;
 7 int a[11];
 8 
 9 int main(){
10     int  k, n;
11     string s;
12     while(cin>>s){
13         memset(a, 0, sizeof(a));
14         cin >> k;
15         if(s[0]==0){
16             printf("0
");
17             break;
18         }
19         int len = s.length(), count_0 = 0;
20         for(int i=0; i<len; i++){
21             if(s[i]=='0'){
22                 a[i] = 1;
23                 count_0++;
24             }
25         }
26         if(count_0==len-1 && count_0<k)     {
27             printf("%d
", len-1);
28             continue;
29         }
30         if(count_0==len-1 && count_0==k){
31             printf("0
");
32             continue;
33         }    
34         int re_count=0, count_0_0 = 0, i;
35         for(i=len-1; i>=0; i--){
36             if(a[i]==0){
37                 re_count++;
38                 continue;
39             }
40             count_0_0++;
41             if(count_0_0 == k)    break;
42         }
43         if(i==-1)    printf("%d
", len-1);
44         else printf("%d
", re_count);
45     }
46     return 0;
47 }

                        

原文地址:https://www.cnblogs.com/ledoc/p/6445551.html