A+B and C (64bit) (20)----正溢和负溢

    xiaoxiao2021-03-25  59

    A+B and C (64bit) (20)

    时间限制 1000 ms  内存限制 65536 KB  代码长度限制 100 KB  判断程序 Standard  (来自 小小)

    题目描述

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

    输入描述:

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

    输出描述:

    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).

    输入例子:

    3 1 2 3 2 3 4 9223372036854775807 -9223372036854775808 0

    输出例子:

    Case #1: false Case #2: true Case #3: false 题目链接:https://www.nowcoder.com/pat/5/problem/4116

    牛客网上的一道题,要不是我最近刚学了计算机组成原理我还真不会这个,可能就只能上java了,这个题的意思是让你判断a+b是不是大于c。

    注意数的范围,两个long long相加,结果可能超出long long,所以说我们需要判断是不是溢出了,详见计算机组成原理,两个正数相加如果是负数,则溢出,两个负数相加如果是正数,则溢出,和计算机储存机制有关(二进制)

    代码:

    #include <cstdio> #include <cstring> #include <iostream> using namespace std; int main(){ int n; long long a,b,c,res; scanf("%d",&n); int tcase=1; while(n--){ int flag=0; scanf("%lld%lld%lld",&a,&b,&c); res=a+b; if(res>=0&&a<0&&b<0){ flag=0; } else if(res<=0&&a>0&&b>0){ flag=1; } else if(res>c){ flag=1; } if(flag==1) printf("Case #%d: true\n",tcase++); else printf("Case #%d: false\n",tcase++); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-40248.html

    最新回复(0)