一 暴力枚举法

  原理:试图寻找一个合适的整数i,看看这个整数能否被两个整形参数numberA和numberB同时整除。这个整数i从2开始循环累加,一直累加到numberA和numberB中较小参数的一半为止。循环结束后,上一次寻找到的能够被两整数整除的最大i值,就是两数的最大公约数。

 int getGreatestCommonDivisor(int numberA, int numberB)
 {
      ||numberB < )
     {
         ;
     }

      || numberB <= )
     {
         ;
     }

     int max = numberA > numberB ? numberA : numberB;
     int min = numberA > numberB ? numberB : numberA;

     )
     {
         return min;
     }

     ;
     ;

     ; i <= min/; i++)
     {
         ) && (numberB % i == ))
         {
             ret = i;
         }
     }

     return ret;
 }

二 辗转相除法(欧几里得算法)

  原理:两个正整数a和b(a > b),它们的最大公约数等于a除于b的余数c和b之间的最大公约数。比如10和25,25除以10余数5,那么10和25的最大公约数,等于10和5的最大公约数。然后以此类推,直到两个数可以整除,或者其中一个数减小到1为止。

  缺陷:当两个整形数较大时,a % b取模运算的性能比较低。

 int gcd(int a, int b)
 {
     )
     {
         return b;
     }
     else
     {
         return gcd(b, a%b);
     }
 }

 int getGreatestCommonDivisor(int numberA, int numberB)
 {
      ||numberB < )
     {
         ;
     }

      || numberB <= )
     {
         ;
     }

     int max = numberA > numberB ? numberA : numberB;
     int min = numberA > numberB ? numberB : numberA;

     )
     {
         return min;
     }
     else
     {
         return gcd(max, min);
     }
 }

三 更相减损术

  原理:两个正整数a和b(a > b),它们的最大公约数等于a - b的差值c和较小数b的最大公约数。

  缺陷:更相减损术的运算次数肯定远大于辗转相除法。特别是当两数相差比较大,相减的次数很大。

 int gcd(int a, int b)
 {
     if (a == b)
     {
         return a;
     }

     if (a > b)
     {
         return gcd(a-b, b);
     }
     else
     {
         return gcd(b-a, a);
     }
 }

 int getGreatestCommonDivisor(int numberA, int numberB)
 {
      ||numberB < )
     {
         ;
     }

      || numberB <= )
     {
         ;
     }

     int max = numberA > numberB ? numberA : numberB;
     int min = numberA > numberB ? numberB : numberA;

     )
     {
         return min;
     }
     else
     {
         return gcd(max, min);
     }
 }

四 辗转相除法和更相减损术相结合

  原理:当a和b均为偶数,gcb(a, b) = 2 * gcb(a/2, b/2) = 2 * gcb(a>>1, b>>1) = gcb(a>>1, b>>1) << 1

        当a为偶数,b为奇数,gcb(a, b) = gcb(a/2, b) = gcb(a>>1, b)

     当a为奇数,b为偶数,gcb(a, b) = gcb(a, b/2) = gcb(a, b>>1)

     当a和b均为奇数,先用更相减损术运算一次,gcb(a, b) = gcb(b, a-b),此时a-b是偶数,用上面的公式

  说明:移位运算的性能非常快,a/2 转换成a>>1,a * 2 = a << 1

     和1做&操作,判断奇偶:结果为真奇数,结果为假偶数

 int gcd(int a, int b)
 {
     if (a == b)
     {
         return a;
     }

     )) && (!(b&)))   // a和b均为偶数
     {
         , b>>) << ;
     }
     )) && (b&)) // a偶数,b奇数
     {
         , b);
     }
     ) && (!(b&))) // a奇数,b偶数
     {
         );
     }
     else                        // a和b均为奇数
     {
         if (a > b)
         {
             return gcd(a-b, b);
         }
         else
         {
             return gcd(b-a, a);
         }

         return gcd(a-b, b);
     }
 }

 int getGreatestCommonDivisor(int numberA, int numberB)
 {
      ||numberB < )
     {
         ;
     }

      || numberB <= )
     {
         ;
     }

     int max = numberA > numberB ? numberA : numberB;
     int min = numberA > numberB ? numberB : numberA;

     )
     {
         return min;
     }
     else
     {
         return gcd(max, min);
     }
 }

