Advertisement
valiamaximova1

Delene i sortirane na chisla s komentari

Jan 23rd, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class ex2 {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner (System.in);
  7.  
  8.         System.out.printf("Input the number of elements : ");
  9.  
  10.         int N = scan.nextInt();
  11.         //дължината на масивите
  12.  
  13.  
  14.         int[] Arr1 = new int[N];
  15.         double[] Arr1a = new double[N];
  16.         int[] Arr2 = new int[N];
  17.         double[] Arr2a = new double[N];
  18.         double[] Arr3 = new double[N];
  19.         // привим 3 масива
  20.         //първите два масива веднъж ги дефинираме като int и
  21.         // втори път като double за да не може да се въвеждат десетични числа
  22.  
  23.        
  24.         System.out.println();
  25.  
  26.         for (int i = 0; i < N; i++) {
  27.             System.out.printf("Input element [%d] of Arr1: ", i); //въвеждаме първият елемент
  28.             Arr1[i] = scan.nextInt();
  29.             Arr1a[i] = Arr1[i]; // казваме, че стойностите от първия масив int и от първия double са еднакви
  30.         }
  31.         System.out.println();
  32.  
  33.         for (int j = 0; j < N; j++) {
  34.             System.out.printf("Input element [%d] of Arr2: ", j);
  35.             Arr2[j] = scan.nextInt();
  36.             Arr2a[j] = Arr2[j];// казваме, че стойностите от втория масив int и от втория double са еднакви
  37.         }
  38.  
  39.         for (int b = 0; b < N; b++) {
  40.             Arr3[b] = Arr1a[b] / Arr2a[b];
  41.             // казваме, че стойностите в третия масив са равни на стойностите от първия делено на стойностите от втория
  42.            
  43.         }
  44.        
  45.  
  46.         System.out.println();
  47.         System.out.println("Results before sort: " + Arrays.toString(Arr3)); // принтираме резултатите преди сортирането
  48.  
  49.         //insersion sort
  50.         for (int k = 1; k < N; k++) {
  51.             double key = Arr3[k];
  52.             int a = k - 1;
  53.  
  54.             while ((a >= 0) && (Arr3[a] > key)) {
  55.                 Arr3[a + 1] = Arr3[a];
  56.                 a--;
  57.             }
  58.  
  59.             Arr3[a + 1] = key;
  60.         }
  61.         System.out.print("Results after sort: " + Arrays.toString(Arr3)); //принтираме резултата след сортирането
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement