-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudy-GCD
More file actions
46 lines (44 loc) · 842 Bytes
/
Copy pathstudy-GCD
File metadata and controls
46 lines (44 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*GCD最大公约数
* for循环法
* 辗转相除法method of successive division
*
*
* */
package Hello;
import java.util.Scanner;
public class GCD {
public static void main(String[] args) {
// TODO Auto-generated method stub
// MSD(null);
forloop(null);
}
public static void MSD(String[] args){
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int oa=a;
int ob=b;
while(b!=0)
{
int r=a%b;
System.out.println(a+","+b+","+r);
a=b;
b=r;
}
System.out.println(oa+"和"+ob+"的最大公约数是"+a);
}
public static void forloop(String[] args){
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int gcd=1;
for(int i=2;i<=a&&i<=b;i++)
{
if(a%i==0 & b%i ==0 )
{
gcd=i;
}
}
System.out.println(gcd);
}
}