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;
}
}