카타 8급 Count of positives / sum of negatives

Jmnote bot (토론 | 기여)님의 2019년 2월 3일 (일) 04:03 판 (봇: 자동으로 텍스트 교체 (-분류:PHP +))

1 개요

카타 8급 C
# 🔗 문제 풀이

틀:카타 8급-29

2 C++

#include <vector>

std::vector<int> countPositivesSumNegatives(std::vector<int> input)
{
    if(input.empty()) return {};
    int posCnt=0, negSum=0;
    for(int x: input)
      x>0 ? posCnt++ : negSum+=x;
    return {posCnt, negSum};
}
#include <vector>

std::vector<int> countPositivesSumNegatives(std::vector<int> input)
{
    std::vector<int> res = {};
    if(input.empty()) return res;
    int posCnt=0, negSum=0;
    for(int num: input) {
      if(num>0) posCnt++;
      else negSum += num;
    }
    res.push_back(posCnt);
    res.push_back(negSum);
    return res;
}

3 PHP

function countPositivesSumNegatives($input) {
  if (empty($input)) return [];
  $posCount = $negSum = 0;
  foreach($input as $value) {
    if ($value > 0) $posCount++;
    else $negSum += $value;
  }
  return [$posCount, $negSum];
}
function countPositivesSumNegatives($input) {
  $count = count($input);
  if( $count < 1 ) return [];
  $posCount = 0;
  $negSum = 0;
  for($i=0; $i<$count; $i++) {
    if( $input[$i] > 0 ) $posCount++;
    else $negSum += $input[$i];
  }
  return [$posCount, $negSum];
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}