Это легкое задание уровня сложности в HackerRank. Ниже приведена моя логика для тех, у кого могут возникнуть проблемы с ее выполнением.
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Remoting.Contexts; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { internal class Program { public void plusMinus(List<int> arr) { string stringFormat = "0.000000"; int arraySize = arr.Count; List<int> positives = new List<int>(); List<int> negatives = new List<int>(); List<int> zeros = new List<int>(); foreach (int i in arr) { if(i > 0){ positives.Add(i); } if(i < 0) { negatives.Add(i); } if(i == 0){ zeros.Add(i); } } double posCount = positives.Count; double negCount = negatives.Count; double zeroCount = zeros.Count; string posVal = Math.Round(posCount / arraySize,6).ToString(stringFormat); string negVal = Math.Round(negCount / arraySize,6).ToString(stringFormat); string zeroVal = Math.Round(zeroCount / arraySize,6).ToString(stringFormat); Console.WriteLine(posVal); Console.WriteLine(negVal); Console.WriteLine(zeroVal); } static void Main(string[] args) { Program program = new Program(); int[] contents = { 1, 1, 1, -1, -1 }; List<int> array = new List<int>(contents); program.plusMinus(array); } } }
Если у вас есть какие-либо вопросы, я буду рад помочь. Спасибо.