问题:http://codeforces.com/problemset/problem/486/A
输入:
1000000000000000
输出:
-1
答案:
500000000000000
检查日志:
wrong answer 1st numbers differ - expected: '500000000000000', found: '-1'
代码:
int main() {
int a, b = 0, c = 0, d, i;
scanf("%d", &a);
for (i = 2 ; i <= a ; i++) {
if (i % 2 == 0) {
c = c + i;
}
if (i % 2 != 0) {
b = b + i;
}
}
d = c - b;
d = d - 1;
printf("%d", d);
return 0;
}
答案 0 :(得分:3)
int
可以容纳的最大值是2,147,483,647。将变量更改为unsigned long long
,最多可容纳18,446,744,000,000,000,000。 (您需要使用%llu
来读入/读出unsigned long long
变量)
此外,始终验证输入,这可能暗示了问题。
if(scanf("%d", &a) < 1) { //if we read less than one
printf("FAILED TO READ INPUT");
return 1;
}
最后,你将从2迭代到1000000000000000,无论循环内部是什么,这都需要很长时间。您需要使用更快的算法才能在一秒钟内完成。