zoukankan      html  css  js  c++  java
  • 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
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    FastAPI项目实战: 个人博客项目的API
    Jmeter分布式执行,java.rmi.UnmarshalException: xxxAbstractSimpleThreadGroup错误
    [转]JMeter分布式的坑
    Docker菜鸟教程-硬敲系列
    VMware EXIS 安装
    2020简单总结
    07.1 迭代器、生成器
    locust 的 ‘1’ 版本时代变化
    移动端专项测试-内存泄漏
    乘风破浪的不止姐姐,还有我们的测试工程师!
  • 原文地址:https://www.cnblogs.com/PennyXia/p/12284932.html
Copyright © 2011-2022 走看看