close

給你一串數列,兩兩之和不可重複

4   // 數字數量

1 2 4 8  // 數列

1 + 1 = 2

1 + 2 = 3

1 + 4 = 5

1 + 8 = 9

2 + 2 = 4

2 + 4 = 6

2 + 8 = 10

4 + 4 = 8

4 + 8 = 12

8 + 8 = 16

每一個sum都只出現一次

 


4

3 7 10 14
3 + 3 = 6

3 + 7 = 10

3 + 10 = 13

3 + 14 = 17

7 + 7 = 14

7 + 10 = 17

7 + 14 = 21

10 + 10 = 20

10 + 14 = 24

14 + 14 = 28

17 重複出現2次,not a b2-sequence

 

--------

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package uva11063;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author awesq
 */
public class UVa11063 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            Scanner sc = new Scanner(System.in);
            for (int index = 1; sc.hasNextInt(); index++) {
                int numbers = sc.nextInt();
                ArrayList alNum = new ArrayList();
                ArrayList alSum = new ArrayList();

                // Input
                while (numbers-- > 0) {
                    alNum.add(sc.nextInt());
                }

                // Operation
                int i, j;
                for (i = 0; i < alNum.size(); i++) {
                    for (j = i; j < alNum.size(); j++) {
                        int total = ((int) alNum.get(i)) + ((int) alNum.get(j));
                        if (alSum.contains(total)) {
                            break;
                        } else {
                            alSum.add(total);
                        }
                    }
                    if (j != alNum.size()) { // Some numbers appear repeatdly.
                        break;
                    }
                }
                if (i != alNum.size()) {
                    System.out.println("Case #" + index + ": It is not a B2-Sequence.");
                } else {
                    System.out.println("Case #" + index + ": It is a B2-Sequence.");
                }
            }
            sc.close();
        } catch (Exception e) {
            System.out.println("Wrong");
        }
    }

}
 

arrow
arrow
    全站熱搜

    awesq123 發表在 痞客邦 留言(0) 人氣()