首页 > 代码库 > Codeforces Round #402 (Div. 2) B
Codeforces Round #402 (Div. 2) B
Description
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 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.
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.
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).
30020 3
1
100 9
2
10203049 2
3
In the example 2 you can remove two digits: 1 and any 0. The result is number 0 which is divisible by any number.
题意:删除第一个数字某些位数,可以使得被10K整除,求最少次数
解法:倒着遍历记录0的个数,以及不为0的个数,如果0的个数等于K,跳出遍历,输出不为0的个数,还有始终达不到K的情况,因为题目说一定有解,那么最后留下来的只有0,也就是输出数字的长度-1
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 int main() 5 { 6 string s; 7 ll num=0; 8 ll ans=0; 9 ll k; 10 cin>>s>>k; 11 for(int i=s.size()-1; i>=0; i--) 12 { 13 if(s[i]==‘0‘) 14 { 15 num++; 16 if(num>=k) 17 { 18 break; 19 } 20 } 21 22 else 23 { 24 ans++; 25 } 26 } 27 if(num==k) 28 { 29 cout<<ans<<endl; 30 } 31 else 32 { 33 cout<<s.size()-1<<endl; 34 } 35 return 0; 36 }
Codeforces Round #402 (Div. 2) B