凑硬币

来源:互联网 发布:bbs url 网络推广 编辑:程序博客网 时间:2024/03/28 16:16

小Y有一朋友有很多硬币,由于实在太多,我们就假设有无限个。某天,他答应给小Y其中两种面值的硬币,数量不限,
现在小Y想知道这两种硬币能凑出哪些面值,不能凑出哪些面值,估计准备去逛商场了吧。

import java.util.Scanner;public class Main {    private static long gcd(long a, long b) {        if (a % b == 0) {            return b;        } else {            return gcd(b, a % b);        }    }    public static void main(String[] args) {        Scanner cin = new Scanner(System.in);        while (cin.hasNext()) {            int t = cin.nextInt();            for (int i = 0; i < t; i++) {                long a = cin.nextLong();                long b = cin.nextLong();                long gcd = gcd(a, b);                if (gcd != 1) {                    System.out.print("infinite!" + "\r\n");                } else {                    System.out.print(                            (a - 1) * (b - 1) / 2 + "\r\n");                }            }        }    }}
0 0
原创粉丝点击