Advertisement
roronoa

tri par variance

Sep 4th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4.  
  5. class Solution {
  6.  
  7.     public static void main(String args[])
  8.     {
  9.         Scanner in = new Scanner(System.in);
  10.         Map<Integer, Double> map = new HashMap<>();
  11.         int N = in.nextInt();
  12.         int M = in.nextInt();
  13.         for (int i = 0; i < N; i++) {
  14.             int[] tab = new int[M];
  15.             for (int j = 0; j < M; j++) {
  16.                 tab[j] = in.nextInt();
  17.                
  18.             }
  19.             map.put(i+1,variance(tab));
  20.         }
  21.         double min = -1;
  22.         int indiceMin = 1;
  23.         for(int x : map.keySet())
  24.         {
  25.             double y = map.get(x);
  26.             if(min == -1)
  27.                 min = y;
  28.             else
  29.             {
  30.                 if(y < min)
  31.                 {
  32.                     min = y;
  33.                     indiceMin = x;
  34.                 }
  35.             }
  36.         }
  37.         System.out.println(indiceMin);
  38.     }
  39.     public static double variance(int[] tab)
  40.     {
  41.         double moy = 0.0;
  42.         for(int x : tab)
  43.             moy += x;
  44.         moy /= tab.length;
  45.         double variance = 0.0;
  46.         for(int x : tab)
  47.         {
  48.             double y = Math.pow(x-moy,2);
  49.             variance += y;
  50.         }
  51.         return variance;
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement