2、用1、2、2、3、4、5这六个数字,写程序打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"的右边不能是"5"。
C# code
public class Program
{
public static void Main()
{
List<int> list = new List<int>();
//n=50
for (int i = 1; i <=50; i++)
{
list.Add(i);
}
int index = 1;
int under = 0;
while (list.Count > 1)
{
Console.WriteLine(string.Format("{0}喊了{1}",list[under],index));
if (index == 3)
{
Console.WriteLine(string.Format("{0}被弹出", list[under]));
Console.Read();
list.RemoveAt(under);
under--;
index = 0;
}
if (under == list.Count - 1) under = -1;
index++;
under++;
}
}
}
public class Program
{
public static void Main()
{
List<string> list = new List<string>();
list.AddRange(GetString(new string[]{"1","2","2","3","4","5"}));
list.Sort();//排序,可以看清楚有没有重复
foreach (string s in list) {
if (s.IndexOf("4") != 2)
{
if(s.IndexOf("35")==-1)
Console.WriteLine(s);
}
}
}
static string[] GetString(string[] cs)
{
if (cs.Length == 2)
{
if (cs[0] == cs[1]) return new string[] { cs[0] + cs[1] };
return new string[] { cs[0] + cs[1], cs[1] + cs[0] };
}
List<string> list = new List<string>();
bool first = true;
for (int i = 0; i < cs.Length; i++)
{
List<string> tmp = new List<string>(cs);
tmp.RemoveAt(i);
if (tmp.IndexOf(cs[i]) != -1 && first) { first = false; continue; }
string[] sub = GetString(tmp.ToArray());
List<string> tmp2 = new List<string>();
foreach (string s in sub)
{
tmp2.Add(cs[i] + s);
}
list.AddRange(tmp2.ToArray());
}
return list.ToArray();
}
}