题目要求是输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
这是我改别人的代码,现在怎么用递归去实现,貌似递归代码只有10多行而已,写不出来。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{ public class MySort
{ private List<string> s0 = new List<string>();
private List<string> r0 = new List<string>();
public MySort()
{
s0.Add("1");
s0.Add("2");
s0.Add("2");
s0.Add("3");
s0.Add("4");
s0.Add("5");
}
private void Sort()
{
string str="";
for(int i0=0;i0<s0.Count;i0++){
List<string> s1=getNextList(s0,i0);
for(int i1=0;i1<s1.Count;i1++){
List<string> s2=getNextList(s1,i1);
for(int i2=0;i2<s2.Count;i2++){
List<string> s3=getNextList(s2,i2);
for(int i3=0;i3<s3.Count;i3++){
List<string> s4=getNextList(s3,i3);
for(int i4=0;i4<s4.Count;i4++){
List<string> s5=getNextList(s4,i4);
for(int i5=0;i5<s5.Count;i5++){
str=s0[i0]+s1[i1]+s2[i2]+s3[i3]+s4[i4]+s5[i5];
if (check(str)&&!r0.Contains(str)) {
r0.Add(str);
}
}
}
}
}
}
}
}
private List<String> getNextList(List<String> inList, int i) {
List<String> rs = new List<String>();
rs.AddRange(inList);
rs.Remove(rs[i]);
return rs;
} private bool check(String s) {
if (s[2] == '4' || s.Contains("35") || s.Contains("53")) {
return false;
}
return true;
}
private void printStr() {
int lineN = 10;
foreach(var s in r0) {
Console.Write(s + " ");
if (lineN-- == 1) {
Console.WriteLine();
lineN = 10;
}
}
Console.WriteLine(r0.Count);
} static void Main(string[] args)
{
MySort mySort = new MySort();
mySort.Sort();
mySort.printStr();
Console.Read();
}
}
}
这是我改别人的代码,现在怎么用递归去实现,貌似递归代码只有10多行而已,写不出来。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{ public class MySort
{ private List<string> s0 = new List<string>();
private List<string> r0 = new List<string>();
public MySort()
{
s0.Add("1");
s0.Add("2");
s0.Add("2");
s0.Add("3");
s0.Add("4");
s0.Add("5");
}
private void Sort()
{
string str="";
for(int i0=0;i0<s0.Count;i0++){
List<string> s1=getNextList(s0,i0);
for(int i1=0;i1<s1.Count;i1++){
List<string> s2=getNextList(s1,i1);
for(int i2=0;i2<s2.Count;i2++){
List<string> s3=getNextList(s2,i2);
for(int i3=0;i3<s3.Count;i3++){
List<string> s4=getNextList(s3,i3);
for(int i4=0;i4<s4.Count;i4++){
List<string> s5=getNextList(s4,i4);
for(int i5=0;i5<s5.Count;i5++){
str=s0[i0]+s1[i1]+s2[i2]+s3[i3]+s4[i4]+s5[i5];
if (check(str)&&!r0.Contains(str)) {
r0.Add(str);
}
}
}
}
}
}
}
}
private List<String> getNextList(List<String> inList, int i) {
List<String> rs = new List<String>();
rs.AddRange(inList);
rs.Remove(rs[i]);
return rs;
} private bool check(String s) {
if (s[2] == '4' || s.Contains("35") || s.Contains("53")) {
return false;
}
return true;
}
private void printStr() {
int lineN = 10;
foreach(var s in r0) {
Console.Write(s + " ");
if (lineN-- == 1) {
Console.WriteLine();
lineN = 10;
}
}
Console.WriteLine(r0.Count);
} static void Main(string[] args)
{
MySort mySort = new MySort();
mySort.Sort();
mySort.printStr();
Console.Read();
}
}
}
2,使用这个数和抽取剩下的数据搭配得到一个组合(循环放到 0 to 5的位置 )。
3,如果组合还未存在于结果集而且不符合给定的条件(4不能在第三位,3和5不能相邻 ),则存到结果集。
4,如果原始List.Count>0,则递归调用自己。