Even Subset Sum Problem

Even Subset Sum Problem

题面翻译

题意简述

给定数组 a a a,求出它一个和为偶数的的非空子集。

输入格式

本题有多组数据。

第一行一个正整数 t t t 表示数据组数。

对于每组数据,第一行一个正整数 n n n

接下来一行 n n n 个正整数 a 1 , a 2 , . . . , a n a_1,a_2,...,a_n a1,a2,...,an

输出格式

对于每组数据,如果有解,在第一行输出 k k k,表示你找到的非空子集的大小。在第二行输出 k k k 个正整数,表示你找到的非空子集在 a a a 数组中的下标。

如果无解,在第一行输出 − 1 -1 1

数据范围

1 ≤ t , n , a i ≤ 100 1 \leq t,n,a_i \leq 100 1t,n,ai100

翻译 by Meatherm

题目描述

You are given an array $ a $ consisting of $ n $ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $ 2 $ ) or determine that there is no such subset.

Both the given array and required subset may contain equal values.

输入格式

The first line contains a single integer $ t $ ( $ 1 \leq t \leq 100 $ ), number of test cases to solve. Descriptions of $ t $ test cases follow.

A description of each test case consists of two lines. The first line contains a single integer $ n $ ( $ 1 \leq n \leq 100 $ ), length of array $ a $ .

The second line contains $ n $ integers $ a_1, a_2, \ldots, a_n $ ( $ 1 \leq a_i \leq 100 $ ), elements of $ a $ . The given array $ a $ can contain equal values (duplicates).

输出格式

For each test case output $ -1 $ if there is no such subset of elements. Otherwise output positive integer $ k $ , number of elements in the required subset. Then output $ k $ distinct integers ( $ 1 \leq p_i \leq n $ ), indexes of the chosen elements. If there are multiple solutions output any of them.

样例 #1

样例输入 #1

3
3
1 4 3
1
15
2
3 5

样例输出 #1

1
2
-1
2
1 2

提示

There are three test cases in the example.

In the first test case, you can choose the subset consisting of only the second element. Its sum is $ 4 $ and it is even.

In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.

In the third test case, the subset consisting of all array’s elements has even sum.

#include<stdio.h>
int main(void)
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int even = 0, odd[10] = { 0 }, odd_num = 0;
		int n, a;
		scanf("%d", &n);
		int nstart = n;
		while (n--)
		{
			scanf("%d", &a);
			if (a % 2 == 0)
			{
				if (even == 0)
					even = nstart - n;
			}
			else
			{
				if (odd_num < 2)
				odd[++odd_num] = nstart - n;
			}
		}
		if (even)
			printf("1\n%d\n", even);
		else if (odd_num >= 2)
		{
			printf("2\n%d %d\n", odd[1], odd[2]);
		}
		else
		{
			printf("-1\n");
		}
	}
	return 0;
}

scanf没有扫描完成不轻易写break
所有题目都是新题,严格按照该题的要求,比如这个题要求输出序号,而不是偶数或者奇数的值

  • 19
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值