随笔 - 51  文章 - 0 评论 - 210 trackbacks - 6
<2008年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

严正声明 : 本站文章转载请注明出处.

与我联系

搜索

 

留言簿(11)

我的标签

随笔分类

随笔档案

Atlas相关

积分与排名

  • 积分 - 58802
  • 排名 - 800

阅读排行榜

在一些数学问题里面可能会用到, 使用递归做的,也许不算最好,仅作参考:

 

 

using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
//我们做一个最简单的排列组合,求出字符串的全排列.
            char[] arr_char = new char[] { 'A''B''C''D','E' };


            
string[] result = get_all_position(null,arr_char,arr_char.Length);

            Console.WriteLine(
string.Join(",",result));
            Console.WriteLine(
"元素个数: " + result.Length.ToString() + " 个");
            Console.ReadLine();
        }

        
static string[] get_all_position(string[] all_position, char[] arr_char, int level)
        {
            
string[] base_position = null;

            
if (level == 0)
                
return new string[] { arr_char[0].ToString() };
            
else
                base_position 
= get_all_position(all_position, arr_char, level - 1);

            
if (level == arr_char.Length)
                
return base_position;

            
char new_char = arr_char[level];

            
int string_length = 0;
            
if (base_position.Length > 0)
                string_length 
= base_position[0].Length;

            
string[] new_position = new string[base_position.Length * (level+1)];

            
for (int i = 0; i < base_position.Length; i++)
            {
                new_position[(string_length
+1* i] = new_char + base_position[i];
                
for (int k = 0; k < string_length; k++)
                {
                    new_position[(string_length
+1* i + k + 1= base_position[i][k] + base_position[i].Replace(base_position[i][k], new_char);
                }
            }

            
return new_position;
        }
    }


}
Tag标签: 算法,全排列
posted on 2008-08-17 16:19 沙加 阅读(119) 评论(0)  编辑 收藏 网摘 所属分类: 算法研究

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
Google站内搜索


China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!

相关文章:


相关搜索:
算法 全排列

相关链接: