(PAT) A+B和C (15)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36984327/article/details/83211674

题目

A+B和C

解题思路

如果a,b为异号相加,那么就不会出现溢出现象。
同理 a+b>c 如果 a,b,c是同号,可以修改为 a>c-b,那么 c + (-b)就是异号相加,就不会出现溢出,即可比较大小

代码

#include <iostream>
using namespace std;

int main()
{
    int a, b, c;
    int t; cin >> t;
    int i = 0;
    while(i<t)
    {
        cin >> a >> b >> c;
        cout << "Case #" << (++i) << ": ";
        if(a>0 && b > 0)
        {
            if(c > 0) cout << (a > (c-b) ? "true" : "false") << endl;
            else cout << "true" << endl;
        }
        else if(a < 0 && b < 0)
        {
            if(c > 0) cout << "false" << endl;
            else cout << (a > (c-b) ? "true" : "false") << endl;
        }
        else
        {
            cout << ((a+b)>c ? "true" : "false") << endl;
        }
    }
    
    
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36984327/article/details/83211674