CodeForces-835B |The number on the board|
CF_835B 黑板上的数字
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.
题目大意:
板上写着一些自然数。它的数字总和不小于k。
但是你有点分心,有人把这个数字改成了n,用其他数字代替了一些数字。众所周知,数字的长度没有变化。
你必须找到这两个数字可以不同的最小位数。
首先算出数字n的各位数之和,如果大于等于k,则不需要改动,可不同的最小位数是0;
如果小于k,找出n中最小的数字,替换为9,再次判断。
题目链接:codeforces-835B
AC代码
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string s;
int main() {
int k, len, cnt = 0, sum = 0, i, temp;
cin >> k >> s;
len = s.length();
for (i = 0; i < len; i++) {
sum += s[i] - '0';
}
sort(s.begin(), s.end());
while (sum < k) {
temp = s[cnt++] - '0';
sum += (9 - temp);//替换为9,不是加9,所以是加上(9 - 原有的数)
}
printf("%d\n", cnt);
return 0;
}