110,538
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "25482";
int n = 2;
var data = Enumerable.Range(1, s.Length - 1);
IEnumerable<int[]> query = data.Select(x => new int[] { x });
for (int i = 1; i < n; i++)
{
query = query.SelectMany(x => data.Select(y => x.Concat(new int[] { y }).ToArray()));
}
query = query
.Select(x => x.OrderBy(y => y).Distinct().ToArray())
.Where(x => x.Count() == n - 1)
.OrderByDescending(x => string.Join(",", x))
.GroupBy(x => string.Join(",", x))
.Select(x => x.First())
.Select(x => new int[] { 0 }.Concat(x).ToArray());
foreach (var item in query)
{
Console.WriteLine(string.Join("", s.Select((x, i) => item.Contains(i) ? "x" + x : x.ToString())));
}
}
}
}