救救孩子=。=、为什么我这个会输出重复的
查看原帖
救救孩子=。=、为什么我这个会输出重复的
255791
衛宮士郎楼主2021/4/23 14:46
#include<iostream>
using namespace std;
int A[20],N,R;
bool B[20];

void f(int n){	//n代表存放数字的下标 
	if(n==R){	//如果全部存放完毕,代表结束返回 
	for(int i=1;i<=R;i++) {
		printf("%3d",A[i]);
	}
		cout<<endl;
		return;
}
	for(int i=1;i<=N;i++){		
		if(B[i]==true&&i>A[n-1]||n==1){
		A[n+1]=i;
		B[i]=false;
		f(n+1);
		B[i]=true;//回溯
	}
  }
}
int main(){
	cin>>N>>R;
	for(int i=0;i<R;i++) B[i]=true;
	f(0);
} 
2021/4/23 14:46
加载中...