PAT A1065. A+B and C (64bit)

来源:互联网 发布:影武者 冰川网络 编辑:程序博客网 时间:2024/06/16 12:11

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 (<=10). 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:
31 2 32 3 49223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: falseCase #2: trueCase #3: false
题解
#include<cstdio>#include<iostream>using namespace std;int main() {long a, b, c, temp;;int result[10], n;cin >> n;for (int i = 0; i < n; i++) {scanf("%lld%lld%lld", &a, &b, &c);temp = a + b;if (a > 0 && b > 0 && temp < 0)    //正溢出result[i] = 1;else if (a < 0 && b < 0 && temp>=0) //负溢出result[i] = 0;else if (temp > c)result[i] = 1;elseresult[i] = 0;}for (int i = 0; i < n; i++) {if (result[i])cout << "Case #" << i + 1 << ": true" << endl;elsecout << "Case #" << i + 1 << ": false" << endl;}return 0;}//此题考的是计算机组成原理部分,两个正数相加为负数即为正溢出//两个负数相加为正数即为负溢出,另外此题中A和B并没有取到2^63,//否则要用带负数的大整数运算,因为long long最大范围为2^63(开集)//A+B不能直接在if里和C比较,要间接赋给temp再进行比较