2115 - 求三个数的最小公倍数

1.     编写程序,求任意三个数的最小公倍数。

要求用子函数和Driver的形式编写代码。你需要设计求3个数最小公倍数的子函数。例如:

input:

20 50 30
output:
300

题目输入

三个整数

题目输出

三个整数的最小公倍数

输入/输出样例

题目输入

2 5 3

题目输出

30

C语言解答

#include <stdio.h>

int f(int a,int b)
{
    if ( !(a%b) )   return b;
    return f(b,a%b);
}

int main(void)
{
    int a,b,c;
    int res;

    scanf("%d %d %d",&a,&b,&c);
    res = a * b / f(a,b);
    res = res * c / f(res,c);
    printf("%d\n",res);
    return 0;
}

C++解答

#include <iostream>
using namespace std;
int main()
{
	long a;
	long b;
	long c;
	long max;
	int i=2;
	cin >>a >>b>>c;
	if(a>b&&a>c)
		max=a;
	if(b>a&&b>c)
		max=b;
	if(c>a&&c>b)
		max=c;
	while(max%a!=0||max%b!=0||max%c!=0)
	{
		max=max++;
		
	}
	cout <<max<<endl;
	return 0;
}
时间限制 1 秒
内存限制 128 MB
讨论 统计
上一题 下一题