【问题标题】:I have a Time Limit Exceeded error in C . How do I overcome it?我在 C 中有一个 Time Limit Exceeded 错误。我该如何克服它?
【发布时间】: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", &amp;num); printf("num=%d\n", num); scanf("%d, &amp;num); printf("num=%d\n", num); 并提供输入,例如2\n3\n。你会发现打印的是 2 和 3 而不是 2 和 \n
  • 编辑您的问题。这个想法是它应该简短,但包含回答它所需的所有信息。出于同样的原因,不鼓励仅提供链接的答案:问题和答案都应该对未来的访问者有所帮助。因此,您的问题可能不应该是关于“我如何通过练习 X”,而是更具体。 “我如何找出我的程序浪费时间的地方”,或者“如何改进这个算法”。理想情况下更具体,告诉我们您已经尝试过的具体内容以及您需要帮助的确切。 (我们不关心那个练习,这是你的学习过程。)
  • 感谢@DevSolar 我一定会牢记并尽快编辑问题。 :)

标签: c performance profiling time-limiting


【解决方案1】:

使用这个:

C/C++

ios_base::sync_with_stdio(false); 
cin.tie(NULL) ;

Python

import psyco
psyco.full()

Java 不要使用 Scanner 类,而是使用 BufferedReader

【讨论】:

    【解决方案2】:

    您可以通过使用适合您的程序的正确算法来克服 TLE,这可以通过查看程序约束轻松确定。

    您可以参考这个tutorial,它清楚地解释了以下几点-

    1. 什么是 TLE?

    2. 为什么会发生 TLE?

    3. 如何克服 TLE?

    【讨论】:

      【解决方案3】:

      更多地关注按位运算符,例如位移和两个整数的按位交换。我的解决方案看起来像

      #define swap(a, b) (a ^= b, b ^= a, a ^= b)
      
      int main() {
          long a, b;
          int count, T;
          scanf("%d", &T);
          while (T--) {
              scanf("%ld%ld", &a, &b);
              for (count = 0; a & (a - 1); a >>= 1)
                  ++count;
              if (a > b)
                  swap(a, b);
              for (; a < b; a <<= 1)
                  ++count;
              printf("%d\n", count);
          }
          return 0;
      }
      

      【讨论】:

      • 请您解释一下整数的位移和按位交换,因为您看到我是一个完整的初学者。请帮忙
      • @PurviSampat:乘法、除法和取模是昂贵的运算。向左移位与*= 2 相同。右移与/= 2 相同。 % 2 == 0 与检查是否设置了最低位 (&amp; 1) 相同。 (如果不设置,则为偶数。)
      猜你喜欢
      • 1970-01-01
      • 2021-08-08
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      相关资源
      最近更新 更多