C/C++训练1---最大公约数与最小公倍数
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Problem Description
输入两个正整数,求它们的最大公约数与最小公倍数。
Input
输入两个正整数,两个整数之间用空格分开。
数据保证在 int 范围内。
Output
第一行输出最大公约数;
第二行输出最小公倍数。
答案保证在 int 范围内。
Sample Input
64 48
Sample Output
16 192
import java.util.Arrays; import java.util.Scanner;
import javax.swing.plaf.synth.SynthSpinnerUI;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int m, n;
m = sc.nextInt();
n = sc.nextInt();
Gbs a = new Gbs(m, n);
System.out.println(a.gys(m, n));
System.out.println(a.gbs(m, n));
sc.close();
}
}
class Gbs{
int a, b;
public Gbs(int a, int b)
{
this.a = a;
this.b = b;
}
public int gys(int m, int n)
{
int c = m;
while(c!=0)
{
c = m%n;
m = n;
n = c;
}
return m;
}
public int gbs(int m, int n)
{
return m*n/gys(a, b);
}
}
本文介绍了一个使用C/C++编写的简单程序,该程序能够接收两个正整数作为输入,并计算出这两个数的最大公约数与最小公倍数。通过实例演示了如何利用辗转相除法来求解最大公约数,进而计算最小公倍数。
847

被折叠的 条评论
为什么被折叠?



