-
Notifications
You must be signed in to change notification settings - Fork 0
Home
tofti666 edited this page Mar 2, 2017
·
1 revision
Day 0 : statistics
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int nbInput = sc.nextInt();
List<Integer> liste = new ArrayList<Integer>();
for(int i=0; i<nbInput; i++){
int val = sc.nextInt();
Integer val2 = Integer.valueOf(val);
liste.add(val);
}
Collections.sort(liste);
//System.out.println(liste);
double mean = getMean(liste, nbInput);
double median = getMedian(liste, nbInput);
int mode = getMode(liste, nbInput);
System.out.println(mean);
System.out.println(median);
System.out.println(mode);
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
}
public static double getMean(List<Integer> liste, int nbInput){
double total = 0;
for(final Integer i : liste){
total += i;
}
total = total/nbInput;
return total;
}
public static double getMedian(List<Integer> liste, int nbInput){
double median = 0;
if(liste != null){
int tailleListe = liste.size();
if(tailleListe % 2 == 0){
median += liste.get(tailleListe/2);
median += liste.get(tailleListe/2 - 1);
median = median/2;
}else{
median = liste.get((tailleListe+1)/2);
}
}
return median;
}
public static int getMode(List<Integer> liste, int nbInput){
int mode = 0;
int lastIndex = 0;
int lastOcc = 1;
if(liste!=null){
mode = liste.get(0);
for(int i=0; i<liste.size(); i++){
int occurrences = Collections.frequency(liste, liste.get(i));
if(occurrences > lastOcc){
mode = liste.get(i);
lastOcc = i;
}
}
}
return mode;
}
}