UVA11063 B2-Sequence【序列】

A B2-Sequence is a sequence of positive integers 1 ≤ b1 < b2 < b3 . . . such that all pairwise sums bi +bj , where i ≤ j, are different. Your task is to determine if a given sequence is a B2-Sequence or not.

Input

Each test case starts with 2 ≤ N ≤ 100, the number of elements in a sequence. Next line will have N integers, representing the value of each element in the sequence. Each element bi is an integer such that bi ≤ 10000. There is a blank line after each test case. The input is terminated by end of file (EOF).

Output

For each test case you must print the number of the test case, starting from 1, and a message indicating if the corresponding sequence it is a B2-Sequence or not. See the sample output below. After each test case you must print a blank line.

Sample Input

4

1 2 4 8

4

3 7 10 14

Sample Output

Case #1: It is a B2-Sequence.

Case #2: It is not a B2-Sequence.

问题链接UVA11063 B2-Sequence

问题简述:(略)

问题分析

  这个问题,看似问的是给定的序列任何两个数之和是否重复,然而是有陷阱的。实际是判定一个序列是否为B2-Sequence,要根据B2-Sequence的定义来做才行。

  根据题意序列的元素都大于等于1并且是升序的,才是B2-Sequence。所以,要先做这个判定。

  然后,再看任何2个数之和是否重复。做这个判定有2种方法,一是将所有的和算出来并且排序,再判定;二是用一个集合来判定。似乎,用集合更加合理。

程序说明:(略)

题记:(略)

参考链接:(略)

AC的C++语言程序如下:

/* UVA11063 B2-Sequence */

#include <bits/stdc++.h>

using namespace std;

const int N = 100;
int b[N];

int main()
{
    int n, caseno = 0;
    while(~scanf("%d", &n)) {
        for(int i = 0; i < n; i++)
            scanf("%d", &b[i]);

        bool flag = true;
        if(b[0] < 1)
            flag = false;
        for(int i = 1; flag && i < n; i++)
            if(b[i] <= b[i - 1])
                flag = false;

        if(flag) {
            set<int> s;
            for(int i = 0; flag && i < n; i++)
                for(int j = i; j < n; j++) {
                    int t = b[i] + b[j];
                    if(s.find(t) != s.end()) {
                        flag = false;
                        break;
                    }
                    s.insert(t);
                }
        }

        printf("Case #%d: It is %sa B2-Sequence.\n\n", ++caseno, flag ? "" : "not ");
    }

    return 0;
}

AC的C++语言程序如下:

/* UVA11063 B2-Sequence */

#include <bits/stdc++.h>

using namespace std;

const int N = 100;
int b[N], sum[N * N + 1];

int main()
{
    int n, caseno = 0;
    while(~scanf("%d", &n)) {
        for(int i = 0; i < n; i++)
            scanf("%d", &b[i]);

        bool flag = true;
        if(b[0] < 1)
            flag = false;
        for(int i = 1; flag && i < n; i++)
            if(b[i] <= b[i - 1])
                flag = false;

        if(flag) {
            int cnt = 0;
            for(int i = 0; i < n; i++)
                for(int j = i; j < n; j++)
                    sum[cnt++] = b[i] + b[j];

            sort(sum, sum + cnt);
            for(int i = 1; i < cnt; i++)
                if(sum[i] == sum[i - 1]) {
                    flag = false;
                    break;
                }
        }

        printf("Case #%d: It is %sa B2-Sequence.\n\n", ++caseno, flag ? "" : "not ");
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/81390134