题解 | #A+B+C Problem#

A+B+C Problem

https://ac.nowcoder.com/acm/contest/20862/A

链接:https://ac.nowcoder.com/acm/contest/20862/A 来源:牛客网

**题目描述 ** HZ is doing homework. The answer to a problem is three integers A, B, CA,B,C. After trying for several hours, he finally solved it. But unfortunately, he lost the value of CC by accident. Besides the values of AA and BB, he only remembers that 0 \leq A,B,C < 10^60≤A,B,C<10 6 and A+B+CA+B+C is a multiple of 10^610 6 . But HZ is so tired that he doesn't want to solve the original problem again. Can you help him calculate the value of CC?

输入描述: The first line of input contains an integer TT (1 \leq T \leq 10^51≤T≤10 5 ), denoting the number of test cases. Each test case contains two integers AA and BB in one line. 0 \leq A,B < 10^60≤A,B<10 6 .

题目大意:总共三个数,a,b,c,已知数a,b,求c,其中a,b,c都是【0,1e6)范围内的数,且三数之和是1e6的倍数。

思路:这是简单的猜数问题,因为三个数都是比1e6小,而三数之和需要是1e6的倍数,所以三数之和按每个数取极限刚小于1e6,三数之和也只能是1e6,2e6(3e6取不到),其中0也是它的倍数(这个没有就wa了) 所以只要找到1e6-a-b且大于0或者2e6-a-b且大于0的数就可以了

代码如下:

using namespace std;
#define maxn1 1000000
#define maxn2 2000000
int main()
{
    long long int t;
    scanf("%lld",&t);
    while(t--)
    {
        long long int a,b;
        scanf("%lld %lld",&a,&b);
        long long int ans;
        if(((ans=maxn1-a-b)>=0)&&(ans<maxn1))ans=maxn1-a-b;
        else if(((ans=maxn2-a-b)>=0)&&(ans<maxn1))ans=maxn2-a-b;
        else if(a==0&&b==0)ans=0;//我的天,不是吧,0居然是那个的倍数
        else ans=-1;
        printf("%lld\n",ans);
    }
    return 0;
}

哪里没有讲清楚的欢迎评论区留言哦

全部评论
好惨,孤伶伶的
点赞 回复
分享
发布于 2021-10-17 09:54
能讲一下I吗
点赞 回复
分享
发布于 2021-10-17 11:58
滴滴
校招火热招聘中
官网直投

相关推荐

2 收藏 评论
分享
牛客网
牛客企业服务