#include <iostream>
using namespace std;
int ABandC(int a, int b, int c)
{
bool samble_a = (a > 0);
bool samble_b = (b > 0);
bool samble_c = (c >= 0);
if (samble_a != samble_b)
{
return (( a+b ) > c );
} else {
if (samble_b == samble_c) {
return ( a > (c - b) );
} else {
return (a > c);
}
}
}
int main()
{
int idx;
int result[10];
int testgrp = 0;
int a,b,c;
cin >> testgrp;
idx = 0;
while (idx < testgrp) {
cin >> a >> b >> c;
result[idx++] = ABandC(a,b,c);
}
idx = 0;
while (idx < testgrp)
{
if (result[idx++]) {
cout << "Case #" << idx << ": true" << endl;
} else {
cout << "Case #" << idx << ": false" << endl;
}
}
using namespace std;
int ABandC(int a, int b, int c)
{
bool samble_a = (a > 0);
bool samble_b = (b > 0);
bool samble_c = (c >= 0);
if (samble_a != samble_b)
{
return (( a+b ) > c );
} else {
if (samble_b == samble_c) {
return ( a > (c - b) );
} else {
return (a > c);
}
}
}
int main()
{
int idx;
int result[10];
int testgrp = 0;
int a,b,c;
cin >> testgrp;
idx = 0;
while (idx < testgrp) {
cin >> a >> b >> c;
result[idx++] = ABandC(a,b,c);
}
idx = 0;
while (idx < testgrp)
{
if (result[idx++]) {
cout << "Case #" << idx << ": true" << endl;
} else {
cout << "Case #" << idx << ": false" << endl;
}
}
}
根据网友思想解决问题:
关键是处理溢出问题,溢出情况有:
a和b都是正数,a+b >= 2^32;
a和b都是负数,a+b <= -2^32;
因此溢出只出现在a b 同号,a b异号的话直接按 (a+b) > c 来算就可以。
对于a b同号,也有两种处理方法:
1. b c 同号, 将 (a+b) > c 转换成 a > (c - b), 此时 (c - b)必定不会出现溢出;
1. b c 异号, 由于式子左边同号,相加后符号不变,式子右边跟左边异号,因此不需要计算即可知道大小关系。
网上大部分说法是定义long long防止溢出,但是如果题目 A B C的范围变为long long 该如何定义呢?