1.题目描述:
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.
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.
给你一个不超过2e9的整数,你可删去其中任意一位数,要你算最少减去多少,使得新的数能够被10^k整除(0也算被整除)
3.解题思路:
首先满足整除10^k的数,末尾至少有k个0;那么我们对于原来那个数只要从末尾开始贪心,如果不是0,那么必定要删掉,如果是0则统计,然后特判一下只有0的时候。
最开始这题往dfs想去了,真心把简单问题复杂化了。
4.AC代码:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define N 12
#define eps 1e-6
#define pi acos(-1.0)
#define e 2.718281828459
#define mod (int)1e9 + 7
using namespace std;
typedef long long ll;
char num[N], k;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
while (~scanf("%s%d", num, &k))
{
int cnt_zero = 0, cnt_other = 0;
int len = strlen(num);
for (int i = len - 1; i >= 0; i--)
{
if (num[i] == '0')
{
cnt_zero++;
if (cnt_zero == k)
break;
}
else
cnt_other++;
}
if (cnt_zero == k)
printf("%d\n", cnt_other);
else
printf("%d\n", len - 1);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}