求最小公约数c语言

Python016

求最小公约数c语言,第1张

没有“最小公约数”,只有“最小公倍数”。

最大公约数

指两个或多个整数共有约数中最大的一个。

最小公倍数:

指两个或多个整数公有的倍数中最小的一个,另外,公约数,亦称“公因数”。它是一个能被若干个整数同时均整除的整数。”

C语言4种常见算法:

//C语言实现 四种方法求最大公约数

// 2019 03

// WANTING WANG

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

#include<math.h>

//辗转相除法

int gcd(int a,int b)

{

if(a%b==0)

return b

else

return gcd(b,a%b)

}

//穷举法

int divisor (int a, int b) //自定义函数求两数的最大公约数

{

int  temp//定义整型变量

temp=(a>b)?b:a//采种条件运算表达式求出两个数中的最小值

while(temp>0)

{

if(a%temp==0&&b%temp==0)//只要找到一个数能同时被a,b所整除,则中止循环

break

temp--//如不满足if条件则变量自减,直到能被a,b所整除

}

return (temp)//返回满足条件的数到主调函数处

}

//更相减损法

int gcd2(int m,int n)

{

int i=0,temp,x

while(m%2==0&&n%2==0)//判断m和n能被多少个2整除

{

m/=2

n/=2

i+=1

}

if(m<n)//m保存大的值

{

temp=m

m=n

n=temp

}

while(x)

{

x=m-n

m=(n>x)?n:x

n=(n<x)?n:x

if(n==(m-n))

break

}

if(i==0)

return n

else

return (int) pow(2,i)*n

}

//Stein算法

int Stein( unsigned int x, unsigned int y )

/* return the greatest common divisor of x and y */

{

int factor = 0

int temp

if ( x <y )

{

temp = x

x = y

y = temp

}

if ( 0 == y )

{

return 0

}

while ( x != y )

{

if ( x &0x1 )

{/* when x is odd */

if ( y &0x1 )

{/* when x and y are both odd */

y = ( x - y ) >>1

x -= y

}

else

{/* when x is odd and y is even */

y >>= 1

}

}

else

{/* when x is even */

if ( y &0x1 )

{/* when x is even and y is odd */

x >>= 1

if ( x <y )

{

temp = x

x = y

y = temp

}

}

else

{/* when x and y are both even */

x >>= 1

y >>= 1

++factor

}

}

}

return ( x <<factor )

}

int main()

{

int i

int a[30]

for(i=0i<30i++)

{

a[i]=rand()%100 + 1

printf("%d ",a[i])

}

printf("\n")

int b[30]

for(i=0i<30i++)

{

b[i]=rand()%100 + 1

printf("%d ",b[i])

}

printf("\n")

clock_t start,finish

double dur

start= clock()

for(i=0i<30i++)

{

//printf("辗转相除法所得最大公约数为:%d\n",gcd(a[i],b[i]))

//printf("穷举法所得最大公约数为:%d\n",divisor(a[i],b[i]))

printf("更相减损法所得最大公约数为:%d\n",gcd2(a[i],b[i]))

//printf("Stein算法所得最大公约数为:%d\n",Stein(a[i],b[i]))

}

finish=clock()

dur=(double)(finish-start)/CLOCKS_PER_SEC

printf("运行所用的时间为:%lf s\n",dur)

return 0

}

#include <stdio.h>

int main()

{

int a,b,c,m,t

printf("请输入两个数:\n")

scanf("%d%d",&a,&b)

if(a<b)

{

t=a

a=b

b=t

}

m=a*b

c=a%b

while(c!=0)

{

a=b

b=c

c=a%b

}

printf("最大公约数是:\n%d\n",b)

printf("最小公倍数是:\n%d\n",m/b)

}

扩展资料

算法思想

利用格式输入语句将输入的两个数分别赋给 a 和 b,然后判断 a 和 b 的关系,如果 a 小于 b,则利用中间变量 t 将其互换。

再利用辗转相除法求出最大公约数,进而求出最小公倍数。最后用格式输出语句将其输出。

#include<stdio.h>是在程序编译之前要处理的内容,称为编译预处理命令。编译预处理命令还有很多,它们都以“#”开头,并且不用分号结尾,所以是c语言的程序语句。