本文介绍了在生成特定的顺序组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个函数来生成一个严格的顺序组合。 这里有一个例子:
{0,1,2,3}:
0 2 1 3 0 1 3 2 0 2 1 3 0 2 3 1 0 3 1 2 0 3 2 1 1 0 2 3 1 0 3 2 1 2 0 3 1 2 3 0 1 3 0 2 1 3 2 0 2 0 1 3 2 0 3 1 2 1 0 3 2 1 3 0 2 3 0 1 2 3 1 0 3 0 1 2 3 0 2 1 3 1 0 2 3 1 2 0 3 2 0 1 3 2 1 0
所以,我需要这个特定的顺序,没有任何other.I已经尝试了一些硬code步骤猜测算法,但有一个现有的功能,可以为我做的?
更新:
的#include< stdio.h中> #包括< string.h中> INT主要(无效) { 炭键[] = {'0','1','2','3','\ 0',}; 炭缓冲[] = {'0','1','2','3','\ 0',}; 的printf(%S \ N键); 交换(安培;键[2],&安培;按键[3]); 的printf(%S \ N键); strcpy_s(键,的sizeof(缓冲区),缓冲区); 交换(安培;键[1],&安培;键[2]); 的printf(%S \ N键); 交换(安培;键[2],&安培;按键[3]); 的printf(%S \ N键); strcpy_s(键,的sizeof(缓冲区),缓冲区); 交换(安培;键[1],&安培;按键[3]); 交换(安培;键[2],&安培;按键[3]); 的printf(%S \ N键); 交换(安培;键[2],&安培;按键[3]); 的printf(%S \ N键); } 无效掉期(字符* X,字符* Y) { 焦炭TMP; TMP = * X; * X = * Y; * Y = TMP; }解决方案
我给你的算法,我曾经读过。 (一些网站)
- 最初,排序的元素,以获得的最小排列。
- 从右边开始,并保持向左移动,只要字母增加值让它成为X
- 在后面的x查找大于x序列中较大的最小值X ..s ..,并用X掉它。 (让它成为S)。 (那么这将是S ..x ..)
- 在这样做之后,反向超越S中的字母。
这就是你现在执行的算法。
假设你有拿到1 3 4 2下一个置换 +步骤2: 1 3 4 2 | +步骤3: 1 3 4 2(4大于3) | | 1 4 3 2 +步骤-4- 扭转它 1 4 3 2 ----> 1 4 2 3 | - |I need a function to generate combinations in a strict order. here's an example :
{0,1,2,3} :
0 1 2 3 0 1 3 2 0 2 1 3 0 2 3 1 0 3 1 2 0 3 2 1 1 0 2 3 1 0 3 2 1 2 0 3 1 2 3 0 1 3 0 2 1 3 2 0 2 0 1 3 2 0 3 1 2 1 0 3 2 1 3 0 2 3 0 1 2 3 1 0 3 0 1 2 3 0 2 1 3 1 0 2 3 1 2 0 3 2 0 1 3 2 1 0So,I need This particular order,not any other.I have tried a few hard-code steps to guess the algorithm,but is there an existing function that can do it for me?
UPDATE:
#include <stdio.h> #include <string.h> int main(void) { char keys[] = {'0','1','2','3','\0',}; char buffer[] = {'0','1','2','3','\0',}; printf("%s\n",keys); Swap(&keys[2],&keys[3]); printf("%s\n",keys); strcpy_s(keys,sizeof(buffer),buffer); Swap(&keys[1],&keys[2]); printf("%s\n",keys); Swap(&keys[2],&keys[3]); printf("%s\n",keys); strcpy_s(keys,sizeof(buffer),buffer); Swap(&keys[1],&keys[3]); Swap(&keys[2] ,&keys[3]); printf("%s\n",keys); Swap(&keys[2],&keys[3]); printf("%s\n",keys); } void Swap(char *x,char *y) { char tmp; tmp = *x; *x = *y; *y = tmp; }解决方案
I am giving you the algorithm I once read. (from some site)
- Initially, sort the elements to obtain the minimum permutation.
- Begin from the right and keep moving left as long as the letters are increasing in value let it be x
- Find the smallest value bigger than x in the sequence "x ..s.." after x and swap it with x. (let it be s). (then it will be "s ..x..")
- Having done this, reverse the letters beyond s.
This is the algorithm that you have to implement now.
Suppose you have to get the next permutation of 1 3 4 2 + Step-2: 1 3 4 2 | + step-3: 1 3 4 2 (4 greater than 3 ) | | 1 4 3 2 + step-4 reverse it 1 4 3 2 ----> 1 4 2 3 |--|
更多推荐
在生成特定的顺序组合
发布评论