zoukankan      html  css  js  c++  java
  • 1065 A+B and C (64bit)

    Given three integers A, B and C in [−], 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

    题意:

      给出三个数字,计算两个数字之和是否大于第三个数字。

    思路:

      因为相加的过程中可能产生溢出,所以要考虑到溢出的情况。所有可能的溢出情况是:正 + 正 = 负; 负 - 负 = 正(0)。如果是第一种溢出情况的话,两数之和一定大于第三个数;如果是第二种溢出的话,两数之和一定小于第三个数。

    Code:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int main() {
     6     int n;
     7     long long a, b, c, sum;
     8     cin >> n;
     9     for (int i = 1; i <= n; ++i) {
    10         cin >> a >> b >> c;
    11         sum = a + b;
    12         if (a > 0 && b > 0 && sum < 0)
    13             cout << "Case #" << i << ": true" << endl;
    14         else if (a < 0 && b < 0 && sum > 0)
    15             cout << "Case #" << i << ": false" << endl;
    16         else {
    17             if (sum > c)
    18                 cout << "Case #" << i << ": true" << endl;
    19             else
    20                 cout << "Case #" << i << ": false" << endl;
    21         }
    22     }
    23     return 0;
    24 }

    思考:

      为什么负数溢出可能为0,而整数不能为0呢?

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    npm publish 失败可能的原因记录
    nodejs版实现properties后缀文件解析
    CSS 毛玻璃效果
    echarts markLine 辅助线非直线设置
    sql 数据类型 建表时如何选择数据类型
    用row_nuber 分页的存储过程
    错误描述:未能找到路径“C:/”的一部分
    设置VS2010默认以管理员权限启动
    通过做nopcommerce电商项目对EF的理解(一)
    获取多表联合查询的存储过程。
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12828634.html
Copyright © 2011-2022 走看看