代码求助。。
查看原帖
代码求助。。
358971
朦胧_XY楼主2021/11/14 17:55

谁能帮我看看我搜索哪里写死了啊。。 QAQ

#include<iostream>
using namespace std;
const int maxn = (1 << 20);
int n, k, len = 1, a[maxn], t[maxn], flag;
int popcount(int x){
	int t = 1, w = 0;
	while(t <= x){
		t *= 2;
	}
	if(t > x) t /= 2;
	while(x > 0){
		if(x >= t){
			w++;
			x -= t;
			t /= 2;
		}
		else t /= 2;
	}
	return w;
}
void find(int x, int l){
	if(l == len){
		flag = 1;
		return;
	}
	for(int i = 1; i <= len; i++){
		if(popcount(i ^ x) == k && t[i] == 0){
			a[l + 1] = i;
			t[i] = 1;
			find(i, l + 1);
			if(flag == 1) return;
			else{
				a[l + 1] = 0;
				t[i] = 0;
			}
		}
	}
	return;
}
int main(){
	scanf("%d %d", &n, &k);
	len <<= n;
	a[0] = 0, t[0]++;
	find(0, 0);
	if(flag == 0) printf("0");
	else{
		printf("1\n");
		for(int i = 0; i <= len; i++){
			printf("%d ", a[i]);
		}
	}
	return 0;
}
2021/11/14 17:55
加载中...