zoukankan      html  css  js  c++  java
  • 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;
    //	}
    }
    
  • 相关阅读:
    测试PHP-FPM的工作流中的疑惑点
    摘要
    Centrifugo简单试用
    react-redux的基本用法
    PHP中使用ElasticSearch(二)
    PHP中使用ElasticSearch(一)
    Vue中的状态管理器
    Laravel数据库迁移
    快速搭建一个vue开发环境
    使用cURL尝试ElasticSearch
  • 原文地址:https://www.cnblogs.com/luoxiaoyi/p/9758544.html
Copyright © 2011-2022 走看看