我从在线评委那里得到错误答案。
#include <stdio.h>
int main(int argc, char * argv[])
{
long long i=0;
long long j=0;
long long p=0;
long long q=0;
long long larger;
long long smaller;
long long cycle_length=1;
long long max_cycle_length=1;
while (scanf("%lld %lld",&p,&q) !=EOF)
{
/*check validity of input*/
if (p <= 0 || p >= 1000000 || q <= 0 || q >= 1000000) continue;
max_cycle_length=1;
if (p > q)
{
larger = p;
smaller = q;
}
else
{
larger = q;
smaller = p;
}
for (i=smaller;i<=larger;i++)
{
cycle_length = 1;
/*printf("i = %lld\r\n",i);*/
j = i;
while (j > 1)
{
/*printf("j = %lld\r\n",j);*/
if ((j % 2) == 0)
{
j = j / 2;
}
else
{
j = 3*j + 1;
}
cycle_length++;
}
if (cycle_length > max_cycle_length)
max_cycle_length = cycle_length;
/*printf("cycle_length = %lld\r\n", cycle_length);*/
}
printf("%lld %lld %lld \r\n",p,q,max_cycle_length);
}
return 0;
}
答案 0 :(得分:3)
您是否使用示例输入和示例输出验证了代码:
Sample Input
1 10
100 200
201 210
900 1000
Sample Output
1 10 20
100 200 125
201 210 89
900 1000 174
我看到的唯一小问题是:
while (scanf("%lld %lld",&p,&q) !=EOF)
应该是:
while (scanf("%lld %lld", &p, &q) == 2)
和
printf("%lld %lld %lld \r\n",p,q,max_cycle_length);
应该是:
printf("%lld %lld %lld\n", p, q, max_cycle_length);
答案 1 :(得分:2)
在线评委是否接受C99
?
long long
(及其printf
转换规范)是C99
类型。它没有由C89
标准定义。
答案 2 :(得分:0)
stdout
开始之前,库也会以文本模式打开 main
。这意味着该库负责管理程序和操作系统之间的换行符差异。
从\r
来电
printf()
(和额外空格)
printf("%lld %lld %lld\n", p, q, max_cycle_length);