当前位置:编程学习 > C#/ASP.NET >>

c#排序问题请教?

winform程序,例如在文本框里输入0-9的任意一位数字,例如1234,对其进行排列,先取出其中的两位,例如12进行排列,比如得出的结果是12xx,21xx,1x2x,2x1x,xx12,xx21,等等。(x)代表空。,在取三位进行排列,例如123x,1x23,12x3等等,再取四个进行排列,例如1234,2134等。请问这样的代码怎么写? --------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> list = new List<string>();
            foreach (var item in Arrange("1234".ToList(), new List<char>()))
            {
                foreach (var item1 in AddReplaceChar(new string(item.ToArray()), 'x'))
                {
                    list.Add(item1);
                }
            }
            list.OrderBy(x => x).Distinct().ToList().ForEach(x => Console.WriteLine(x));
        }

        static IEnumerable<List<T>> Arrange<T>(List<T> source, List<T> current)
        {
            if (current.Count == source.Count)
            {
                yield return current;
            }
            else
            {
                foreach (var item in source)
                {
                    if (!current.Any(x => x.Equals(item)))
                    {
                        foreach (var item1 in Arrange(source, current.Union(new List<T>() { item }).ToList()))
                        {
                            yield return item1;
                        }
                    }
                }
            }
        }

        static IEnumerable<string> AddReplaceChar(string source, char replacechar)
        {
            foreach (var item in _AddReplaceChar(source, replacechar)
                                    .Where(x => 
                                        x != new string(Enumerable.Repeat(replacechar, source.Length).ToArray()))
                                    .Distinct())
                yield return item;
        }

        static IEnumerable<string> _AddReplaceChar(string source, char replacechar)
        {
            yield return source;
            for (int i = 0; i < source.Length; i++)
            {
                string replaced = source.Substring(0, i) + replacechar.ToString() + source.Substring(i + 1);
                yield return replaced;
                if (replaced != source)
                {
                    foreach (var item in AddReplaceChar(replaced, replacechar))
                    {
                        yield return item;
                    }
                }
            }
        }
    }
}


