C#练习题答案: 计数阳性/阴性的总和【难度:0级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

计数阳性/阴性的总和【难度:0级】:

答案1:

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 countPositives = input.Count(n => n > 0);
        int sumNegatives = input.Where(n => n < 0).Sum();

        return new int[] { countPositives, sumNegatives };
    }
}

答案2:

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 countPositives = input.Count(n => n > 0);
        int sumNegatives = input.Where(n => n < 0).Sum();

        return new int[] { countPositives, sumNegatives };

       return null; //return an array with count of positives and sum of negatives
    }
}

答案3:

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() };
    }
}

答案4:

using System;
using System.Collections.Generic;
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() };
      }
}

答案5:

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

public class Kata
{
    public static int[] CountPositivesSumNegatives(int[] input)
    {
        int c = 0;
        int s = 0;
        if (input == null || input.Length == 0){
          return new int[]{};
        }
        for (int i = 0; i < input.Length; i++){  
          if (input[i] > 0){
            c += 1;
          }
          if (input[i] < 0){
            s += input[i];
          }
        }
        return new int[]{c, s};
        }
}

答案6:

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

public class Kata
{
    public static int[] CountPositivesSumNegatives(int[] input)
    {
        if (input == null || input.Length == 0) 
        {
            return new int[0];
        }
        
        return input.Aggregate(new int[] {0, 0}, (acc, x) => 
        {
            if (x > 0) 
            {
                acc[0]++;
            }
            else if (x < 0) 
            {
                acc[1] += x;
            }
            
            return acc;
        });
    }
}

答案7:

using System;
using System.Linq;

public class Kata
{
    private static readonly int[] Empty = new int[0];
    
    public static int[] CountPositivesSumNegatives(int[] input)
    {
        if (input == null || input.Length == 0)
        {
            return Empty;
        }
        
        return new int[]
        {
            input.Count(x => x > 0),
            input.Sum(x => Math.Min(0, x)),
        };
    }
}

答案8:

using System;
using System.Linq;

public class Kata
{
    private static readonly int[] Empty = new int[0];
    
    public static int[] CountPositivesSumNegatives(int[] input)
    {
        if (input == null || input.Length == 0)
        {
            return Empty;
        }
        
        int count = 0;
        int sum = 0;
        
        foreach (var element in input)
        {
            if (element > 0)
            {
                count += 1;
            }
            else if (element < 0)
            {
                sum += element;
            }
        }
        
        return new int[] { count, sum };
    }
}

答案9:

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

public class Kata
{
    public static int[] CountPositivesSumNegatives(int[] input)
    {
        if((input == null) || (input.Length == 0))
        {
          return new int[0];
        }
        
        return new[] { input.Where(i => i > 0).Count(), input.Where(i => i<0).Sum() };
    }
}

答案10:

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

public class Kata
{
    public static int[] CountPositivesSumNegatives(int[] input)
    {
        return (input == null || !input.Any()) ? new int[]{} :
        new int[] {input.Where(x => x > 0).Count(), input.Where(x => x < 0).Sum()};
    }
}

答案11:

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

public class Kata
{
    public static int[] CountPositivesSumNegatives(int[] input)
    {
        if (input == null || input.Length == 0) return new int[] {};
        
        int positiveCount = input.Where(n => n > 0).Count();
        int negativeSum = input.Where(n => n < 0).Sum();

        return new int[] {positiveCount, negativeSum};
    }
}

答案12:

using System.Collections.Generic;

public class Kata
{
    public static int[] CountPositivesSumNegatives(int[] input)
    {
        if (input == null || input.Length == 0)
        {
            return new int[0];
        }

        int positeve = 0;
        int negative = 0;

        foreach (var t in input)
        {
            if (t > 0)
            {
                positeve++;
            }
            else
            {
                negative += t;
            }
        }

        List<int> test = new List<int>();

        test.Add(positeve);
        test.Add(negative);

        return test.ToArray();
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值