A-Sequence(cnt[ i ]存储任意多个数的和)

For this problem an A-sequence is a sequence of positive integers ai satisfying 1 ≤ a1 < a2 < a3 < . . .and every ak of the sequence is not the sum of two or more distinct earlier terms of the sequence. You should write a program to determine if a given sequence it is or it is not an A-sequence.
Input
The input consists of a set of lines, each line starts with an integer 2 ≤ D ≤ 30 that indicates the number of integers that the current sequence has. Following this number there is the sequence itself. The sequence is composed by integers, each integer is greater than or equal to 1 and less than or equal to 1000. The input is terminated by enf of file (EOF).
Output
For each test case in the input you should print two lines: the first line should indicate the number of the test case and the test case itself; in the the second line you should print ‘This is an A-sequence.’, if the corresponding test case is an A-sequence or ‘This is not an A-sequence.’, if the corresponding test case is not an A-sequence.

Sample Input
2 1 2
3 1 2 3
10 1 3 16 19 25 70 100 243 245 306

Sample Output
Case #1: 1 2
This is an A-sequence.
Case #2: 1 2 3
This is not an A-sequence.
Case #3: 1 3 16 19 25 70 100 243 245 306
This is not an A-sequence.

题目大意:序列中任意多个的和是否存在与序列中(自己本身除外)

#include <iostream>
#include <cstring>
using namespace std;
const int N = 30010, M = 35;
int a[M];
int cnt[N];
int n, s = 0;

int main()
{
    while(~scanf("%d", &n))
    {
        int sum = 0;
        for(int i = 0; i < n; i ++ )
        {
            scanf("%d", &a[i]);
            sum += a[i];
        }
    
    
    bool flag = true;
    for(int i = 1; i < n && flag; i ++ )
        if(a[i - 1] > a[i] || a[i - 1] < 1 || a[i] < 1)
            flag = false;
    
    if(flag)
    {
        memset(cnt, 0, sizeof cnt);
        cnt[0] = 1;//首先是存储它们本身
        for(int i = 0; i < n; i ++ )
            {
                if(cnt[a[i]])
                {
                    flag = false;
                    break;
                }
        /*
        1.由于前面判断flag是否为0,
        那么用cnt[i]来存储是否有子序列(不一定连续)的和为i。
        2.cnt[i]如果存在子序列的和为i,那么cnt[i] = 1;否则为0
        3.把cnt[0] = 1默认自己算子序列的和,但是再A序列中是符合要求的
        我们通过cnt[0] = 1, 先求出它们本身,其次可以求出序列中任意多个数的和!
        */
            for(int j = sum; j >= a[i]; j -- )
                if(cnt[j - a[i]])
                    cnt[j] = 1;
            }
    }
        printf("Case #%d:", ++ s);
        for(int i = 0; i < n; i ++ )
            printf(" %d ", a[i]);
    
        printf("\nThis is %san A-sequence.\n", flag ? "" : "not ");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值