题解:P16336 「ALFR Round 10」Another Add Problem

· · 题解

题目传送门

思路

要想让余数最小,则尽量让 n 为 k 的倍数,此时余数为 0 ,为最优解。

所以分以下几种情况讨论:

AC code

#include<iostream>
using namespace std;

int main(){
    int t;
    cin>>t;
    while(t--){
        int n,m,k;
        cin>>n>>m>>k;
        if(m==0){
            cout<<n<<endl;
        }else if(m<=k-n%k){
            cout<<n%k<<endl;
        }else{
            cout<<0<<endl;
        }
    }
    return 0;//完结撒花!!!
}