Codeforces Round #667 (Div. 3) D

Decrease the Sum of Digits

在这里插入图片描述
题意: 将一个数加n使其各位上的数字相加之和小于s
思路: 贪心处理,从后向前依次处理,刚开始还想着转化成数组,结果想麻烦了,从后向前处理即可 对于每一位,算一下当前各位数之和是否满足条件,若不满足,加上一个数使其进位
即:n += b - n % b; b *= 10; 最后输出与原数之差即可
代码:

#include<iostream>
#include<queue>
#include<stack>
#include<cstring>
#include<stdlib.h>
#include<cstdio>
using namespace std;
#define LL long long

LL n, s;
int T;

int sum(LL ed){
    LL m = 0;
    while(ed){
        m += ed%10;
        ed /= 10;
    }
    return m;
}

int main(){
    scanf("%d", &T);
    while(T--){
        scanf("%lld%lld", &n, &s);
        //输入;
        LL num = n,b = 10;
        while(sum(n) > s){
            n += b - n % b;
            b *= 10;        
        }
        LL ans = n - num ;
        cout << ans << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值