Java练习 SDUT-1131_最大公约数与最小公倍数

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.*;

public class Main{
	public static void main(String[] args) {
		int a,b,c,d;
		node t;
		Scanner cin = new Scanner(System.in);
		a = cin.nextInt();
		b = cin.nextInt();
		t = new node(a,b);
		c = t.f(a,b);
		d = a * b / c;
		System.out.println(c);
		System.out.println(d);
		cin.close();
	}
}

class node
{
	int a,b;
	node (int x,int y)
	{
		a = x;
		b = y;
	}
	int f(int a,int b)
	{
		if(b==0)
			return a;
		else
			return f(b,a%b);
	}
//      int f(int a,int b)  //两种方法写;
//	{
//		while(b!=0)
//		{
//			int c = b;
//			b = a % b;
//			a = c;
//		}
//		return a;
//	}
}
posted @ 2018-10-09 09:37  洛沐辰  阅读(812)  评论(0编辑  收藏  举报