穷举算法

xiaotupansy 2007-01-03 01:34:12
要一个实例
例如输入123
要能得到所有的排列
123,132,213,231,312,321,共有3!=6种
...全文
442 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
diandian82 2007-01-08
  • 打赏
  • 举报
回复
using System;
using System.Collections;

namespace MyNameSpace
{
class GenerateWords
{
public static void Main()
{
string str = "1234";
ArrayList result = new ArrayList();
ArrayList kinds = Generate_Permutations1(str);
for (int i = 0; i < kinds.Count; i++)
{
ArrayList res = Generate_Permutations(kinds[i].ToString());
result.AddRange(res);
}

Console.WriteLine("Count = " + result.Count);
for (int i = 0; i < result.Count; i++)
{
Console.WriteLine(result[i]);
}
}
public static ArrayList Generate_Permutations(string word)
{
ArrayList result = new ArrayList();
if (word.Length == 1)
{
result.Add(word);
return result;
}
for (int i = 0; i < word.Length; i++)
{
string shorter_word = word.Substring(0, i) + word.Substring(i + 1, word.Length - i - 1);
ArrayList shorter_Permutations = Generate_Permutations(shorter_word);
for (int j = 0; j < shorter_Permutations.Count; j++)
{
string longer_word = word[i].ToString() + shorter_Permutations[j].ToString();
result.Add(longer_word);
}
}
return result;
}

public static ArrayList Generate_Permutations1(string word)
{
ArrayList result = new ArrayList();
result.Add(word[word.Length - 1]);
if (word.Length <= 1)
return result;
for (int i = word.Length - 2; i >= 0; i--)
{
int count = result.Count;
for (int j = 0; j < count; j++)
{
result.Add(word[i].ToString() + result[j].ToString());
}
result.Add(word[i].ToString());
}
return result;
}

}
}
xiaotupansy 2007-01-07
  • 打赏
  • 举报
回复
比如说4个数字1,2,3,4,我要得到的结果的个数是要有A41+A42+A43+A44=64种
diandian82 2007-01-06
  • 打赏
  • 举报
回复
http://blog.csdn.net/diandian82/archive/2006/07/05/881284.aspx
xiaotupansy 2007-01-03
  • 打赏
  • 举报
回复
如果我要加上一个包含1,2位的全排列,比如说1,2,3,12,13,21,31,23,32,等等,那又应该怎么写啊?
j9988 2007-01-03
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program //计算P(M,N) M=3;N=3
{
static int Count=0; //存放排列组合数.
static void Main(string[] args)
{
int M = 3;
int N = 3;
int[] a = new int[M];

for (int i = 1; i <= M; i++) //数组赋初值"1,2,3"
{
a[i-1] = i;
}
if (M < N)
{
}
else
{
getValue(a, 0, N);
Console.WriteLine(Count);//输出组合数
Console.Read();
}

}

static void getValue(int[] a, int k, int n)
{
int temp;
if (k >= n) //输出结果:
{
string s = "";
for (int i = 0; i < n; i++)
{
s = s + a[i].ToString() + " ";
}
Count++;
Console.WriteLine(s);
}
else
{
for (int i = k; i < a.Length; i++)
{
temp = a[i];
a[i] = a[k];
a[k] = temp;
getValue(a, k + 1, n); //递归调用
temp = a[k];
a[k] = a[i];
a[i] = temp;
}
}
}
}
}
xiaotupansy 2007-01-03
  • 打赏
  • 举报
回复
代码请贴出来,谢谢!
shrinerain 2007-01-03
  • 打赏
  • 举报
回复
旋转法产生全排列.
pele007 2007-01-03
  • 打赏
  • 举报
回复
把数字分开存在一个数组里,然后进行排列组合的遍历
xiaotupansy 2007-01-03
  • 打赏
  • 举报
回复
给个地址连接好不好啊?
老大
JasonHeung 2007-01-03
  • 打赏
  • 举报
回复
my blog
xiaotupansy 2007-01-03
  • 打赏
  • 举报
回复
对了,顺便问一个word的小问题
我下载了一份word文档,我想把它里面的格式给清除掉。我点格式-清除所有格式,结果格式是清除了,但是旁边有个小方框还标明了原来的格式,我想把那个小方框给去掉,怎么做?
http://community.csdn.net/Expert/topic/5267/5267439.xml?temp=.1484644

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