• A1065 A+Band C(64 bit)


    Given three integers A, B and C in [−263,263 ),you are supposed to tell whether A+B>C.

    Input Specification:

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

    Output Specification:

    For each test case, output in one line Case #X: true if A+B>C, or Case #X: false otherwise, where 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

    思路:

    A+B相加之后可能会溢出,溢出的情况根据溢出后的数值分类讨论

    问题:

    部分测试点没通过

    解决:

    •A+B必须放在long long型变量中才可以与C比较,而不是在if的条件中直接比较,即 long long sum=A+B; if(sum>C)

    •A B 最小值都是-263,相加得-264,故sum溢出后最小为0

     1 #include <iostream>
     2 using namespace std;
     3 int main() {
     4     long long A, B, C;
     5     int n;
     6     cin >> n;
     7     for (int i = 0; i < n; i++) {
     8         cin >> A >> B >> C;
     9         long long sum = A + B;
    10         if (A < 0 && B < 0 && sum>=0)
    11             printf("Case #%d: false
    ", i+1);
    12         else if (A > 0 && B > 0 && sum< 0)
    13             printf("Case #%d: true
    ", i+1);
    14         else if (sum> C)
    15             printf("Case #%d: true
    ", i + 1);
    16         else
    17             printf("Case #%d: false
    ", i+1);    
    18     }
    19     return 0;
    20 }
    作者:PennyXia
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    PHP中的traits
    MySQL规范与疑难杂症
    从关系型数据库到非关系型数据库
    putty详细使用说明
    MySQL时间相关处理
    sendmail
    MongoDB简明教程
    assert()和eval()
    在NS3下如何用Python脚本,以及PyViz可视化
    八卦一下模型检验(二) (转自g9老大的博文)
  • 原文地址:https://www.cnblogs.com/PennyXia/p/12284932.html
Copyright © 2020-2023  润新知