C#练习题答案: 行权重【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战
作者:互联网
行权重【难度:1级】:
答案1:
using System.Linq;
class Kata
{
public static int[] RowWeights(int[] a)
{
return new int[] { a.Select((x, y) => y % 2 == 0 ? x : 0).Sum(), a.Select((x, y) => y % 2 == 0 ? 0 : x).Sum() };
}
}
答案2:
class Kata
{
public static int[] RowWeights(int[] a)
{
int[] ret = new int[2];
for(int i = 0; i < a.Length; i++)
{
if(i % 2 == 0)
{
ret[0] += a[i];
}
else
{
ret[1] += a[i];
}
}
return ret;
}
}
答案3:
using System.Linq;
class Kata
{
public static int[] RowWeights(int[] a)
{
return new int[]{a.Where((x,i) => i % 2 == 0).Sum(), a.Where((x,i) => i % 2 != 0).Sum()};
}
}
答案4:
using System.Collections.Generic;
using System.Linq;
class Kata
{
public static int[] RowWeights(int[] numbers)
{
return new int[]
{
numbers.Where((n, index) => index % 2 == 0).Sum(),
numbers.Where((n, index) => index % 2 == 1).Sum()
};
}
}
答案5:
class Kata
{
public static int[] RowWeights(int[] a)
{
int[] result = new int[2];
for (int i = 0; i < a.Length; i++)
{
if (i % 2 == 0)
result[0] += a[i];
else
result[1] += a[i];
}
return result;
}
}
答案6:
class Kata
{
public static int[] RowWeights(int[] a)
{
int[] result = new int[2];
for(int i = 0; i < a.Length; i++)
{
if (i % 2==0)
{
result[0] += a[i];
}
else
result[1] += a[i];
}
return result;
}
}
答案7:
class Kata
{
public static int[] RowWeights(int[] a)
{
int weightTeam1 = 0;
int weightTeam2 = 0;
for (int i = 0; i < a.Length; i++)
{
if (i % 2 == 0)
{
weightTeam1 += a[i];
}
else
{
weightTeam2 += a[i];
}
}
return new int[] { weightTeam1, weightTeam2 };
}
}
答案8:
using System.Linq;
public class Kata
{
public static int[] RowWeights(int[] a) =>
a.Length == 1 ? new int[] { a[0], 0 } :
new int[] {a.Select((x,i)=> i%2==0 ? x : 0).Sum(), a.Select((x,i)=>i%2==1 ? x : 0).Sum()};
}
答案9:
class Kata
{
public static int[] RowWeights(int[] a)
{
var result = new int[2];
for(int i = 0; i < a.Length; i++)
{
if(i % 2 == 0)
result[0] += a[i];
else
result[1] += a[i];
}
return result;
}
}
答案10:
class Kata
{
public static int[] RowWeights(int[] a)
{
int weightTeam1 = 0;
int weightTeam2 = 0;
for (int i=0; i < a.Length; i++)
{
if (i % 2 == 0)
weightTeam1 += a[i];
else
weightTeam2 += a[i];
}
return new int[2] {weightTeam1, weightTeam2};
}
}
答案11:
using System;
class Kata
{
public static int[] RowWeights(int[] a)
{
int b = 0, c = 0;
for (int i = 0; i <= a.Length - 1; ++i)
{
if(i % 2 == 0)
{
b += a[i];
}
else
{
c += a[i];
}
}
return new int[2]{b, c};
}
}
标签:练习题,景越,C#,RowWeights,int,static,result,new,public 来源: https://blog.csdn.net/weixin_45444821/article/details/100750737