使用以下代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class Program
{
public class PermutationFinder<T>
{
private T[] items;
private Predicate<T[]> SuccessFunc;
private bool success = false;
private int itemsCount;
public void Evaluate(T[] items, Predicate<T[]> SuccessFunc)
{
this.items = items;
this.SuccessFunc = SuccessFunc;
this.itemsCount = items.Count();
Recurse(0);
}
private void Recurse(int index)
{
T tmp;
if (index == itemsCount)
success = SuccessFunc(items);
else
{
for (int i = index; i < itemsCount; i++)
{
tmp = items[index];
items[index] = items[i];
items[i] = tmp;
Recurse(index + 1);
if (success)
break;
tmp = items[index];
items[index] = items[i];
items[i] = tmp;
}
}
}
}
static void Main(string[] args)
{
new Program().Start();
}
void Start()
{
string[] items = new string[9];
items[0] = "1";
items[1] = "2";
items[2] = "3";
items[3] = "5";
items[4] = "6";
items[5] = "7";
items[6] = "8";
items[7] = "9";
items[8] = "0";
new PermutationFinder<string>().Evaluate(items, Evaluate);
Console.ReadLine();
}
public bool Evaluate(string[] items)
{
Console.WriteLine(string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}", items[0], items[1], items[2], items[3], items[4], items[5], items[6], items[7], items[8]));
bool someCondition = false;
if (someCondition)
return true; // Tell the permutation finder to stop.
return false;
}
}
}
我能够生成序列的排列&#34; 1,2,3,5,6,7,8,9,0&#34;。 我正在寻找的是一种方法,首先将每个生成的排列转换为整数,将整数乘以2,然后检查它是否是整数的排列&#34; 1234567890&#34;。如果是,我希望它输出原始整数。有谁知道如何实现这个目标?
谢谢!