欧几里得算法求最大公约数
import java.util.Scanner;
public class GCD{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
try{
System.out.println("Please input first number:");
int firstNum = Integer.valueOf(sc.next());
System.out.println("Please input second number:");
int secondNum = Integer.valueOf(sc.next());
System.out.println("The gcd of " + firstNum + " and " + secondNum + " is: " + gcd(firstNum,secondNum));
}catch(Exception e){
System.out.println("The Input Just now is not a int type,Pls input again:");
continue;
}
}
}
public static int gcd(int p, int q){
if(0 == q){
return p;
}
int r = p % q;
return gcd( q, r);
}
public static <T> String getType(T t){
if(t instanceof Integer){
return "int";
}else if(t instanceof String){
return "string";
}else{
return " do not know";
}
}
}
http://www.cnblogs.com/makexu/

浙公网安备 33010602011771号