题解 | 最简真分数

最简真分数

https://www.nowcoder.com/practice/1f1db273eeb745c6ac83e91ff14d2ec9

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    // 求最大公约数

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int N = in.nextInt();
            if(N == 0){
                return;
            }
            int [] array = new int[N];
            for(int index = 0;index<array.length;index++){
                array[index] = in.nextInt();
            }
            int count = 0;
            for(int i = 0 ;i<array.length;i++){
                for(int j = i+1;j<array.length;j++){
                    if(gcd(array[i],array[j])==1){
                        count++;
                    }
                }
            }
            System.out.println(count);
        }
    }

    public static int gcd(int x,int y){
        if(y == 0) return x;
        return gcd(y, x%y);  
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务