【Codeforces Round #427 (Div. 2) B】The number on the board

Link:http://codeforces.com/contest/835

Description

原本有一个数字x,它的各个数码的和原本是>=k的;
现在这个数字x,在不改变位数的情况下,变成了n;
问你n和原来的数字x最少可能有多少位不一样.
(x是未知的)

Solution

如果各位大于等于k,直接输出0;
否则,先把n小的数码加到9;
一直加知道大于等于k;

NumberOf WA

0

Reviw


Code

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5;

int k,a[N+100],sum,now;
char s[N+100];

main(){
    scanf("%lld",&k);
    scanf("%s",s+1);
    int len = strlen(s+1);
    for (int i = 1;i <= len;i++)
        a[i] = s[i]-'0',sum+=a[i];
    sort(a+1,a+1+len);
    now = 0;
    while (sum < k){
        now++;
        sum+=(9-a[now]);
    }
    printf("%lld\n",now);
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(151)  评论(0编辑  收藏  举报