RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 661194
Accepted
TorSen
TorSen
Asked:2020-05-03 02:51:38 +0000 UTC2020-05-03 02:51:38 +0000 UTC 2020-05-03 02:51:38 +0000 UTC

长代码执行还是无限循环?

  • 772

有 2 个列表存储数字的数字。那么,您需要找到这些数字的 GCD。如果我们采用数字 9239923923999 - 99992399 - 一切似乎都很好,它起作用了。如果 99999999999999 和 9,那么我只是等待 .. 如果我在某个地方有一个无限循环,或者代码本身是弯曲的,因此需要很长时间来计算,我怎么能理解?如果是第二个选项,如何加速??GCD算法取值如下:

{
   while (a != b) {
        if (a > b) {
            long tmp = a;
            a = b;
            b = tmp;
        }
        b = b - a;
    }
    return a; 
    }

因为减去存储在列表中的两个数字比模除法更容易。减法本身:

{

    Item * tempfirst = A->Head;
    Item * tempsecond = B->Head;
    while (tempfirst && tempsecond)
    {
        if (tempfirst->digit < tempsecond->digit)
        {
            Item * CurrentItem = tempfirst;
            CurrentItem->digit += 10;
            CurrentItem = CurrentItem->next;
            while (CurrentItem->digit <= 0) 
            {
                CurrentItem->digit += 9;
                CurrentItem = CurrentItem->next;
            }
            CurrentItem->digit -= 1;
        }
        tempfirst->digit -= tempsecond->digit;
        tempfirst = tempfirst->next;
        tempsecond = tempsecond->next;
    }
        tempfirst = A->Tail;
        while (tempfirst) 
        {
            if (tempfirst->digit != 0)
                break;
            A->Tail = tempfirst->prev;
            A->Tail->next = NULL;
            free(tempfirst); 
            tempfirst = A->Tail; 
            A->size--;
        }
        return A;
    }

}

对于其余部分,我似乎可以确定..但是我觉得在减法中某处有错误或其他原因。

c
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Harry
    2020-05-03T02:54:51Z2020-05-03T02:54:51Z

    好吧,你给......所以你可以在胡萝卜阴谋之前计算GCD ......

    很明显,你需要计算余数!毕竟,想一想 - 即使您只是通过普通算术从 99999999999999 中减去 9,即使需要 1 纳秒,也需要 10 ^ 13 纳秒,或 10,000 秒 - 3 小时。而您,使用复杂的程序?!

    • 4
  2. jfs
    2020-05-03T05:55:10Z2020-05-03T05:55:10Z

    欧几里德的求最大公约数 (gcd) 的算法,写成有余数的除法:

    U gcd(U a, U b)
    {
      while (b) {
        U t = b;
        b = a % b;
        a = t;
      }
      return a;
    }
    

    哪里U:typedef unsigned long U;

    如果没有有效的实现a % b,那么可以尝试一种使用x >> 1(右移)和x & 1(偶数/奇数)按位运算的算法:

    U gcd(U a, U b)
    {
      if (a == b)
        return a;
      if (a == 0)
        return b;
      if (b == 0)
        return a;
      // a != b and > 0
      if (a & 1) { // a is odd
        if (b & 1) {// b is odd
          // both odd, diff is even, reduce larger arg
          return (a > b) ? gcd((a - b) >> 1, b) : gcd((b - a) >> 1, a);
        } else { // a is odd, b is even
          return gcd(a, b >> 1);
        }
      } else if (b & 1) { // a is even, b is odd
        return gcd(a >> 1, b);
      }
      // a is even, b is even
      return gcd(a >> 1, b >> 1) << 1; // non tail-recursive
    }
    

    使用示例:

    #include <stdio.h>
    
    int main(void)
    {
      printf("%lu\n", gcd(99999999999999, 9));
      return 0;
    }
    

    运行示例:

    $ cc *.c && time ./a.out
    9
    ./a.out  0.00s user 0.00s system 0% cpu 0.001 total
    

    可以看到代码是瞬间执行的。

    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5