日期:2014-05-20 浏览次数:21022 次
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication9
{
class Program
{
private int x;
private void Permutation_Solution1(char[] pStr, int begin, int end,int min,int max)
{
int count = 0;
if (begin == end - 1) //只剩一个元素
{
for (int i = x; i < end; i++)
{
count++;
}
if (count >= min && count <= max)
{
for (int i = x; i < end; i++) //打印
{
Console.Write(pStr[i]);
}
Console.WriteLine(string.Empty);
}
}
else
{
for (int k = begin; k < end; k++)
{
swap(ref pStr[k], ref pStr[begin]); //交换两个字符
Permutation_Solution1(pStr, begin + 1, end,min,max);
swap(ref pStr[k], ref pStr[begin]); //恢复
}
}
}
private void swap(ref char p1,ref char p2)
{
char c;
c = p1;
p1 = p2;
p2 = c;
}
static void Main(string[] args)
{
string str = "abcde";
char[] c =str.ToCharArray();
Program g = new Program();
int len=str.Length;
Console.WriteLine("请输入min,max(在1和{0}之间)",len );
int min = Convert.ToInt32(Console.ReadLine());
int max = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
g.x = i;
g.Permutation_Solution1(c, i, j+1,min ,max);
}
}
Console.ReadKey();
}
}
}