标签:style c class blog code java
链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1449+
题意:给出三个整数A,B,C,判断A+B与C的大小关系(-2^63<=A,B,C<2^63)。若A+B>C,输出“>”,若A+B=C,输出“=”,若A+B<C,输出“<”。
直接的想法可以模拟65位加法(二进制);
或者对可能溢出的情况做一些处理,具体见代码
# include <stdio.h> typedef long long int lli; # define sign(x) ((x)>>63) # define lsb(x) ((x)&((lli)0x1)) # define maskit(x) (lsb(x) | ((sign(x)<<31))) lli a, b, c; char solve(void) { if (sign(a) == sign(b)) { lli x = a>b ? (b+(a-b)/2):(a+(b-a)/2); lli y = c/2; if (x<y) return ‘<‘; else if (x>y) return ‘>‘; else { int s = sign(a); if (sign(a)) { a = -a; b = -b; c = -c; } int ia = maskit(a); int ib = maskit(b); int ic = maskit(c); // printf("%d %d %d\n", ia, ib, ic); if (s) { if (lsb(ia+ib) < ic) return ‘>‘; else return lsb(ia+ib)==ic ? ‘=‘:‘<‘; } else { if (lsb(ia+ib) < ic) return ‘<‘; else return lsb(ia+ib)==ic ? ‘=‘:‘>‘; } } } else { lli x = (a+b); if (x<c) return ‘<‘; else return x==c ? ‘=‘:‘>‘; } } int main() { int T; scanf("%d", &T); for (int i = 0; i < T; ++i) { scanf("%I64d%I64d%I64d", &a, &b, &c); printf("%c\n", solve()); } return 0; }
CSU 1449: A+B and C,布布扣,bubuko.com
标签:style c class blog code java
原文地址:http://www.cnblogs.com/txd0u/p/3754554.html