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

数字对【难度:1级】:

答案1:

using System;
using System.Linq;
public class Kata {
  public static int [] getLargerNumbers(int[] a, int[] b) {
    return a.Zip(b, (x, y) => Math.Max(x, y)).ToArray();
  }
}​

答案2:

using System;
using System.Linq;

public class Kata
{
  public static int [] getLargerNumbers(int [] a , int [] b)
    => a.Zip(b, (x,y) => Math.Max(x,y)).ToArray();
}​

答案3:

using System;
using System.Collections.Generic;
public class Kata
{
  public static int [] getLargerNumbers(int [] a , int [] b)
  {
    List<int> tmpArray=new List<int>();
    for(int i=0;i<a.Length;i++) 
    {
      tmpArray.Add(a[i]>=b[i]?a[i]:b[i]);
    }
    return tmpArray.ToArray();
  }
}​

答案4:

using System;

public class Kata
{
  public static int [] getLargerNumbers(int [] a , int [] b)
  {
    int [] newArray = new int [a.Length];
    for(var arrayIndex = 0; arrayIndex < a.Length; arrayIndex++)
    {
      int largerNumber = a[arrayIndex] > b[arrayIndex] ? a[arrayIndex] : b[arrayIndex];
      newArray[arrayIndex] = largerNumber;
    }
    return newArray;
  }
}​

答案5:

using System;

public class Kata
{
  // Takes two integer arrays.
  // Returns an array consisting of the largest values among the two integer arrays.
  public static int [] getLargerNumbers(int [] a , int [] b)
  {
 
    // throw error if parameters are of different sizes
    if ( a.Length != b.Length ){
      throw new IndexOutOfRangeException();
    }
    
    int len = a.Length - 1;
    
    int[] newArray = new int[ len+1 ];
    
    for( int i = 0; i <= len; ++i ){
      if ( a[i] > b[i] ){
        newArray[i] = a[i];
      } else {
        newArray[i] = b[i];
      }
    }
  
    return newArray;
  }
}​

答案6:

using System;
using System.Linq;

public class Kata
{
  public static int[] getLargerNumbers(int[] a , int[] b)
  {
    return a.Zip(b, Math.Max).ToArray();
  }
}​

答案7:

using System;
using System.Linq;

public class Kata
{
  public static int [] getLargerNumbers(int [] a , int [] b) =>
                        a.Select((x, i) => Math.Max(x, b[i])).ToArray();
}​

答案8:

using System;

public class Kata
{
  public static int [] getLargerNumbers(int [] a , int [] b)
  {
        int[] newArray = new int[a.Length];
        for (int x = 0; x < a.Length; x++) {
          if (a[x] >= b[x])
          newArray[x] = a[x];

          if (b[x] > a[x])
            newArray[x] = b[x];
          }
    return newArray;
  }
}​

答案9:

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

public class Kata
{
  public static int [] getLargerNumbers(int [] a , int [] b)
  {
    var newArray = a.Zip(b, (first, second) => first > second ? first : second);
    return newArray.ToArray();
  }
}​

答案10:

using System;

public class Kata
{
  public static int [] getLargerNumbers(int [] a , int [] b)
  {  
      for(int i = 0; i <= a.Length-1;i++)
      {
        if(a[i] < b[i])    
        {
          a[i] = b[i];
        }
      }      
      return a;        
  }
}​




上一篇:利用Java反射动态创建数组


下一篇:Silverlight 结合ArcGis 在地图上画线