我如何使用 16 位整数类型?
how can i work with 16 digit integer type?
问题: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;
}
一个int
的最大值是2,147,483,647。将变量改为unsigned long long
,最多可容纳18,446,744,000,000,000,000多个。 (您需要使用 %llu
来读取 in/out unsigned long long
变量)
此外,始终验证输入,这会暗示问题。
if(scanf("%d", &a) < 1) { //if we read less than one
printf("FAILED TO READ INPUT");
return 1;
}
最后,您要从 2 迭代到 1000000000000000,无论循环内有什么,这都将花费很长时间。您需要使用更快的算法才能在一秒的期限内完成。
问题: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;
}
一个int
的最大值是2,147,483,647。将变量改为unsigned long long
,最多可容纳18,446,744,000,000,000,000多个。 (您需要使用 %llu
来读取 in/out unsigned long long
变量)
此外,始终验证输入,这会暗示问题。
if(scanf("%d", &a) < 1) { //if we read less than one
printf("FAILED TO READ INPUT");
return 1;
}
最后,您要从 2 迭代到 1000000000000000,无论循环内有什么,这都将花费很长时间。您需要使用更快的算法才能在一秒的期限内完成。