精品C语言求最大公约数和最小公倍数算法总结
C
C
C
TP312 A
The algorithm summarization of evaluating the highest common divisor and the lowest common
multiple in C Language
Zhao Ye Lin Tutor TeacherLiu Xiao Hua
Introduction to the algorithm basic thought, algorithm process and code realization and its analysing comparison in terms of evaluating the highest common divisor and the lowest common multiple of any two positive integers by learning to using C Language
KeywordsC Language algorithm the highest common divisor the lowest common multiple
CC
1
Ca,b
a b=0
gcd(a,b) =
gcd(b,a mod b) b!=0
,
:a,ba ,btemp
1ab
2a/b
3temp=0b
4temp!=0batempa
5
int divisor (int a,int b) /*自定义函数求两数的最大公约数*/ {
int temp; /*定义整型变量*/
if(a
b)?b:a; /*采种条件运算
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
达式求出两个数中的最小值*/
while(temp>0)
{
if (a%temp==0&&b%temp==0) /*只要找到一个数能同时被a,b所整除~则中止循环*/
break;
temp--; /*如不满足if条件则变量自减~直到能被a,b所整除*/
}
return (temp); /*返回满足条件的数到主调函数处*/ }
#include "stdio.h"
main()
{
int m,n,t1;
printf("please input two integer number:");
scanf("%d%d",&m,&n);
t1=divisor(m,n);
printf("The higest common divisor is %d\n",t1);
getch();
}
a,b,abba
int multiple (int a,int b)
{
int p,q,temp;
p=(a>b)?a:b; /*求两个数中的最大值*/
*求两个数中的最小值*/ q=(a>b)?b:a; /
temp=p; /*最大值赋给p为变量自增作准备*/
while(1) /*利用循环语句来求满足条件的数值*/
{
if(p%q==0)
break; /*只要找到变量的和数能被a或b所整除~则中止循环*/
p+=temp; /*如果条件不满足则变量自身相加*/
}
return (p);
}
#include "stdio.h"
main()
{
int m,n,t2;
printf("please input two integer number:");
scanf("%d%d",&m,&n);
t2=multiple(m,n);
printf("The least common multiple is %d\n",t2);
getch();
}
C
[1] C 2004 [2] C 2004
[3] C 2002 [4] 2002