Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.
You have to find the minimum number of digits in which these two numbers can differ.
The first line contains integer k (1 ≤ k ≤ 109).
The second line contains integer n (1 ≤ n < 10100000).
There are no leading zeros in n. It's guaranteed that this situation is possible.
Print the minimum number of digits in which the initial number and n can differ.
3 11
1
3 99
0
In the first example, the initial number could be 12.
In the second example the sum of the digits of n is not less than k. The initial number could be equal to n.
题意:一个自然数,各个数字之和不小于k,把它改成n,至少改了多少位? 改变前后,位数不变
思路:把每一位都置为9,并把n排序,从最后一位到第一位,如果该位是9,则不用改,如果该位上的9改成n对应的数之后,总位数之和依然不小于k,则不用改,否则需要改
AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=100000+10;
char a[maxn];
int main()
{
int k;
while(scanf("%d %s",&k,a)!=EOF)
{
int len=strlen(a);
int sum=9*len;
int ans=0;
sort(a,a+len);
for(int i=len-1;i>=0;i--)
{
if(a[i]=='9')
continue;
else
{
int num='9'-a[i];
sum-=num;
if(sum<k)
ans++;
else
{
continue;
}
}
}
cout<<ans<<endl;
}
return 0;
}