[Lucky Algorithm] Plus Minus (4/77)
Plus Minus (Hacker Rank)Permalink
Given an array of integers, calculate which fraction of its elements are positive, which fraction of its elements are negative, and which fraction of its elements are zeroes, respectively. Print the decimal value of each fraction on a new line.
Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to are acceptable.
#!/bin/python3
import sys
n = int(input().strip())
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
pos_num, neg_num, zero_num = 0, 0, 0
print("%6f"%float(len([a for a in arr if a > 0])/n))
print("%6f"%float(len([a for a in arr if a < 0])/n))
print("%6f"%float(len([a for a in arr if a == 0])/n))
Java 풀이
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] arr = new int[5];
for(int arr_i=0; arr_i < 5; arr_i++){
arr[arr_i] = in.nextInt();
}
long min = arr[0], max = arr[0];
long sum = 0;
for(Integer i:arr) {
sum += i;
if(min >i) {
min = i;
}
if(max < i) {
max = i;
}
}
min = sum - min;
max = sum - max;
System.out.println(max +" "+ min);
}
}
행운의 77문제 프로젝트Permalink
행운의 77문제 프로젝트는 한 달동안 알고리즘 문제 77개를 푸는 프로젝트입니다.