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

对角线弦【难度:2级】:

答案1:

using System;
using System.Linq;
using System.Text;

public class Kata
{
    public static string[] DiagonalsOfSquare(params string[] array)
    {
        int n = array.Length;
        if (n == 0 || array.Any(s => s.Length != n))
            return null;
        (string s, int i)[] sorted = array.Select((s, i) => (s, i)).OrderBy(s => s).ToArray();
        string[] res = new string[array.Length];
        for (int i = 0; i < n; i++)
            res[sorted[i].i] = string.Concat(Enumerable.Range(0, n).Select(j => sorted[(i + j) % n].s[j]));
        return res;
    }
}

答案2:

using System;
using System.Linq;

public class Kata
{
    public static string[] DiagonalsOfSquare(string[] array, int k = 0)
    {
        if (array.Length == 0 || array.Any(x => x.Length != array.Length)) return null;
        var l = array.OrderBy(x => x).ToList();
        var g = l.Select((x, j) => j == 0 ? (x, l) : (_, l) = (x, l.Select((_, i) => l[(i + 1) % array.Length]).ToList()));
        return g.OrderBy(x => array.ToList().IndexOf(x.Item1, array.Count(w => w == x.Item1) > 1 ? k++: 0)).Select(x => String.Join("",x.Item2.Select((e, i) => e[i]))).ToArray();
    }
}

答案3:

using System.Linq;
using System;

public class Kata
{
  public static string[] DiagonalsOfSquare(string[] a)
  {
    if (a == null || a.Length == 0 || a[0].Length != a.Length) return null;
    
    var sorted = a.Select((x,i)=> new { OriginalI = i, S = x }).OrderBy(x => x.S).ToList();
    
    return sorted.Select( (d,i) => 
        new {
            Diag = string.Concat( d.S.Select( (_,j)=> sorted[(i+j)%a.Length].S[j] )),
            FinalIndex = d.OriginalI
        }).OrderBy(x => x.FinalIndex).Select( p => p.Diag).ToArray();
  }
}

答案4:

using System.Collections.Generic;
using System.Text;
using System.Linq;
public class Kata
{
        public static string[] DiagonalsOfSquare(string[] array)
        {
            if (array.Length <= 0 || array[0].Length <= 0 || array.Length != array[0].Length) return null;
            var list = array.Select((x, i) => (x, i)).OrderBy(x => x.x).ToList();
            list.AddRange(list);
            var dict = new Dictionary<int, string>();
            var n = array.Length;
            for (int i = 0; i < n; i++)
            {
                var builder = new StringBuilder();
                for (int j = 0; j < n; j++)
                    builder.Append(list[i + j].x[j]);
                dict.Add(list[i].i, builder.ToString());
            }
            var result = new List<string>();
            for (int i = 0; i < n; i++)
                result.Add(dict[i]);
            return result.ToArray();
        }
}

答案5:

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

public class Kata
{
    public static string[] DiagonalsOfSquare(string[] array)
    {
      if(!IsValidArray(array)) return null;
      
      var sortedArray = Sort(array);
      var originalCopy = new List<string>(array).ToArray();
      var swapMatrix = GetSwapMatrix(originalCopy, sortedArray);
      var diagonalArray = Diagonalize(sortedArray);
      return Unswap(diagonalArray, swapMatrix);
    }
    
    public static bool IsValidArray(string[] array)
    {
      if(array.Length == 0) return false;
      
      var targetLength = array.Length;
      foreach(var item in array)
        if(item.Length != targetLength) return false;
      
      return true;
    }
    
    public static string[] Sort(string[] array)
    {
      return array.OrderBy(s => s).ToArray();
    }
    
    public static int[] GetSwapMatrix(string[] original, string[] swapped)
    {
      var result = new int[original.Length];
      for(int i = 0; i < original.Length; ++i)
      {
        var target = swapped[i];
        result[i] = IndexOf(target, original);
        original[result[i]] = "";
      }
      
      return result;
    }
    
    public static int IndexOf(string target, string[] array)
    {
      for(int i = 0; i <  array.Length; ++i)
      {
        if(array[i] == target) return i;
      }
      return -1;
    }
    
    public static string[] Diagonalize(string[] array)
    {
      var result = new string[array.Length];
      for(int i = 0; i < array.Length; ++i)
      {
        var sb = new StringBuilder();
        for(int j = 0; j < array[i].Length; ++j)
        {
          var nextArrayIndex = (j + i) % array.Length;
          sb.Append(array[nextArrayIndex][j]);
        }
        result[i] = sb.ToString();
      }
      return result;
    }
    
    public static string[] Unswap(string[] array, int[] swapMatrix)
    {
      var result = new string[array.Length];
      for(int i = 0; i < array.Length; ++i)
      {
        result[swapMatrix[i]] = array[i];
      }
      return result;
    }
}

答案6:

using System.Linq;

