My first 10 out of 10!
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String numArgsStr = scanner.nextLine();
double numArgs = Double.parseDouble(numArgsStr);
String integers = scanner.nextLine();
String[] integersArray = integers.split(" ");
double pos = 0.0;
double zero = 0.0;
double neg = 0.0;
for (int i = 0; i < integersArray.length; i++) {
int j = Integer.parseInt(integersArray[i]);
if(j < 0) {
neg++; } else if(j == 0) {
zero++; } else {
pos++; }
}
System.out.println(printDiv(pos, numArgs));
System.out.println(printDiv(neg , numArgs));
System.out.println(printDiv(zero, numArgs));
}
static String printDiv(double num, double den) {
if(Double.toString(num / den).length() > 8)
return Double.toString(num / den).substring(0, 8);
else
return String.format("%-8s", Double.toString(num / den)).replace(' ', '0');
}
}
No comments:
Post a Comment