上面那个回帖点错了,现敲的代码,如下:
- import java.util.regex.Pattern;
- import java.io.*;
- public class Demo {
- public static void main (String args[]) throws IOException {
- Pattern regex = Pattern.compile("[0-9]*/[0-9]*");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String num = "";
- while (!(num = br.readLine()).equals("-1")) {
- if (!regex.matcher(num).matches()) {
- System.out.println("格式不正确,请重新输入!");
- continue;
- }
- int a = Integer.parseInt(num.substring(0, num.indexOf('/')));
- int b = Integer.parseInt(num.substring(num.indexOf('/')+1));
- if (a >= b || !isRelativelyPrime(a, b))
- System.out.println(num + "不是最简真分数");
- else
- System.out.println(num + "是最简真分数");
- }
- }
- private static boolean isRelativelyPrime (int a, int b) {
- int tmp = 0;
- while (a > 0) {
- tmp = b % a;
- b = a;
- a = tmp;
- }
- if (b == 1)
- return true;
- else
- return false;
- }
- }
复制代码 |