The number on the board

题目链接  http://codeforces.com/problemset/problem/835/B

B. The number on the board
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
3
11
output
1
input
3
99
output
0
题目大意:

有一个数被替换掉了几个数字,现在只知道被改过的数,题目问被改过的数字至少改掉几个数字后能够回到原数(只要是符合各位数字之和>=k即可,所以原数有好多种可能,只要满足一种就行)
第一行的3表示原数每位数字之和>=3。第二行11表示被改过的数。只要把1改成2或者3或者4......(满足之和>=3)就行。所以改变一位数字即可,答案输出1。
解题思路:
k用整型存储没问题,但是n太大了,必须用字符串存储。先求n的各位数字之和,判断是否>=k。如果>=k,则输出0;如果不,就按从小到大的顺序排序n字符串,然后从头开始(也就是把最小数换成'9',然后判断改变后之和是否>=k),如果不,就继续吧第二小的数字改成‘9’,直到满足之和>=k,然后输出循环的次数就是答案。
代码:
#include<iostream>
#include<algorithm>                 //sort排序函数需要用到的头文件
using namespace std;
int main()
{
    long long int k;
    string s;
    while(cin>>k>>s)
    {
        int summ=0,i;
        sort(s.begin(),s.end());    //把s字符串从小到大排序
        long long int sum=0;        //用于存储各位数字的和
        for(int i=0;i<=s.size()-1;i++)   //计算各位数字之和
            sum=sum+s[i]-'0';
        if(sum>=k)                      //如果各位数字之和满足题意,说明不用改变数字,输出0
            cout<<0<<endl;
        else
        {

            k=k-sum;                    //否字的话,让k存储 满足题意的各位数字之和 与 目前各位数字之和 的 差值
            for(i=0;i<=s.size()-1;i++)
            {
                summ=summ+'9'-s[i];           //从小的数字开始,计算如果把小的数字改为‘9’后,与目标(即上述k)的关系
                if(summ>=k)                   //如果等于或者超过k,则满足题意
                    break;
            }
            cout<<i+1<<endl;                 //i即为改变数字的个数
        }
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Given the grid below for the game of ACSL Patolli, utilize the following rules to play the game. All rules must be applied in the sequential order listed. 1 . There are 2 players. Each player has 3 markers. 2. The markers move according to the roll of a die (1 – 6). 3. Markers move in numerical order around the grid. 4. If, on a die roll, a marker lands on an occupied location, then that marker loses its turn and remains at its previous location. 5. A marker can jump over another marker on its way to finish its move. 6. A marker finishes its way around the grid when it lands on location 52. It is then removed from the board. A move can’t take a marker beyond location 52. If it does, the marker remains at its previous location. 7. If, on a die roll, a marker lands on an unoccupied location that is a prime number, the marker then moves six locations forward. However, it stops immediately before any occupied location. 8. If, on a die roll, a marker lands on an unoccupied location that is a perfect square greater than 4, the marker then moves 6 locations backwards. However, it stops immediately before any occupied location. 9. If, on a die roll, a marker lands on an unoccupied location that is neither a prime number nor a perfect square, then determine if the marker made at least one horizontal move followed by at least one vertical move (such as going from 6 to 8, 11 to 13, 26 to 28 … but not 2 to 4 or 30 to 32). In that case, the marker can only land on a location on its path that is a multiple of the die roll value even if it moves a smaller distance than the die roll value. However, if all the locations in its path that are multiples are occupied, then the marker does not move from its current location. The rules listed in #7 and #8 do not apply when using #9.
最新发布
03-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值