1234
123x
1243
124x
12x3
12x4
12xx
1324
132x
1342
134x
13x2
13x4
13xx
1423
142x
1432
143x
14x2
14x3
14xx
1x23
1x24
1x2x
1x32
1x34
1x3x
1x42
1x43
1x4x
1xx2
1xx3
1xx4
1xxx
2134
213x
2143
214x
21x3
21x4
21xx
2314
231x
2341
234x
23x1
23x4
23xx
2413
241x
2431
243x
24x1
24x3
24xx
2x13
2x14
2x1x
2x31
2x34
2x3x
2x41
2x43
2x4x
2xx1
2xx3
2xx4
2xxx
3124
312x
3142
314x
31x2
31x4
31xx
3214
321x
3241
324x
32x1
32x4
32xx
3412
341x
3421
342x
34x1
34x2
34xx
3x12
3x14
3x1x
3x21
3x24
3x2x
3x41
3x42
3x4x
3xx1
3xx2
3xx4
3xxx
4123
412x
4132
413x
41x2
41x3
41xx
4213
421x
4231
423x
42x1
42x3
42xx
4312
431x
4321
432x
43x1
43x2
43xx
4x12
4x13
4x1x
4x21
4x23
4x2x
4x31
4x32
4x3x
4xx1
4xx2
4xx3
4xxx
x123
x124
x12x
x132
x134
x13x
x142
x143
x14x
x1x2
x1x3
x1x4
x1xx
x213
x214
x21x
x231
x234
x23x
x241
x243
x24x
x2x1
x2x3
x2x4
x2xx
x312
x314
x31x
x321
x324
x32x
x341
x342
x34x
x3x1
x3x2
x3x4
x3xx
x412
x413
x41x
x421
x423
x42x
x431
x432
x43x
x4x1
x4x2
x4x3
x4xx
xx12
xx13
xx14
xx1x
xx21
xx23
xx24
xx2x
xx31
xx32
xx34
xx3x
xx41
xx42
xx43
xx4x
xxx1
xxx2
xxx3
xxx4
Press any key to continue . . . --------------------编程问答-------------------- 下面的输出是按照xx的多少排序:1xxx在最前,4321在最后。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> list = new List<string>();
            foreach (var item in Arrange("1234".ToList(), new List<char>()))
            {
                foreach (var item1 in AddReplaceChar(new string(item.ToArray()), 'x'))
                {
                    list.Add(item1);
                }
            }
            list.OrderByDescending(x => x.Where(y => y == 'x').Count())
                .ThenBy(x => x).Distinct()
                .ToList()
                .ForEach(x => Console.WriteLine(x));
        }

        static IEnumerable<List<T>> Arrange<T>(List<T> source, List<T> current)
        {
            if (current.Count == source.Count)
            {
                yield return current;
            }
            else
            {
                foreach (var item in source)
                {
                    if (!current.Any(x => x.Equals(item)))
                    {
                        foreach (var item1 in Arrange(source, current.Union(new List<T>() { item }).ToList()))
                        {
                            yield return item1;
                        }
                    }
                }
            }
        }

        static IEnumerable<string> AddReplaceChar(string source, char replacechar)
        {
            foreach (var item in _AddReplaceChar(source, replacechar)
                                    .Where(x => 
                                        x != new string(Enumerable.Repeat(replacechar, source.Length).ToArray()))
                                    .Distinct())
                yield return item;
        }

        static IEnumerable<string> _AddReplaceChar(string source, char replacechar)
        {
            yield return source;
            for (int i = 0; i < source.Length; i++)
            {
                string replaced = source.Substring(0, i) + replacechar.ToString() + source.Substring(i + 1);
                yield return replaced;
                if (replaced != source)
                {
                    foreach (var item in AddReplaceChar(replaced, replacechar))
                    {
                        yield return item;
                    }
                }
            }
        }
    }
}

1xxx
2xxx
3xxx
4xxx
x1xx
x2xx
x3xx
x4xx
xx1x
xx2x
xx3x
xx4x
xxx1
xxx2
xxx3
xxx4
12xx
13xx
14xx
1x2x
1x3x
1x4x
1xx2
1xx3
1xx4
21xx
23xx
24xx
2x1x
2x3x
2x4x
2xx1
2xx3
2xx4
31xx
32xx
34xx
3x1x
3x2x
3x4x
3xx1
3xx2
3xx4
41xx
42xx
43xx
4x1x
4x2x
4x3x
4xx1
4xx2
4xx3
x12x
x13x
x14x
x1x2
x1x3
x1x4
x21x
x23x
x24x
x2x1
x2x3
x2x4
x31x
x32x
x34x
x3x1
x3x2
x3x4
x41x
x42x
x43x
x4x1
x4x2
x4x3
xx12
xx13
xx14
xx21
xx23
xx24
xx31
xx32
xx34
xx41
xx42
xx43
123x
124x
12x3
12x4
132x
134x
13x2
13x4
142x
143x
14x2
14x3
1x23
1x24
1x32
1x34
1x42
1x43
213x
214x
21x3
21x4
231x
234x
23x1
23x4
241x
243x
24x1
24x3
2x13
2x14
2x31
2x34
2x41
2x43
312x
314x
31x2
31x4
321x
324x
32x1
32x4
341x
342x
34x1
34x2
3x12
3x14
3x21
3x24
3x41
3x42
412x
413x
41x2
41x3
421x
423x
42x1
42x3
431x
432x
43x1
43x2
4x12
4x13
4x21
4x23
4x31
4x32
x123
x124
x132
x134
x142
x143
x213
x214
x231
x234
x241
x243
x312
x314
x321
x324
x341
x342
x412
x413
x421
x423
x431
x432
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321
Press any key to continue . . . --------------------编程问答-------------------- xx最多有2个:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> list = new List<string>();
            foreach (var item in Arrange("1234".ToList(), new List<char>()))
            {
                foreach (var item1 in AddReplaceChar(new string(item.ToArray()), 'x'))
                {
                    list.Add(item1);
                }
            }
            list.Where(x => x.Where(y => y == 'x').Count() <= 2)
                .OrderByDescending(x => x.Where(y => y == 'x').Count())
                .ThenBy(x => x).Distinct()
                .ToList()
                .ForEach(x => Console.WriteLine(x));
        }

        static IEnumerable<List<T>> Arrange<T>(List<T> source, List<T> current)
        {
            if (current.Count == source.Count)
            {
                yield return current;
            }
            else
            {
                foreach (var item in source)
                {
                    if (!current.Any(x => x.Equals(item)))
                    {
                        foreach (var item1 in Arrange(source, current.Union(new List<T>() { item }).ToList()))
                        {
                            yield return item1;
                        }
                    }
                }
            }
        }

        static IEnumerable<string> AddReplaceChar(string source, char replacechar)
        {
            foreach (var item in _AddReplaceChar(source, replacechar)
                                    .Where(x => 
                                        x != new string(Enumerable.Repeat(replacechar, source.Length).ToArray()))
                                    .Distinct())
                yield return item;
        }

        static IEnumerable<string> _AddReplaceChar(string source, char replacechar)
        {
            yield return source;
            for (int i = 0; i < source.Length; i++)
            {
                string replaced = source.Substring(0, i) + replacechar.ToString() + source.Substring(i + 1);
                yield return replaced;
                if (replaced != source)
                {
                    foreach (var item in AddReplaceChar(replaced, replacechar))
                    {
                        yield return item;
                    }
                }
            }
        }
    }
}

