最小公倍数(不易理解)
题目描述:
求n个数的最小公倍数。
Input
输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。
Output
为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。
Sample Input
2 4 6 3 2 5 7Sample Output
12 70
通过答案:
#include <stdio.h>
int main()
{
int n,i,j,a,b,temp;
int str[100];
while ( scanf("%d", &n)!=EOF)
{
for(j=0;j<n;j++){
scanf("%d",&str[j]);
}
//用辗转相除法
for(i=0;i<n-1;i++){
a=str[i];
b=str[i+1];
while(a%b!=0){ //循环直至余数不为0
temp=a%b; //如果a<b,第一次相当n1与n2值交换
a=b; //使除数b变为被除数a
b=temp; //使余数变为除数b
}
str[i + 1] = str[i] / b* str[i + 1];
}
printf("%d\n",str[i]);
}
return 0;
}