当前位置: 代码迷 >> Java相关 >> 如何求两个数的最大公约数?求大神指导!!![em06]
  详细解决方案

如何求两个数的最大公约数?求大神指导!!![em06]

热度:365   发布时间:2013-03-31 15:15:54.0
如何求两个数的最大公约数?求大神指导!!![em06]
如何求两个数的最大公约数?求大神指导!!!
搜索更多相关的解决方案: 七夕  指导  公约数  如何  

----------------解决方案--------------------------------------------------------
程序代码:
#include <stdio.h>
int main() {
    int a, b, n1, n2, temp;
    scanf("%d%d", &n1, &n2);
    if(n1 < n2) {
        temp = n1;
        n1 = n2;
        n2 = temp;
    }
    a = n1; b = n2;
    while(b != 0) {
        temp = a % b;
        a = b;
        b = temp;
    }
    printf("%d %d\n", a, n1 * n2 / a);
    return 0;
}
2个都有了,自己看看吧....
----------------解决方案--------------------------------------------------------
C语言版?
----------------解决方案--------------------------------------------------------
class MyException extends Exception{
    private static final long serialVersionUID = 1L;
    String message;
    public MyException(String ErrorMessagr){
        message = ErrorMessagr;
    }
    public String getMessage(){
        return message;
    }
}
public class ZDGYS{
    static int zdgy(int x,int y)throws MyException{
        if(x<0||y<0){
            throw new MyException("这两个数都不能小于0。");
        }
        int a=1;
        if(x>y){
            if(x%y==0){
                a=y;
               }else for(int i=2,j=0;i<=y/2;i++){
                if(y%i==0){
                    j=y/i;
                    if(x%j==0){
                        a=j;
                        break;
                       }
                     }
                }
        } else{   
            if(y%x==0){
                a=x;
               }else for(int i=2,j=0;i<=x/2;i++){
                if(x%i==0){
                    j=x/i;
                    if(y%j==0){
                        a=j;
                        break;
                        }
                    }
                  }
           }
        return a;
    }
    public static void main(String[]args){
        try{
            int A = zdgy(390,130);
            System.out.println("最大公约数为:"+A);
        }catch(MyException a){
            System.out.println(a.getMessage());
        }
    }
}
----------------解决方案--------------------------------------------------------
import java.util.Scanner;
public class Test20_3 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int m,n;
        System.out.print("Enter the two number: ");
        m = input.nextInt();
        n = input.nextInt();
        int bigGcd = gcd(m, n);
        System.out.print("The biggest approximate number is " + bigGcd);
        
    }
    private static int gcd(int m, int n){
        if(m < n){
            int temp = m;
            m = n;
            n = temp;
        }
        if(m % n == 0)
            return n;
        else
            return gcd(n, m % n);
            
    }
}

----------------解决方案--------------------------------------------------------
自己刚做的

----------------解决方案--------------------------------------------------------
回复 3楼 ren829
最好是用java编的!
----------------解决方案--------------------------------------------------------
回复 6楼 wmb429006
非常感谢!!!!!!
----------------解决方案--------------------------------------------------------
程序代码:
/*
* 从键盘上输入两个正整数,求他们的最大公约数
*/
import java.util.* ;
public class TJ05 {
    public static void main(String args[]){
        int m,n,a,b,i,c=1;
        System.out.print("请输入两个正整数:") ;
        Scanner scan = new Scanner(System.in) ;
        m=scan.nextInt() ;        //接收从键盘上输入的第一个数
        n=scan.nextInt() ;        //接收从键盘上输入的第二个数
        a=m>n?n:m ;        //取两数中的最小数
        b=m>n?m:n ;        //取两数中的最大数
        for(i=1;i<a/2;i++){
            if(b%a==0){
                System.out.println(m+"和"+b+"最大公约数为: "+a);
                System.exit(1);        //退出程序
            }else if(a%i==0&&b%i==0){
                c=i ;        //如果i能同时整除a和b,将i的值赋给c
                continue ;    //返回下一个循环体
            }
        }
        System.out.println(m+"和"+b+"最大公约数为: "+c);
    }
}

----------------解决方案--------------------------------------------------------
程序代码:
/*
*从键盘上输入两个数,求他们的最大公约数
*/
public class TJ06 {
    public static void main(String args[]){
        System.out.print("请输入两个正整数:") ;
        TJ06_1 tj = new TJ06_1();
        tj.getInfo() ;
    }
}
import java.util.Scanner;
public class TJ06_1 {
    public TJ06_1(){}
    Scanner scan = new Scanner(System.in) ;
    int c;
     int a=scan.nextInt() ;            //接收送键盘上输入的第一个数
     int b=scan.nextInt() ;
    public void getInfo(){
        a=a>b?a:b ;        //取两数的最大值
        b=a>b?b:a ;        //取两数的最小值
        if(a%b==0){
            c=b ;
            System.out.println(a+"和"+b+"的最大公约数为: "+c) ;
        }else{
            b=a%b ;            //将a对b取摸得值赋给b
            getInfo() ;
        }
   
    }
}
这是看了5楼的代码后  编的

----------------解决方案--------------------------------------------------------