Day 10 D. Decrease the Sum of Digits

Problem:
You are given a positive integer n. In one move, you can increase n by one (i.e. make n:=n+1). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of n be less than or equal to s.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow.

The only line of the test case contains two integers n and s (1≤n≤1018; 1≤s≤162).

Output
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of n be less than or equal to s.

Example
input
5
2 1
1 1
500 4
217871987498122 10
100000000000000001 1

output
8
0
500
2128012501878
899999999999999999

题目大致意思为:输入n和s 你可以对n进行每次+1的操作 求最少多少次能让n的每个位上的数加起来不大于s

#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long 
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        ll n, s;
        cin >> n >> s;
        vector <int> v;
        ll nn = n;
        while (nn)
        {
            v.push_back(nn % 10);
            nn /= 10;
        }
        v.push_back(0);//补一位0 便于进位
        ll sum = 0;
        ll i;
        for (i = v.size() - 1; i >= 0; i--)//从高位开始取     
        {
            sum += v[i];
            if (sum > s)//如果有位数取到了比s大的 或是多个位数加起来比s大的 退出循环  
            {
                break;
            }
        }
        if (sum <= s)//如果sum加起来的结果不大于s 说明不需要操作次数就可以满足题目要求
        {
            cout << 0 << endl;
            continue;
        }
        ll j;
        for (j = i; j <= v.size() - 1; j++)//进一位的操作 
        {
            sum -= v[j];
            v[j + 1]++;
            sum++;
            if (sum <= s)
            {
                break;
            }
        }
        ll res = 0;
        for (i = v.size() - 1; i >= 0; i--)//进一位后 满足题意的数字
        {
            res *= 10;
            if (i > j)
            {
                res += v[i];
            }
        }
        cout << res - n << endl;//满足题意的操作次数
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
问题要求找到能够使得批评者的总批评水平最小的方案。我们可以采取贪心算法来解决这个问题。 中文解释: 1. 首先,我们可以创建一个数组`criticism`,大小为n,用来存储每个观众的初始批评水平。 2. 然后,我们按照观众的座位编号从左到右遍历每个批评者和他们的观察范围(li到ri)。 3. 对于每个批评者的观察范围内的观众,我们计算将他们的批评水平减少到0(或尽可能接近0而不为负)所需的最小物品数量。我们将这个值命名为`min_items`。 4. 将`min_items`从观众的批评水平中减去。 5. 记录所有批评者的`min_items`的总和。 6. 遍历完所有批评者后,返回`min_items`的总和。 下面是对应的C++代码实现: ```cpp #include <iostream> #include <vector> int minCriticismLevels(int n, int m, std::vector<int>& a, std::vector<std::pair<int, int>>& critics, int k) { std::vector<int> criticism(a.begin(), a.end()); // 初始化批评水平数组 int totalMinItems = 0; for (int i = 0; i < m; i++) { int minItems = 0; int criticStart = critics[i].first; int criticEnd = critics[i].second; // 遍历批评者的观察范围 for (int j = criticStart - 1; j <= criticEnd - 1; j++) { if (criticism[j] > 0) { // 计算将观众的批评水平减少到0所需的最小物品数量 int itemsNeeded = std::min(criticism[j], k); minItems += itemsNeeded; criticism[j] -= itemsNeeded; k -= itemsNeeded; } } totalMinItems += minItems; } return totalMinItems; } int main() { int n, m, k; std::cin >> n >> m >> k; std::vector<int> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } std::vector<std::pair<int, int>> critics(m); for (int i = 0; i < m; i++) { std::cin >> critics[i].first >> critics[i].second; } int minSumCriticismLevels = minCriticismLevels(n, m, a, critics, k); std::cout << minSumCriticismLevels << std::endl; return 0; } ``` 请注意,这只是一个基本的实现示例,并未进行错误处理和输入验证。在实际应用中,您可能需要根据实际情况进行更多的输入检查和错误处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值