The number on the board

版权声明:如有错误,请指出,不胜感激。 https://blog.csdn.net/qq_36424540/article/details/76474735

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.
Input
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.
Output
Print the minimum number of digits in which the initial number and n can differ.
Examples
Input
3
11
Output
1
Input
3
99
Output
0
Note
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.

一道水题,每位数字最大到9,如果总和比k小的话,我们就把差最大的改为9。

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int Max=1e5+10;

char str[Max];
int cha[Max];


int cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int k;
    scanf("%d",&k);
    getchar();
    gets(str);
    int sum_digit=0;
    int len=strlen(str);

    for(int i=0;i<len;i++)
    {
        cha[i]=9-str[i]+'0';
        sum_digit += str[i]-'0';//计算总和
    }


    if(sum_digit>=k)
        printf("0\n");
    else
    {
        int sum=0;
        sort(cha,cha+len,cmp);
        for(int i=0;i<len;i++)
        {
            sum += cha[i];
            if(sum+sum_digit>=k)
            {
                printf("%d\n",i+1);
                break;
            }
        }
    }
    return 0;
}
阅读更多
换一批

没有更多推荐了,返回首页