Advertisement
n4wn4w

ARRAYS

Apr 7th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1.                 Scanner scanner = new Scanner(System.in);
  2.         String input = scanner.nextLine();
  3.         String[] elements = input.split(" ");
  4.         int[] nums = new int [elements.length];
  5.        
  6.         for (int i = 0; i < elements.length; i++) {
  7.             nums[i] = Integer.parseInt(elements[i]);
  8.         }
  9.             // input ////////////
  10.    
  11.                 int max = Integer.MIN_VALUE;
  12.         int min = Integer.MAX_VALUE;
  13.         long sum = 0;
  14.        
  15.        
  16.         for (int i = 0; i < nums.length; i++) {
  17.            
  18.             int num = nums[i];
  19.            
  20.            
  21.             if(num > max){
  22.                 max = num;
  23.             }
  24.            
  25.             if(num < min){
  26.                 min = num;
  27.             }
  28.            
  29.             sum += num;
  30.            
  31.         }
  32.            
  33.         double avg1 = (double)sum / nums.length;
  34.        
  35.         System.out.println(min);
  36.         System.out.println(max);
  37.         System.out.println(sum);
  38.         System.out.println(avg1);
  39.  
  40.  
  41. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42.  
  43. int[] numbers = new int[10];
  44.  
  45.         for (int i=0; i<numbers.length; i++) {
  46.             numbers[i] = i+1;
  47.         }
  48.        
  49.         numbers[3] = 20;
  50.         numbers[5] = numbers[2] + numbers[7];
  51.  
  52.         for (int i = 0; i<numbers.length; i++) {
  53.             System.out.printf("numbers[%d] = %d\n", i, numbers[i]);
  54.         }
  55.        
  56.         for (int num : numbers) {
  57.             System.out.println(num);
  58.         }
  59.        
  60.         String[] names = { "Peter", "Maria", "Katya", "Todor" };
  61.        
  62.         for (int i = 0; i<names.length; i++) {
  63.             System.out.printf("names[%d] = %s\n", i, names[i]);
  64.         }
  65.        
  66.         for (String name : names) {
  67.           System.out.println(name);
  68.         }
  69.  
  70.         names[4] = "Nakov"; // ArrayIndexOutOfBoundsException
  71.        
  72.         //names.length = 5; // array.length is read-only field
  73.  
  74. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  75. ////   sort LEYYERS
  76. // Read a number n and n text lines
  77.         Scanner scanner = new Scanner(System.in);
  78.         int n = scanner.nextInt();
  79.         scanner.nextLine();
  80.         String[] lines = new String[n];
  81.         for (int i = 0; i < n; i++) {
  82.             lines[i] = scanner.nextLine();
  83.         }
  84.        
  85.         // Sort the array of strings
  86.         Arrays.sort(lines);
  87.        
  88.         // Print the sorted array
  89.         for (int i = 0; i < lines.length; i++) {
  90.             System.out.println(lines[i]);
  91.         }
  92.  
  93. ////////////////////////////////////////////////////////////////////////////////////////////////////
  94.  
  95. // sort NUMBERS
  96.  
  97.                                 Scanner scanner = new Scanner(System.in);
  98.                 int n = scanner.nextInt();
  99.                 scanner.nextLine();
  100.                                 String[] lines = input.split(" ");
  101.  
  102.                 //String[] lines = new String[n];
  103.                 int[] numStr = new int [lines.length];
  104.  
  105.                 for (int i = 0; i < n; i++) {
  106.                 lines[i] = scanner.nextLine();
  107.                 }
  108.                
  109.                 for (int i = 0; i < lines.length; i++) {   
  110.                  numStr[i] = Integer.parseInt(lines[i]);
  111.                 }
  112.                
  113.                 // Sort the array of numbers
  114.                 Arrays.sort(numStr);
  115.                
  116.                 // Print the sorted array
  117.                 for (int i = 0; i < numStr.length; i++) {
  118.                     System.out.println(numStr[i]);
  119.                 }
  120.  
  121. ////////////////////  2  reshenie za sort a NUMBERS ////////////////////////////////////////////
  122.  
  123.     // Read a number n and n text lines
  124.         Scanner scanner = new Scanner(System.in);
  125.         String input = scanner.nextLine();
  126.         String[] lines = input.split(" ");
  127.         int[] numStr = new int [lines.length];
  128.  
  129. //      for (int i = 0; i < n; i++) {
  130. //      lines[i] = scanner.nextLine();
  131. //      }
  132.        
  133.         for (int i = 0; i < lines.length; i++) {   
  134.          numStr[i] = Integer.parseInt(lines[i]);
  135.         }
  136.        
  137.         // Sort the array of numbers
  138.         Arrays.sort(numStr);
  139.        
  140.         // Print the sorted array
  141.         for (int i = 0; i < numStr.length; i++) {
  142.             System.out.println(numStr[i]);
  143.         }
  144.  
  145.        
  146. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement