思路 CodeForces 835B

Some natural number was written on the board. Its sum of digits was not less thank. But you were distracted a bit, and someone changed this number ton, 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.

Example
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 ton.


題意:要使第二個數的每一位相加之和大於第一個數,至少需要改變第二個數中的幾位數字。1->2\3\4\5\6\7\8\9
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char k[100005];
int b[100005];
int main()
{
	int n;

	scanf("%d %s",&n,k);
		int len = strlen(k);
		int sum = 0;
		for(int i = 0; i < len; i++)
		{
			b[i] = k[i]-'0';
			sum = sum + b[i];
		}
		sort(b,b+len);
		int count = 0;
		if(sum >= n) { printf("0\n");return 0;}//先判斷一次
		for(int i = 0; i < len; i++)
		{
			sum = sum + 9 - b[i];
			count ++ ;
			if(sum >= n) { printf("%d\n",count); return 0;} 
		}	
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char k[100005];
int b[100005];
int main()
{
	int n;
	while(~scanf("%d%s",&n,k))
	{
		int len = strlen(k);
		int sum = 0;
		for(int i = 0; i < len; i++)
		{
			b[i] = k[i]-'0';
			sum = sum + b[i];
		}
		sort(b,b+len);
		int count = 0;
		for(int i = 0; i <= len; i++)//注意要多判斷一次因爲是先判斷再加
		{
			if(sum >= n) { printf("%d\n",count);break;} 
			sum = sum + 9 - b[i];
			count ++ ;
		}
		
	}
	
}




發佈了46 篇原創文章 · 獲贊 6 · 訪問量 5441
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章