描述:
将一个整数数组的所有正数统计,负数相加,返回一个数组。
例如:
输入 int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15}
返回 int[] {10, -65}.
MyCode:
using System;
using System.Collections.Generic;
using System.Linq;
public class Kata
{
public static int[] CountPositivesSumNegatives(int[] input)
{
if(input == null || !input.Any())//判断输入的数组是否为空
{
return new int[] {};
}
int sum1 = 0,sum2 = 0;//声明两个变量,sum1统计正数,sum2求负数和
for(int i = 0;i < input.Length;i++)
{
if(input[i] > 0)
sum1 ++;
else
sum2 += input[i];
}
int[] retInt = new int[]{sum1,sum2};
return retInt;
}
}
CodeWar:
using System;
using System.Linq;
public class Kata
{
public static int[] CountPositivesSumNegatives(int[] input)
{
return (input == null || input.Length ==0) ? new int[0] : new int[] { input.Count(o => o > 0), input.Where(o => o < 0).Sum() };
}
}