Full array algorithms (C language)

/ *
Recursive function ideas:
Example: Input 123.
Output 1-2-3.
1-3-2.
2-1-3.
2-3-1.
3-1-2.
3-2-1.
Law: under conditions of the number n, the number of each will be in the first (n-1) times. Here with a for loop on the line.
And then the number of their full-back arrangement. This creates recursive.
* /
#Include <stdio.h>
#include <stdlib.h>

INPUT int (int []);
// input array, and returns the number of array elements.
INPUT int (the nums int [])
{
int I;
char C = '0';

for (i = 0; c != '\n'; i++)
{
	scanf_s("%d", &nums[i]);
	c = getchar();
}
return i;

}

QuanPaiLie void (int [], int, int);
// recursive, the last n-1 before a full array placed. And so on.
// p, a final number. After the number q, "leader."
QuanPaiLie void (the nums int [], int P, Q int)
{
int I, TEMP;

//递归的结束条件。最后只有一个数,它的全排列就是他自己。
if (p == q)
{
	for (i = 0; i <= q; i++)
	{
		printf("%d-", nums[i]);
	}
	printf("\n");
}
else
{
	//每个数都当一次该全排列的“龙头”。
	for (i = p; i <= q; i++)
	{
		//将那个数交换该排列的到“龙头”。
		temp = nums[i];
		nums[i] = nums[p]; 
		nums[p] = temp;
	
		//将“龙头”的后面再进行全排列
		QuanPaiLie(nums, p + 1, q);

		//用完要换回来。
		temp = nums[i];
		nums[i] = nums[p];
		nums[p] = temp;
	}
}

}

int main()
{
int nums[999], num;

num = input(nums);
QuanPaiLie(nums,0,num-1);

system("pause");
return 0;

}

发布了1 篇原创文章 · 获赞 0 · 访问量 24

Guess you like

Origin blog.csdn.net/qq_45914759/article/details/104045633