最大公约数Greatest Common Divisor(GCD)的更多相关文章

  1. Greatest common divisor(gcd)

    欧几里得算法求最大公约数 If A = 0 then GCD(A,B)=B, since the GCD(0,B)=B, and we can stop. If B = 0 then GCD(A,B) ...

  2. LeetCode.1071-字符串最大公约数(Greatest Common Divisor of Strings)

    这是小川的第391次更新,第421篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第253题(顺位题号是1071).对于字符串S和T,当且仅当S = T + ... + T ...

  3. upc组队赛17 Greatest Common Divisor【gcd+最小质因数】

    Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...

  4. 845. Greatest Common Divisor

    描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...

  5. [UCSD白板题] Greatest Common Divisor

    Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...

  6. 2018CCPC桂林站G Greatest Common Divisor

    题目描述 There is an array of length n, containing only positive numbers.Now you can add all numbers by ...

  7. CCPC2018 桂林 G "Greatest Common Divisor"(数学)

    UPC备战省赛组队训练赛第十七场 with zyd,mxl G: Greatest Common Divisor 题目描述 There is an array of length n, contain ...

  8. 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)

    定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...

  9. hdu 5207 Greatest Greatest Common Divisor 数学

    Greatest Greatest Common Divisor Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...

随机推荐

  1. Visual Studio 调试技巧

    .net程序开发工具我都用vs(visual studio),开发过程中的跟踪调试最常用的就是断点跟踪调试了,但是现在才发现,用了这么多年vs断点跟踪调试是白用了啊.它居然还可以有这么多用法. 设置断 ...

  2. JavaScript遍历table的行和列

    来源:http://blog.csdn.net/bobwu/article/details/7497412 <HTML> <head> <SCRIPT LANGUAGE= ...

  3. Python文件处理之文件写入方式与写缓存(三)

    Python的open的写入方式有: write(str):将str写入文件 writelines(sequence of strings):写多行到文件,参数为可迭代对象 首先来看下writelin ...

  4. Centos Mysql 升级

    如何升级CentOS 6.5下的MySQL CentOS 6.5自带安装了MySQL 5.1,但5.1有诸多限制,而实际开发中,我们也已经使用MySQL 5.6,这导致部分脚本在MySQL 5.1中执 ...

  5. Spring的依赖注入(DI)三种方式

    Spring依赖注入(DI)的三种方式,分别为: 1.  接口注入 2.  Setter方法注入 3.  构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...

  6. spring-boot支持双数据源mysql+mongo

    这里,首先想说的是,现在的web应用,处理的数据对象,有结构化的,也有非结构化的.同时存在.但是在spring-boot操作数据库的时候,若是在properties文件中配置数据源的信息,通过默认配置 ...

  7. linux下的chmod,chown和chgrp

    对于linux的权限掌握以下几个命令就可以非常熟练的操作系统中的各种权限了. 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode f ...

  8. Windows 7无线网卡启用wifi共享蓝屏!

    我的笔记本是联想Y460P,装的是Windows 7 Ultiame(x64)系统,通过设置笔记本的无线(Intel WiFi Link 1000 BGN)搭建Wifi环境并共享,使手机能够通过笔记本 ...

  9. Tomcat的startup.bat一闪而过问题的解决

    问题描述:点击Tomcat的startup.bat,一闪而过. 问题分析: 1.Tomcat的startup.bat--->catalina.bat--->setclasspath.bat ...

  10. Makefile所有内嵌函数

    一.文本处理函数以下是GNU make内嵌的文本(字符串)处理函数.1       $(subst FROM,TO,TEXT) 函数名称:字符串替换函数—subst. 函数功能:把字串“TEXT”中的 ...