简单的一道回溯题,具体解法看代码,有注释
#include<iostream>
#include<bits/stdc++.h>
#include<cstdio>
#include<string>
using namespace std;
int ret = 0;
void dfs(vector<int>& nums, int xorsum, int idx) {
// 递归结束条件
if (idx == nums.size()){
ret += xorsum;
return;
}
// 子集包含该元素
dfs(nums, xorsum^nums[idx], idx+1);
// 子集不包含该元素
dfs(nums, xorsum, idx+1);
}
int main()
{
vector<int> nums;
int k;
while (1){
int d;
int ret = scanf("%d", &d);
if (ret == EOF){
break;
}
nums.push_back(d);
}
dfs(nums, 0, 0);
cout << ret << endl;
return 0;
}
标签:idx,nums,int,dfs,P25,异或,ret,Problem,include
From: https://www.cnblogs.com/understanding-friends/p/16779022.html