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,求至少改变数字n多少位的数字使得n的所有位数之和不小于k。
可以直接把n排序一下,然后都变9就行了。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int k,len,sum;
char n[100000+1000];
int main()
{
while(scanf("%d",&k)!=EOF)
{
sum = 0;
scanf("%s",n);
len = (int)strlen(n);
for(int i=0;i<len;i++)
sum += n[i] - '0';
if(sum>=k)
printf("0\n");
else
{
sort(n,n+len);
for(int i=0;i<len;i++)
{
sum += 9 - n[i] +'0';
if(sum>=k)
{
printf("%d\n",i+1);
break;
}
}
}
}
return 0;
}