Codeforces Round #626 A. Even Subset Sum Problem(奇偶)

传送门

题意:

给一个长度为n的数组,问是否能找到一个该数组的子集的和为偶数
输出子集大小,以及在原数组中的位置,不存在输出-1

思路:

不存在的情况就一种,n==1且a[1]是奇数
如果数组中有偶数,直接输出那偶数即可
否则输出
2
1 2即可,因为数组全是奇数,前两个相加肯定是偶数

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
typedef long long ll;
using namespace std;
const int MAXN=1e6+50;
const int inf=0x3f3f3f3f;
const int M=5000*4;
int a[150];
int main()
{
	int t,n;
	cin>>t;
	while(t--){
		cin>>n;
		int f=0,pos=0;
		rep(i,1,n){
			cin>>a[i];
			if(a[i]%2==0){
				pos=i;
			}
		}
		if(n==1&&a[1]%2==1){
			cout<<-1<<endl;
			continue;
		}
		if(pos!=0){
			cout<<1<<endl;
			cout<<pos<<endl;
			continue;
		}
		cout<<2<<endl;
		cout<<1<<" "<<2<<endl;
	}
    return 0;
}
/*

*/
发布了158 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44091178/article/details/104723280