CodeForces-835B |The number on the board| 黑板上的数字

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;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值