栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

#1059. A+B and C (64bit)【模拟】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

#1059. A+B and C (64bit)【模拟】

原题链接

Problem Description:

Given three integers A , B A, B A,B and C C C in $(-2^{63}, 2^{63}), you are supposed to tell whether A + B > C A + B > C A+B>C.

Input Specification:

The first line of the input gives the positive number of test cases, T ( ≤ 10 ) T(leq 10) T(≤10). Then T T T test cases follow, each consists of a single line containing three integers A , B A, B A,B and C C C, separated by single spaces.

Output Specification:

For each test case, output in one line Case #X: true if A + B > C A + B > C A+B>C, or Case #X: false otherwise, where X X X is the case number (starting from 1).

Sample Input:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false
Problem Analysis:

本题涉及到 64 64 64 位整数相加,相加过程会爆 long long,因此我们将所有情况列出来,进行判断即可。

  1. a > 0 , b > 0 , a + b < 0 a > 0, b > 0, a + b < 0 a>0,b>0,a+b<0,正向溢出,此时 a + b a + b a+b 一定大于 c c c。
  2. a < 0 , b < 0 , a + b ≥ 0 a < 0, b < 0, a + b geq 0 a<0,b<0,a+b≥0,反向溢出,此时 a + b a + b a+b 一定小于 c c c。
  3. 均无溢出,此时直接判断 a + b a + b a+b 是否大于 c c c 即可。
Code
#include 
#include 
#include 
#include 

using namespace std;

typedef long long LL;

bool check(LL a, LL b, LL c)
{
    LL res = a + b;
    if (a > 0 && b > 0 && res < 0) return true;
    if (res > c) return true;
    return false;
}

int main()
{
    int T;
    cin >> T;
    for (int i = 1; i <= T; i ++ )
    {
        LL a, b, c;
        cin >> a >> b >> c;
        if (check(a, b, c)) printf("Case #%d: truen", i);
        else printf("Case #%d: falsen", i);
    }
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/290538.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号