Advertisement
Azazavr

какой элемент встречается в массиве чаще всего

Feb 27th, 2015
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. /*Создайте массив из 11 случайных целых чисел из отрезка [-1;1],
  2.  выведите массив на экран в строку. Определите какой элемент встречается в массиве
  3.  чаще всего и выведите об этом сообщение на экран. Если два каких-то элемента встречаются
  4.  одинаковое количество раз, то не выводите ничего.
  5.  */
  6. public class MaxElementMassiv {
  7.  
  8.     public static void main(String[] args) {
  9.         int m[] = new int[11];
  10.         int a = 0, b = 0, c = 0;
  11.  
  12.         for (int i = 0; i < m.length; i++) {
  13.             m[i] = (int) ((Math.random() * 4) - 2); //(0.9*1число)-2число || (0*1число)-2число
  14.         }
  15.         for (int i = 0; i < m.length; i++) {
  16.             System.out.print(m[i] + " ");
  17.             if (m[i] == 1) {
  18.                 a += 1;
  19.             } else if (m[i] == 0) {
  20.                 b += 1;
  21.             } else if (m[i] == -1) {
  22.                 c += 1;
  23.             }
  24.         }
  25.         System.out.println();
  26.         System.out.println(a + " " + b + " " + c);
  27.  
  28.         if (a > b && a > c)
  29.             System.out.println("Больше всего 1");
  30.         else if (b > a && b > c)
  31.             System.out.println("Больше всего 0");
  32.         else if (c > a && c > b)
  33.             System.out.println("Больше всего -1");
  34.         else
  35.             System.out.println(" нет доминирующего значения");
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement