C#练习题答案: 排序奇数【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

这是一篇关于C#编程的基础练习,重点在于排序数组中的奇数。提供了10种不同的解决方案,适合C#初学者巩固数据类型和数组操作知识。
摘要由CSDN通过智能技术生成

排序奇数【难度:2级】:

答案1:

using System.Collections.Generic;
using System.Linq;

public class Kata
{
    public static int[] SortArray(int[] array)
    {
        Queue<int> odds = new Queue<int>(array.Where(e => e%2 == 1).OrderBy(e => e));
    
        return array.Select(e => e%2 == 1 ? odds.Dequeue() : e).ToArray();
    }
}

答案2:

using System;
using System.Collections.Generic;

public class Kata
{
  public static int[] SortArray(int[] array)
  {
      List<int> odd = new List<int>();
      
      foreach (var number in array)
      {
        if (number %2 != 0)
          odd.Add(number);        
      }
      int[] oddAr = odd.ToArray();
      Array.Sort(oddAr);
      
      
      for (int i = 0, j = 0; i<array.Length; i++)
      {
        if (array[i] %2 != 0)
        {
          array[i] = oddAr[j];
          j++;
        }
      }
     return array;
  }
}

答案3:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
{
  public static int[] SortArray(int[] array)
  {
      var list = new List<int>(array);
      var oddNumbers = list.Where(x => x % 2 == 1).OrderBy(x => x).ToList();
      var oddIndex = 0;
      return list.Select(x => (x % 2 == 1) ? oddNumbers[oddIndex++] : x).ToArray();  
  }
}

答案4:

using System.Linq;

public class Kata
{
    public static int[] SortArray(int[] array)
    {
        int[] sortedOddNumbers = array.Where(x => x % 2 == 1).OrderBy(x => x).ToArray();
        int i = 0;
        foreach (int oddNumber in sortedOddNumbers)
        {            
            while(array[i] % 2 == 0)
            {
                i++;
            }
            array[i] = oddNumber;
            i++;
        }
        return array;
    }
}

答案5:

using System.Collections.Generic;
using System.Linq;

public class Kata
{
  public static int[] SortArray(int[] array)
  {
    Queue<int> sortedOddNumbers = new Queue<int>(array.Where(i => IsOdd(i)).OrderBy(i => i));

    return array.Select(i => IsOdd(i) ? sortedOddNumbers.Dequeue() : i).ToArray();
  }

  private static bool IsOdd(int i)
  {
    return i % 2 == 1;
  }
}

答案6:

using System;
using System.Collections.Generic;

public class Kata
{
  public static int[] SortArray(int[] array)
  {
    if(array.Length == 0) return array;
    for(int i=0;i<array.Length-1;i++)
    {
      if(array[i]%2==0) continue;
      for(int j=i+1;j<array.Length;j++)
      {
        if(array[j]%2==0) continue;
        if(array[j]<array[i])
        {
          int aux = array[i];
          array[i] = array[j];
          array[j] = aux;
        }
      }
    }
    return array;
  }
  
}

答案7:

using System;
using System.Linq;
using System.Collections.Generic;
public class Kata
{
  public static int[] SortArray(int[] array)
  {
    var odds = array.Where(x => x % 2 != 0).ToArray(); //create new array of odds
    Array.Sort(odds); 
    var ans = new List<int>(odds); //convert into list so it can be modified 
    foreach(var item in array.Select((Value, Index) => new {Value, Index}) //instantiate val and index reference for each item 
                             .Where(x => x.Value % 2 == 0)) //filter evens
    {
      if (item.Index > ans.Count()-1) ans.Add(item.Value); //if index in orig array is out of bonds of the current list, add it to the end
      else ans.Insert(item.Index, item.Value); //else, insert it at the index in orig arraay
    }
    return ans.ToArray();
  }
}

答案8:

using System;

public class Kata
{
  public static int[] SortArray(int[] array)
  {
    int len = array.Length, oddsIdx = 0;
    int[] odds = new int[len],
      indexes = new int[len];
    
    for (int i = 0; i < len;i++)
      if (array[i] % 2 != 0) 
        odds[oddsIdx] = array[(indexes[oddsIdx++] = i)];
      
    Array.Sort(odds);
      
    for (int i = (len - oddsIdx), n = 0; i < len; i++, n++)
      array[indexes[n]] = odds[i];
      
    return array;
  }
}

答案9:

using System;

public class Kata
{
  public static int[] SortArray(int[] array)
  {
    int len = array.Length;
    int[] odds = new int[len];
    
    for (int i = 0, oddsIdx = 0, tempInt = 0; i < len; i++)
      if ((tempInt = array[i]) % 2 != 0){
        array[i] = -1;
        odds[oddsIdx++] = tempInt;
      }
    
    Array.Sort(odds);

    for (int i = --len, oddsIdx = len; i > -1; i--)
      if (array[i] == -1) array[i] = odds[oddsIdx--];

    return array;
  }
}

答案10:

public class Kata
{
  public static int[] SortArray(int[] array)
  {
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] % 2 != 0)
                {
                    var aux = 0;
                    for (int j = i + 1; j < array.Length; j++)
                    {
                        if (array[i] > array[j] &amp;&amp; array[j] % 2 != 0)
                        {
                            aux = array[i];
                            array[i] = array[j];
                            array[j] = aux;
                        }
                    }
                }
            }
            return array;
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值