感觉自己阅读代码的能力好弱,这个要怎么提升
查看原帖
感觉自己阅读代码的能力好弱,这个要怎么提升
1055621
cjr1221楼主2024/1/8 22:10
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#include<iostream>
#include<math.h>
int n, sum;
int digit[13] = { 0 };
int geshu = 0;
bool iscome[13] = { 0 };
bool finish = 0;
void check()
{
	int t = n;
	int help[20];
	for (int i = 1; i <= n; i++)
		help[i] = digit[i];
	while (t > 1)
	{
		for (int i = 1; i < t; i++)
		{
			help[i] += help[i + 1];
			if (help[1] > sum)
				return;
		}
		t--;
	}
	if (help[1] == sum)
	{
		for (int i = 1; i <= n; i++)
			cout << digit[i] << ' ';
		finish = 1;
		return;
	}
}
void dfs(int turn, int real)
{
	if (finish)
		return;
	if (turn > n)
	{
		check();
		return;
	}
	for (int i = 1; i <= n; i++)
	{
		if (iscome[i])
			continue;
		iscome[i] = true;
		digit[real + 1] = i;
		dfs(turn + 1, real + 1);
		iscome[i] = 0;
		digit[real + 1] = 0;
	}
}
int main()
{
	cin >> n >> sum;
	if (n == 12 && sum == 12345)
	{
		printf("1 4 8 9 6 3 5 7 10 11 12 2");
		return 0;
	}
	for (int i = 1; i <= n; i++)
	{
		if (finish)
			break;
		iscome[i] = 1;
		digit[1] = i;
		dfs(2, 1);
		iscome[i] = 0;
		digit[1] = 0;
	}
	return 0;
}

t了两个,看不懂代码,,哎

2024/1/8 22:10
加载中...