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)
  • 相关阅读:
    YII框架实现排序
    YII2 实现登录时候修改最新登录时间
    YII框架下实现密码修改
    json在PHP中应用技巧
    更换Python pip库镜像地址
    Python3创建RIDE桌面快捷方式的另一种方法
    谈谈测试人员的基本素养
    《微软的软件测试之道》阅读笔记
    PPT如何一页多张打印且铺满整个页面
    Linux 在线模拟器
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12828634.html
Copyright © 2011-2022 走看看