中国教程网论坛's Archiver

sooo 发表于 2008-3-17 22:58

求助:求45 和60的最大公约数的程序怎么写啊

#@#$^% #@#$^% 这个程序不会写,好心的蝈蝈们麻烦会的写下。#@#$@! 先谢谢了

hagoromo 发表于 2008-5-14 23:59

我也是刚学,笨方法,不通用

public class gys {
        public static void main(String []args){
                int a = 45,b = 60,x,i;
               
                x = b % a;
                i = a % x;

                if (i == 0){
                        System.out.println("45和60的最大公約数是"+x);
                }
        }
}

kanbokb 发表于 2008-7-22 17:46

你试试

public class numSort {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                int a=45;
                int b=60;
                int c;
                int d;
                for(int i=45;i>1;i--){
                        if(b%i==0&&a%i==0){
                                c=i;
                                System.out.println("最大公约数:"+c);
                                break;
                        }
                        else{                       
                        }
                }
                for(int j=60;j<=(60*45);j++)
                        if(j%60==0&&j%45==0){
                                d=j;
                                System.out.println("最小公倍数:"+d);
                        break;
                        }
                        else{
                               
                        }
                  
                               
                        }

        }

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.