일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- node.js
- 최강의 인생
- 블록체인
- MySQL
- Programmers
- array.push()
- 자바스크립트
- 코플릿
- Where
- 개발자의 책장
- SQL
- for문
- 코딩공부
- 정규표현식
- select
- 코딩테스트
- JavaScript
- 개발자_조이킴
- Algorithms
- join
- array
- Developer_JoyKim
- 코드스테이츠
- 역행자
- array.slice()
- 배열
- 재귀함수
- 알고리즘
- Hackerrank
- 프로그래머스
Archives
- Today
- Total
CodingSpace
[HackerRank/Algorithms] Warmup - Plus Minus 본문
Problem. Warmup - Plus Minus
Link.
https://www.hackerrank.com/challenges/plus-minus/problem?isFullScreen=true
Description.
Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero.
Print the decimal value of each fraction on a new line with 6 places after the decimal.
Key Point.
My Answer.
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'plusMinus' function below.
*
* The function accepts INTEGER_ARRAY arr as parameter.
*/
function plusMinus(arr) {
// Write your code here
let len = arr.length;
let positive = 0;
let negative = 0;
let zero = 0
for(let i = 0; i < len; i++) {
if(arr[i] > 0)
positive = positive + 1;
else if(arr[i] < 0)
negative = negative + 1;
else if(arr[i] === 0)
zero = zero + 1;
}
positive = (positive / len).toFixed(6);
negative = (negative / len).toFixed(6);
zero = (zero / len).toFixed(6);
console.log(positive);
console.log(negative);
console.log(zero);
}
function main() {
const n = parseInt(readLine().trim(), 10);
const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
plusMinus(arr);
}
References.
'HackerRank > Algorithm' 카테고리의 다른 글
[HackerRank/Algorithms] Warmup - Birthday Cake Candles (0) | 2022.08.22 |
---|---|
[HackerRank/Algorithms] Warmup - Mini-Max Sum (0) | 2022.08.22 |
[HackerRank/Algorithms] Warmup - Diagonal Difference (0) | 2022.08.21 |
[HackerRank/Algorithms] Warmup - A Very Big Sum (0) | 2022.08.19 |
[HackerRank/Algorithms] Warmup - Solve Me First (0) | 2022.08.18 |
Comments