标题:关于数字排列问题
只看楼主
yelang7
Rank: 1
等 级:新手上路
威 望:1
帖 子:265
专家分:0
注 册:2006-11-3
 问题点数:0 回复次数:2 
关于数字排列问题
最近我在网上看到这样一个数字排列的程序题目,想了一些,怎么都还是有不合提议的地方,所以我把它贴上来,大家一起探讨.

程序題目:
用1、2、2、3、4、5这六个数字,用C#写一个main函数,打印出所有不同的排列,
如512234、412345等。
要求:"4"不能在第三位,"3"与"5"不能相连.
解决思路:强化题目,用1、2、2、3、4、5这六个数字排列“递增”序列。其他要求不变。
算法思路:显然是递归,初始序列122345,先从末两位(45)变化(45,54),然后末三位(345) ... 直到最后六位.怎样解决重复问题?很简单,由于是递增序列,每生成新序列可与前一生成序列比较,如<放弃当前序列。当然有更好效率,如预先预测。

如果有结果了,可以给我留言.我的QQ:281665154;MSN和E-MAIL:dmdenanpy@126.com
搜索更多相关主题的帖子: 数字排列 序列 思路 
2007-01-25 10:07
yelang7
Rank: 1
等 级:新手上路
威 望:1
帖 子:265
专家分:0
注 册:2006-11-3
得分:0 

本问题以解决,是用的递归函数,我们再来想想用没有其他的解决办法的???????

/*排列组合*/
/*题目:用1、2、2、3、4、5这六个数字,用C#写一个main函数,打印出所有不同的排列,*/
/*如512234、412345等。要求:"4"不能在第三位,"3"与"5"不能相连。*/
const int MAXN = 6;//排列数组的长度
static int[] Array = new int[MAXN];//用来储存1,2,2,3,4,5这六个排列的数
static int[] TemporaryArray = new int[MAXN];//用来临时储存排列好后的序列
static int[] PositionState = new int[MAXN];//用来标记各个位置的状态
static int count = 0;//用来记录符合题意的排列数长度
//打印每次排列过后的一个数
private static void Print()
{
for (int i = 0; i < MAXN; ++i)
{
Console.Write("{0}", TemporaryArray[i]);
}
Console.Write(" ");

}
//对Array[MAXN]中的数进行排列
private static void NORepeatedPermutation(int depth)
{
int i;

for (i = 0; i < MAXN; ++i)
{
if (PositionState [i] > 0)
{
--PositionState [i];

if (
!(i + 1 == 4 && depth == 2) && // 4 at position 3
!(i + 1 == 3 && depth > 0 && TemporaryArray[depth - 1] == 5) && // 53
!(i + 1 == 5 && depth > 0 && TemporaryArray[depth - 1] == 3) // 35
)
{
TemporaryArray[depth] = i + 1;
if (MAXN - 1 == depth)
{
++count;
//if (count%10 == 0)
//{
// Console.WriteLine();
//}
Print();
}
else
NORepeatedPermutation(depth + 1);
}

++PositionState [i];
}
}

}

static void Main(string[] args)
{
//初始化Array[MAXN]
Array[0] = 1;
Array[1] = 2;
Array[2] = 2;
Array[3] = 3;
Array[4] = 4;
Array[5] = 5;
for (int i = 0; i < MAXN; i++)
{
++PositionState[Array[i] - 1]; //PositionState [] 的值为:1,2,1,1,1,0
}

Console.WriteLine("The value arranged completely is :");
NORepeatedPermutation(0);
Console.WriteLine();
Console.WriteLine("Total count:{0}", count);
Console.ReadLine();
}

[此贴子已经被作者于2007-1-25 16:49:59编辑过]


想象和行动一致,做最棒的 IT 人. http://yelang7.
2007-01-25 14:26
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
得分:0 
楼主有点牛哦,呵呵.

飘过~~
2007-01-25 20:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-118006-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.133755 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved