Pairing of integer multiples


Submit solution

Points: 20
Time limit: 1.0s
Memory limit: 64M

Author:
Problem type

題目說明

<1123檢定考題>
程式輸入一連串數字,第1個輸入值是n表示陣列長度,第2到n+1個輸入值是陣列內容,輸入值都是大於0且小於1000的整數。n大於等於2。
程式計算陣列中總共有幾組「倍數配對」並輸出該數量。對於陣列nums,對於任意兩個索引 i 和 j,
若 nums[i] 是 nums[j] 的整數倍或 nums[j] 是 nums[i] 的整數倍,則 nums[i] 和 nums[j] 為一個「倍數配對」。

輸入

輸入一連串數字,第1個輸入值是n表示陣列長度,第2到n+1個輸入值是陣列內容,輸入值都是大於0且小於1000的整數。n大於等於2。

輸出

計算陣列中總共有幾組「倍數配對」並輸出該數量。

sample input0

4 3 6 12 7

sample output0

3

說明:6和12都是3的整數倍,12是6的整數倍。

sample input1

4 3 3 3 7

sample output1

3

說明:3是3的整數倍

sample input2

2 3 4

sample output2

0

Comments


  • 0
    scu14156131  commented on Dec. 14, 2025, 1:50 a.m.

    import java.util.*; public class Pairingofintegermultiples {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
    
        int n =sc.nextInt();
        // check n>=2 
        if(n<2) return;
    
        int[] array = new int[n];
        int count = 0;
        int current_arrayvalue = 0;
    
        for(int i =0; i<n; i++) {
            array[i] = sc.nextInt();
    
            if(array[i] => 1000 && array[i] <= 0) return;
        }
    
        for(int j = 0; j<n; j++) {
            current_arrayvalue = array[j];
            for(int k = j+1; k<n; k++) {
                if(array[k] % current_arrayvalue == 0) {
                    count++;
                }
            }
        }
        System.out.print(count);
    
    }

    }