【发布时间】:2015-12-04 01:47:24
【问题描述】:
编写程序的目的详情见链接:https://www.codechef.com/problems/COOKMACH/
还有错误
超过时间限制
Sub-Task Task # Score Result (time)
1 0 NA AC (0.000000)
1 1 NA TLE (1.010000)
1 2 NA TLE (1.010000)
1 3 NA TLE (1.010000)
Final Score -> 0.000000 Result - TLE
2 4 NA TLE (1.010000)
2 5 NA TLE (1.010000)
2 6 NA TLE (1.010000)
7 NA WA (0.000000)
最终得分 - 0.000000 结果 - TLE
代码是
#include <stdio.h>
int main(void)
{
int test, set, des, a = 1, ctr = 0, str = 0, x;
scanf("%d", &test);
if (test > 0 && test <= 200)
{
for (x = 0; x < test; x++)
{
ctr = 0;
scanf("%d", &set);
scanf(" %d\n", &des);
if ((set > 0 && set <= 10000000) && (des > 0 && des <= 10000000))
{
if (set <= 100 && des <= 100)
{
if (set == des)
ctr = 0;
if (set == 1)
{
while (set != des)
{
set = set * 2;
ctr++;
}
}
else if (set != 1)
{
if (des % 2 == 0)
{
while (a < des)
{
a = a * 2;
str++;
}
}
if (a == des || des == 1)
{
if (set < des)
{
if (set % 2 == 0)
{
while (set != des)
{
set = set * 2;
ctr++;
}
}
else if (set % 2 == 1)
{
set = (set - 1) / 2;
ctr++;
while (set != des)
{
set = set * 2;
ctr++;
}
}
}
if (set > des)
{
if (set % 2 == 0)
{
while (set != des)
{
set = set / 2;
ctr++;
}
}
else if (set % 2 == 1)
{
set = (set - 1) / 2;
ctr++;
while (set != des)
{
set = set / 2;
ctr++;
}
}
}
}
}
}
printf("%d\n", ctr);
}
}
return 0;
}
}
【问题讨论】:
-
“如何克服它?” -- 更快地编写代码。 (抱歉。我知道,这个答案和问题一样有用。) 调试 101:代码、输入、预期输出、观察到的输出。如果可以在不通过外部链接的情况下对这个问题做出某种意义,那就太好了。评论也不会错。
-
@Awildelephant
%d不会扫描\n。它会跳过它们。 -
@Awildelephant 它保留在
stdin。%d,跳过前导空白字符。由于\n是一个空白字符,它会读取并丢弃它并等待一个数字。尝试int num; scanf("%d", &num); printf("num=%d\n", num); scanf("%d, &num); printf("num=%d\n", num);并提供输入,例如2\n3\n。你会发现打印的是 2 和 3 而不是 2 和\n。 -
编辑您的问题。这个想法是它应该简短,但包含回答它所需的所有信息。出于同样的原因,不鼓励仅提供链接的答案:问题和答案都应该对未来的访问者有所帮助。因此,您的问题可能不应该是关于“我如何通过练习 X”,而是更具体。 “我如何找出我的程序浪费时间的地方”,或者“如何改进这个算法”。理想情况下更具体,告诉我们您已经尝试过的具体内容以及您需要帮助的确切。 (我们不关心那个练习,这是你的学习过程。)
-
感谢@DevSolar 我一定会牢记并尽快编辑问题。 :)
标签: c performance profiling time-limiting