全排列代码有的地方不懂
  • 板块学术版
  • 楼主hanran_
  • 当前回复6
  • 已保存回复6
  • 发布时间2021/7/20 15:41
  • 上次更新2023/11/4 14:03:40
查看原帖
全排列代码有的地方不懂
260361
hanran_楼主2021/7/20 15:41


#include <bits/stdc++.h>

using namespace std;


long long n;
long long a[100001] = {1};
long long total;


long long print(int t) {
	cout << n << "=";
	
	for (int i = 1; i <= t - 1; i++) {
		cout << a[i] << "+";
	}
	cout << a[t] << endl;
	total++;
}

long long search(int s,int t) {
//	cout << 1;
    
	for (int i = a[t - 1]; i <= s; i++) {
//		cout << a[t - 1] << endl; 
		if (i < n) 
		{
			a[t] = i;
			s -= i;
			if (s == 0)	print(t);
			
			else {
				search(s,t + 1);
			}
//			cout << i << endl;
			s+=i;
		}
	}
}

int main () {
	cin >> n;
	
	search(n,1);
	
	cout << "total=" << total << endl;
	
	return 0;
}




2021/7/20 15:41
加载中...