(1)3和5不能相连
(2)4不能在第三位
给出你的算法。
我是这样做的。
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//char[] a ={ '1','2','2','3','4','5'};
char[] a ={ '1','2','2','3','4','5'};
int i = 0;
IList result=Program.PaiLie(a);
IList result2 = new ArrayList();
//StreamWriter tw = new StreamWriter("c:\\out.txt");
//Console.SetOut(tw);
foreach (string str in result)
{
if (str.Contains("35") || str.Contains("53") || str.IndexOf('4') == 3)
{
i++;
}
else
result2.Add(str);
//Console.WriteLine(str);
}
foreach (string str2 in result2)
{
Console.WriteLine(str2);
}
//tw.WriteLine("i = " + i);
//tw.WriteLine("result:-- " + result.Count);
//tw.WriteLine("Total:-- "+result2.Count);
Console.WriteLine("i = " + i);
Console.WriteLine("result:-- " + result.Count);
Console.WriteLine("Total:-- "+result2.Count);
}
public static IList PaiLie(char[] a)
{
IList resultStr =new ArrayList();
//resultStr.Add(new string(a));
int n = a.Length;
if (n == 2)
{
resultStr.Add(new string(a));
char temp=a[0];
a[0] = a[1];
a[1] = temp;
resultStr.Add(new string(a));
}
if (n > 2)
{
for (int i = 0; i < n; i++)
{
string b = new string(a);
b=b.Remove(i, 1);
char[] c = b.ToCharArray();
IList tempStr = PaiLie(c);
foreach (string s in tempStr)
{
string xx = a[i].ToString()+s;
resultStr.Add(xx);
}
}
}
return resultStr;
}
}
}
去掉部分注释,可以导出到文件。using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//char[] a ={ '1','2','2','3','4','5'};
char[] a ={ '1','2','2','3','4','5'};
int i = 0;
IList result=Program.PaiLie(a);
IList result2 = new ArrayList();
//StreamWriter tw = new StreamWriter("c:\\out.txt");
//Console.SetOut(tw);
foreach (string str in result)
{
if (str.Contains("35") || str.Contains("53") || str.IndexOf('4') == 3)
{
i++;
}
else
result2.Add(str);
//Console.WriteLine(str);
}
foreach (string str2 in result2)
{
Console.WriteLine(str2);
}
//tw.WriteLine("i = " + i);
//tw.WriteLine("result:-- " + result.Count);
//tw.WriteLine("Total:-- "+result2.Count);
Console.WriteLine("i = " + i);
Console.WriteLine("result:-- " + result.Count);
Console.WriteLine("Total:-- "+result2.Count);
}
public static IList PaiLie(char[] a)
{
IList resultStr =new ArrayList();
//resultStr.Add(new string(a));
int n = a.Length;
if (n == 2)
{
resultStr.Add(new string(a));
char temp=a[0];
a[0] = a[1];
a[1] = temp;
resultStr.Add(new string(a));
}
if (n > 2)
{
for (int i = 0; i < n; i++)
{
string b = new string(a);
b=b.Remove(i, 1);
char[] c = b.ToCharArray();
IList tempStr = PaiLie(c);
foreach (string s in tempStr)
{
string xx = a[i].ToString()+s;
resultStr.Add(xx);
}
}
}
return resultStr;
}
}
}
大家可以给出更好的算法啊,多提宝贵意见!