经典JAVA题五

 1、求两个正整数的最大公约数和最小公倍数

 方法一:采用递归算法

 

View Code
 1 package JAVA;
2
3 import java.util.Scanner;
4
5 public class test06_1 {
6 public static void main(String args[]) {
7
8 int a, b;
9 System.out.print("请输入两个整数(用空格隔开):");
10 Scanner in = new Scanner(System.in);
11 a = in.nextInt();
12 b = in.nextInt();
13 /*
14 * 交换两个数字
15 */
16 if(a<b){
17 a=a+b;
18 b=a-b;
19 a=a-b;
20 }
21
22 System.out.println(a+"和"+b+"的最大公约数为:"+fun1(a,b));
23 System.out.println(a+"和"+b+"的最小公倍数为:"+fun2(a,b));
24 }
25
26 // 最大公约数
27 public static int fun1(int a, int b) {
28 if (a == 0) {
29 return b;
30 } else {
31 System.out.println(a+"==="+b);
32 return fun1(a % b, a);
33 }
34 }
35
36 // 最小公倍数
37 public static int fun2(int a, int b) {
38 return a * b / fun1(a, b);
39 }
40 }

 

方法二:采用辗转相除法

package JAVA;

import java.util.Scanner;

public class test06 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a,b;
		System.out.print("请输入两个整数(用空格隔开):");
        Scanner in=new Scanner(System.in);
        a=in.nextInt();
        b=in.nextInt();
        
        /*
         * 交换两个数字
         */
        if(a<b){
        	a=a+b;
        	b=a-b;
        	a=a-b;
        }
        
        int c=fun(a,b);
        System.out.println(a+"和"+b+"的最大公约数为:"+c);
        System.out.println(a+"和"+b+"的最小公倍数为:"+a*b/c);
	}

	/*
	 * 辗转相除法
	 */
	private static int fun(int a, int b) {
		// TODO Auto-generated method stub
		int temp;
		temp=a%b;
		while(temp>0){
			a=b;
			b=temp;
			temp=a%b;
		}
		return b;
	}
   
}

 2、求三个正整数的最大公约数和最小公倍数及求三个数中的最大值

View Code
 1 package JAVA;
2
3 import java.util.Scanner;
4
5 public class test06 {
6
7 /**
8 * @param args
9 */
10 public static void main(String[] args) {
11 // TODO Auto-generated method stub
12 int a,b,c,max;
13 System.out.print("请输入三个整数(用空格隔开):");
14 Scanner in=new Scanner(System.in);
15 a=in.nextInt();
16 b=in.nextInt();
17 c=in.nextInt();
18
19 max=Math.max(Math.max(a,b), c);
20 System.out.println("方法一:三个整数的最大值为:"+max);
21
22 System.out.println("方法二:三个整数的最大值为:"+Max(a,b,c));
23
24 int n=fun(a,b);
25 int m=fun1(n,c);
26 System.out.println(a+"、"+b+"、"+c+"的最大公约数为:"+m);
27 System.out.println(a+"、"+b+"、"+c+"的最小公倍数为:"+fun2(a,b,c));
28 }
29
30
31 /*
32 * 求三个正整数的最大值
33 */
34 private static int Max(int a,int b,int c){
35 int temp,max;
36 temp=a>b?a:b;
37 max=temp>c?temp:c;
38 return max;
39 }
40 /*
41 * 辗转相除法求最大公约数
42 */
43 private static int fun(int a, int b) {
44 // TODO Auto-generated method stub
45 int temp;
46 temp=a%b;
47 while(temp>0){
48 a=b;
49 b=temp;
50 temp=a%b;
51 }
52 return b;
53 }
54
55 private static int fun1(int n, int c) {
56 // TODO Auto-generated method stub
57 return fun(n,c);
58 }
59
60 /*
61 * 求最小公倍数
62 */
63 private static int fun2(int a,int b,int c){
64 int k,i=1,j = 0;
65 boolean run=true;
66 k=Max(a,b,c);/*最大值是k*/
67 while(run){
68 j=k*i;
69 if(j%a==0&&j%b==0&&j%c==0) break;/*j能被3个数整除*/
70 i++;
71 }
72 return j;
73 }
74 }



posted @ 2011-11-16 13:05  月亮的影子  阅读(266)  评论(0编辑  收藏  举报