12xx
13xx
14xx
1x2x
1x3x
1x4x
1xx2
1xx3
1xx4
21xx
23xx
24xx
2x1x
2x3x
2x4x
2xx1
2xx3
2xx4
31xx
32xx
34xx
3x1x
3x2x
3x4x
3xx1
3xx2
3xx4
41xx
42xx
43xx
4x1x
4x2x
4x3x
4xx1
4xx2
4xx3
x12x
x13x
x14x
x1x2
x1x3
x1x4
x21x
x23x
x24x
x2x1
x2x3
x2x4
x31x
x32x
x34x
x3x1
x3x2
x3x4
x41x
x42x
x43x
x4x1
x4x2
x4x3
xx12
xx13
xx14
xx21
xx23
xx24
xx31
xx32
xx34
xx41
xx42
xx43
123x
124x
12x3
12x4
132x
134x
13x2
13x4
142x
143x
14x2
14x3
1x23
1x24
1x32
1x34
1x42
1x43
213x
214x
21x3
21x4
231x
234x
23x1
23x4
241x
243x
24x1
24x3
2x13
2x14
2x31
2x34
2x41
2x43
312x
314x
31x2
31x4
321x
324x
32x1
32x4
341x
342x
34x1
34x2
3x12
3x14
3x21
3x24
3x41
3x42
412x
413x
41x2
41x3
421x
423x
42x1
42x3
431x
432x
43x1
43x2
4x12
4x13
4x21
4x23
4x31
4x32
x123
x124
x132
x134
x142
x143
x213
x214
x231
x234
x241
x243
x312
x314
x321
x324
x341
x342
x412
x413
x421
x423
x431
x432
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321
Press any key to continue . . . --------------------编程问答-------------------- 我想把输出写到winform的界面上,而不是在控制台上,请我怎么实现? --------------------编程问答--------------------
引用 4 楼 bianyuandianfeng 的回复:
我想把输出写到winform的界面上,而不是在控制台上,请我怎么实现?


直接add到你的control里,比如listbox1.item.add之类的 --------------------编程问答--------------------
引用 4 楼 bianyuandianfeng 的回复:
我想把输出写到winform的界面上,而不是在控制台上,请我怎么实现?

都帮你写到这个份上了,还不知道怎么实现。让我怎一个郁闷了得。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,