最大公约数

xiaoxiao2021-02-28  146

最大公约数

时间限制: 1000 ms  |  内存限制: 65535 KB 难度: 1 描述 编写程序,输入两个正整数x和y,求它们的最大公约数。 输入 有多组测试数据,以EOF结束。 每组测试数据有2个整数x、y。 输出 每组数据输出一行。 样例输入 8,6 样例输出 2

import java.util.Scanner; public class Main { public static void fun(int numone, int numtwo) { int temp = numone % numtwo; while (temp != 0) { numone = numtwo; numtwo = temp; temp = numone % numtwo; } System.out.println(numtwo); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String str = scanner.nextLine(); String arr[] = str.split(","); int numone = Integer.parseInt(arr[0]); int numtwo = Integer.parseInt(arr[1]); if (numone > numtwo) { fun(numone, numtwo); } else { fun(numtwo, numone); } } } }

转载请注明原文地址: https://www.6miu.com/read-27048.html

最新回复(0)