2115 - 求三个数的最小公倍数
时间限制 : 1 秒
内存限制 : 128 MB
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; }
Java解答
import java.util.Scanner; /** * * @author zhenghan33 */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner cin=new Scanner(System.in); int[]a=new int[3]; a[0]=cin.nextInt(); a[1]=cin.nextInt(); a[2]=cin.nextInt(); java.util.Arrays.sort(a); int n=a[2]; while(n%a[0]!=0||n%a[1]!=0||n%a[2]!=0) n++; System.out.println(n); } }