Advertisement
Uimin_Maxim

Задача 3

Jan 27th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. //Уймин Максим
  2. //IT School, 2 группа
  3. //09.10.2015
  4. //Задача 3. Найти самое часто повторяющееся значение среди элементов массива
  5. import java.io.PrintStream;
  6. import java.util.Scanner;
  7. public class Exersise_3 {
  8.     public static PrintStream out = System.out;
  9.     public static Scanner scan = new Scanner(System.in);
  10.     public static int[] CreateArray (int N){
  11.         //Функция создаёт массив и заполняет его случайными числами
  12.         int[]array = new int[N];
  13.         out.println("Original array:");
  14.         for (int i=0;i<N;i++){
  15.             array[i]=(-10) + (int)(Math.random()*((10-(-10))+1));
  16.             out.print(array[i]+" ");
  17.         }
  18.         out.println();
  19.         return array;
  20.     }
  21.     public static void CountElements (int[] array,int N){
  22.         int[]k = new int[N];
  23.         for (int i=0;i<N;i++){
  24.             k[i]=0;
  25.             for (int j=0;j<N;j++){
  26.                 if (array[i] == array[j]) k[i] = k[i]+1;
  27.             }
  28.         }
  29.         int max=-2147483648;
  30.         int m=0;
  31.         for (int i=0;i<N;i++){
  32.             if (k[i]>max){ max = k[i]; m=array[i];}
  33.         }
  34.         out.println("Required number is "+m);
  35.     }
  36.     public static void MainFunction (){
  37.         //Здесь собран весь функционал программы
  38.         out.println("Enter the array size(intger number, greather then zero)");
  39.         int N = scan.nextInt();
  40.         int[] array=new int [N];
  41.         array = CreateArray(N);
  42.         CountElements(array, N);
  43.     }
  44.     public static void main (String [] args){
  45.         //Основная функция, отсюда начинается выполнение приложения
  46.         MainFunction();
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement