• 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;
    //	}
    }
    
  • 相关阅读:
    09.Java数据算法
    08.Java反射问题
    07.Java类加载问题
    占位
    占位
    占位
    占位
    HTML与CSS粗浅摘要
    JavaScript(4):模态对话框、隐藏二级菜单及多选框的实现
    JavaScript(3):DOM选择器
  • 原文地址:https://www.cnblogs.com/luoxiaoyi/p/9758544.html
Copyright © 2020-2023  润新知