求最大公约数

来源:互联网 发布:做完系统电脑优化 编辑:程序博客网 时间:2024/05/18 02:39
package TEST;
import java.util.Scanner;
public class ttt {
 public int gcd(int a,int b){
  int c;
  if(a==b){
   return a;
  }
  if(a<b){
   int temp;
   temp=a;
   a=b;
   b=temp;
  }
  while((c=a%b)!=0){
   a=b;
   b=c;
  }
  return b;
  }


public static void main(String[] args) {
 // TODO Auto-generated method stub
ttt g=new ttt();
 Scanner sc=new Scanner(System.in);
 System.out.println("请输入第一个正整数");
 int NumA=sc.nextInt();
 System.out.println("请输入第二个正整数");
 int NumB=sc.nextInt();
 System.out.println("最大公约数是:"+g.gcd(NumA, NumB));
}
}
0 0