把所有字符串上的数字加起来,看是否超过k,没有超过k的话,把字符串sort,从第一位开始,将字符变成9,直到sum大于等于k为止。
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
int main ()
{
int i,k,cnt,sum;
string s;
cin >> k;
cin >> s;
i = 0;
sum = 0;
while(i<s.length())
{
sum += (s[i]-'0');
i++;
}
if(sum>=k)
{
cout << "0" << endl;
}
else
{
i = 0;
cnt = 0;
sort(s.begin(),s.end());
while(i<s.length())
{
sum += (9-(s[i]-'0'));
i++;
cnt++;
if(sum>=k)
break;
}
cout << cnt << endl;
}
return 0;
}