【问题标题】:C# sorting from chosen # nearest to farthestC#从选择的#最近到最远排序
【发布时间】:2018-03-29 04:35:06
【问题描述】:

使用 System.Linq;

        int min = 1;
        int max = 100;
        int[] a = new int[10];
        Random randNum = new Random();
        for (int i = 0; i < a.Length; i++)
        {
            a[i] = randNum.Next(min, max);
        }
        int t;
        Console.WriteLine("array :");
        foreach (int aa in a)
            Console.Write(aa + " ");
        Console.Write("\n");
        Console.WriteLine("Chosen number");
        {... }
            a = a.OrderBy(e => Math.Abs(e, b));
        foreach (var b in a) Console.Write("{0} ", x);
        {
            int b = int.Parse(Console.ReadLine());


            {
                for (int p = 0; p <= a.Length - 2; p++)
                {
                    for (int i = 0; i <= a.Length - 2; i++)
                    {
                        if (a[i]> a[i + 1])
                        {
                            t = a[i + 1];
                            a[i + 1] = a[i];
                            a[i] = t;

                        }

                    }
                }
                Console.Write("\n");
                Console.WriteLine("\n" + "+ :");
                foreach (int aa in a)
                    Console.Write(aa + " ");
                Console.Write("\n");
            }

这是我尝试使用的代码。但我想做的是选择某个数字并排序离所选数字最近的距离。例如数组中有 7、5、3、2、9,假设我从数组中选择 5。然后结果应该出来 5 7 3(顺序无关)2 9.请帮助我,我很菜鸟TnT

【问题讨论】:

标签: c# arrays sorting compare


【解决方案1】:

更改条件(a[i] &gt; a[i + 1]) 以比较(a[i] and b) 和(a[i+1] and b) 之间的差异

【讨论】:

  • 我不明白你在说什么,你能更具体地帮助我吗?
  • @장재현 你有什么问题?只需将数组项的比较替换为数组项与数字之间的差异比较
  • 我必须继续使用if吗?或删除是否进行比较??
【解决方案2】:

您想按数组元素与所选数字之间的距离对数组进行排序。假设a 是一个数字数组,b 是选择的数字,

然后替换 for (;;) { ... } 有这个

using System.Linq;

a = a.OrderBy(e => (int) Math.Abs(e - b)).ToArray();
foreach (var x in a) Console.Write("{0} ", x);

【讨论】:

  • 正如你所说,我来到这里,但它仍然给我错误。请帮助我更多
【解决方案3】:

好吧,我花了一些时间来理解你想要什么样的类型,然后我查看了你的代码,在我看来你让自己陷入了一个过于复杂的方法,所以我决定我'我不会尝试帮助您编写自己的代码,而是从头开始编写整个排序。

我最终得到了一个使用辅助数组但只使用单个循环来完成订单的方法:

void StrangeSort(int[] arr, int num)
{
    var index = Array.IndexOf(arr, num);
    if(index > -1)
    {
        var copy = new int[arr.Length];
        int copyIndex = 0, 
            down = index-1, 
            up = index+1;
        copy[copyIndex] = arr[index];
        while(down > -1 || up < arr.Length)
        {
            if(down > -1)
            {
                copyIndex++;
                copy[copyIndex] = arr[down];
                down--;
            }
            if(up < arr.Length)
            {
                copyIndex++;
                copy[copyIndex] = arr[up];
                up++;
            }
        }
        copy.CopyTo(arr, 0);
    }
}

You can see a live demo on rextester.

当然,总是有 linq:

var index = Array.IndexOf(arr, num); // arr being your array, num being the value selected

arr = arr.Select((v, i) => new {Value = v, Index = i})
        .OrderBy(e => Math.Abs(index - e.Index))
        .Select(e => e.Value)
        .ToArray();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    相关资源
    最近更新 更多