题目: B. The number on the board
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.
思路:n最大值可为10^100000,共100001位,所以不能用int,要用字符串输入。先假设那个数初始值为n,如果n的数位和大于k就不用改变数位,结果为0。否则逐渐将其中数位最小值变成9直到满足题目条件,累计改变数位数。
CODE:
#include<bits/stdc++.h>
using namespace std;
char s[1000005];
int main()
{
int k,a[10],i,sum,num,maxr;
while(~scanf("%d%s",&k,s)){
memset(a,0,sizeof(a));
num=sum=0;
maxr=-1;
for(i=0;s[i];i++){
int t=s[i]-'0';
maxr=max(maxr,t);
a[t]++;
sum+=t;
}
if(sum>=k) puts("0");
else{
int flag=0;
for(i=0;i<=maxr;i++){
while(a[i]){
a[i]--;
sum-=i;
sum+=9;
num++;
if(sum>=k){
flag=1;
break;
}
}
if(flag){
printf("%d\n",num);
break;
}
}
}
}
return 0;
}