public class Kata
{
    public static string[] DiagonalsOfSquare(string[] array)
    {
        // Throw out invalid input
        if (array == null || array.Length == 0 || array.Any(s => s.Length != array.Length)) return null;

        var n = array.Length;
        var sorted = array.Select((s, i) => new { s, i }).OrderBy(a => a.s);
        var diagonals = Enumerable.Range(0, n).Select(x => new { s = string.Concat(Enumerable.Range(0, n).Select(y => sorted.ElementAt((x + y) % n).s[y])), sorted.ElementAt(x).i });
        return diagonals.OrderBy(a => a.i).Select(a => a.s).ToArray();
    }
}

答案7:

using System.Collections.Generic;

public class Kata
{
    public static string[] DiagonalsOfSquare(string[] array)
    {
        if (array == null || array.Length == 0)
            return null;
        int N = array.Length;
        for (int i = 0; i < N; i++)
            if (array[i].Length != N)
                return null;
        List<string> strList = new List<string>(array);
        strList.Sort();

        Dictionary<string, string> resultDict = new Dictionary<string, string>();
        int index = 0;
        for (int k = 0; k < N; k++)
        {
            string s = "";
            for (int i = 0, j = 0; i < N; i++, j++)
                s += strList[i][j];
            string temp = strList[0];
            if (!resultDict.ContainsKey(temp))
                resultDict.Add(temp, s);
            else
            {                    
                string valTemp = resultDict[temp];
                resultDict.Remove(temp);
                resultDict.Add(temp + index.ToString(), valTemp);
                index++;
                resultDict.Add(temp + index.ToString(), s);
                index++;
            }
            strList.RemoveAt(0);
            strList.Add(temp);
        }

        string[] resultArr = new string[N];
        index = 0;
        for (int i = 0; i < N; i++)
        {
            if (resultDict.ContainsKey(array[i]))
                resultArr[i] = resultDict[array[i]];
            else
            {
                resultArr[i] = resultDict[array[i] + index.ToString()];
                index++;
            }
        }

        return resultArr;
    }
}

答案8:

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

public class Kata
{
    public static string[] DiagonalsOfSquare(string[] array)
    {
       if (array.Length == 0) return null;

            var list = new List<Tuple<int, string>>();

            for (int i = 0; i < array.Length; i++)
            {
                list.Add(new Tuple<int, string>(i, array[i]));

                if (array[i].Length != array.Length) return null;
            }

            list = list.OrderBy(l => l.Item2).ToList();

            var result = new List<Tuple<int, string>>();

            for (int i = 0; i < array.Length; i++)
            {
                var wx = "";                

                for (int j = 0; j < array.Length; j++)
                {
                    var sum = i + j;
                    var ix = sum >= array.Length ? sum - array.Length : sum;

                    wx += list[ix].Item2[j];
                }

                result.Add(new Tuple<int, string>(list[i].Item1, wx));
            }

            return result
                .OrderBy(x => x.Item1)
                .Select(c => c.Item2)
                .ToArray();
    }
}

答案9:

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

public class Kata {
    public static string[] DiagonalsOfSquare( string[] array ) {
        if ( array == null || array.Length == 0 ) {
            return null;
        }
        if ( array.Any( s => s.Length != array.Length ) ) {
            return null;
        }
        var sorted = array.OrderBy( _ => _ ).ToList( );
        var a = array.ToArray( );
        var result = new string[array.Length];
        for ( int i = 0; i < sorted.Count; i++ ) {
            var f = sorted [ 0 ];
            var j = Array.IndexOf( a, f );
            result [ j ] = GetDiagonal( sorted );
            a [ j ] = null;
            sorted.RemoveAt( 0 );
            sorted.Add( f );
        }
        return result.ToArray( );
    }

    private static string GetDiagonal( List<string> array ) {
        var sb = new StringBuilder( array.Count );
        for ( int i = 0; i < array.Count; i++ ) {
            for ( int j = 0; j < array.Count; j++ ) {
                if ( i == j ) {
                    sb.Append( array [ i ] [ j ] );
                }
            }
        }
        return sb.ToString( );
    }

}

答案10:

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

public class Temp
{
    public int A;
    public string B;
    public string C;

    public Temp(int A, string B, string C)
    {
        this.A = A;
        this.B = B;
        this.C = C;
    }
}
public class Kata
{
    public static string[] DiagonalsOfSquare(string[] array)
    {
        if(array == null || array.Length == 0)
          return null;
        
        var tupelList = new List<Temp>();
        for(int i=0; i<array.Length; i++)
        {
            if(array[i].Length != array.Length)
              return null;
            tupelList.Add(new Temp(i, array[i], string.Empty));
        }
        tupelList.Sort((x, y) =>
        {
            var isDigitX = Char.IsDigit(x.B[0]);
            var isDigitY = Char.IsDigit(y.B[0]);
                            
            if (isDigitX &amp;&amp; !isDigitY)
                return 1;
            else if (!isDigitX &amp;&amp; isDigitY)
                return -1;
            else
                return String.Compare(x.B, y.B);
        });
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < array.Length; i++)
        {
            for (int j = 0; j < array.Length; j++)
            {
                sb.Append(tupelList.ElementAt((j + i) % array.Length).B[j]);
            }
            tupelList.ElementAt(i).C = sb.ToString();
            sb.Clear();
        }
        tupelList.Sort((x, y) => x.A - y.A);
        return tupelList.Select(x => x.C).ToArray();
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值