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.
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int k,a[100005];
char s[100005];
scanf("%d",&k);
scanf("%s",s);
int sum=0,len=strlen(s);
for(int i=0; i<len; i++)
{
a[i]=s[i]-'0';
sum+=a[i];
}
if(sum>=k)
printf("0\n");
else
{
int w=0;
sort(a,a+len);
for(int i=0;i<len;i++)
{
sum+=9-a[i];
w++;
if(sum>=k)
break;
}
printf("%d\n",w);
}
return 0;
}
本文介绍了一种算法,用于解决给定两个条件:一个数的数位之和至少为k,另一个数n,在不改变数位长度的情况下,找出原始数字与n之间的最小数位差异。
842

被折叠的 条评论
为什么被折叠?



