#include
#define LEN 100 /*数组最大长度*/
/*求取n个数的最小公倍数(LCM)*/
/*先求前2个数的LCM,再求与第3个数的LCM,如此直至最后一个数*/
int getLCM (int array[], int n) {
int i,LCMTemp,x,y;
LCMTemp = array[0];
for (i=0; ix = LCMTemp;
y = array[i+1];
LCMTemp = LCM (x,y);
}
}
/*求2个数的最小公倍数*/
int LCM (int m, int n) {
int m1,n1;
int gcd,lcm;
m1=m;
n1=n;
if (n1)
while ((m1 %= n1) && (n1 %= m1));
gcd = m1+n1;
lcm = m*n/gcd;
return lcm;
}
int main (void) {
int array[LEN], n, lcm, i;
printf ("请输入整数个数:");
scanf ("%d", &n);
putchar ('\n');
printf ("请输入%d个整数:",n);
for (i=0; iscanf ("%d", &array[i]);
putchar ('\n');
lcm = getLCM (array, n);
printf ("以上%d个整数的最小公倍数为:%d",n,lcm);
putchar ('\n');
getch (); /*屏幕暂留*/
return 0;
}
运行结果
#include
int gcd(int n,int m)
{if(n%m==0)return m;
return gcd(m,n%m);
}
int lcm(int n,int m)
{return n/gcd(n,m)*m;
}
int main()
{int n,i,x,y;
scanf("%d%d",&n,&x);
for(i=1;i {scanf("%d",&y); x=lcm(x,y); } printf("这%d个数的最小公倍数是%d\n",n,x); return 0; }
#include
#include
void c()//清空缓冲区函数
{
char ch;
while((ch=getchar())!='\n'&&ch!=EOF);
}
int main()
{
int n,i,num;
printf("input N:");
scanf("%d",&n);
c();
int *p=(int *)malloc(n*sizeof(int));//创建动态数组
for(i=0;i{
printf("the no %d is:",i+1);
scanf("%d",p+i);
c();
}
for(num=p[0];1;num++)//找出最小公倍数
{
for(i=0;i{
if(num%p[i]!=0)
break;
}
if(i==n)
break;
}
printf("最小公倍数是:%d\n",num);
free(p);
return 0;
}