Advertisement
Guest User

Test1Seasоn5V1

a guest
Jun 27th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Test1Season05 {
  5.  
  6.     public static void main(String[] args) {
  7.        
  8.         Task01();
  9.        
  10.         Task02();
  11.        
  12.         Task03();
  13.        
  14.         Task04();
  15.        
  16.     }
  17.    
  18.     static void Task01() {
  19.        
  20.         System.out.println("------ TASK01 -----");
  21.        
  22.         Random rn = new Random();
  23.        
  24.         int heads = 0;
  25.        
  26.         int tries = 0;
  27.        
  28.         while (heads < 5) {
  29.            
  30.             ++tries;
  31.            
  32.             int number = rn.nextInt(2); // 0   Tails, 1   Heads
  33.            
  34.             if (number == 1) {
  35.                 ++heads;
  36.             } else {
  37.                 heads = 0;
  38.             }
  39.         }
  40.        
  41.         System.out.println("Number of tries: " + tries);
  42.     }
  43.  
  44.     static void Task02() {
  45.        
  46.         System.out.println("------ TASK02 -----");
  47.        
  48. //      Scanner sc = new Scanner(System.in);
  49. //     
  50. //      String str = sc.nextLine();
  51.  
  52.         String str = "Write a method that takes a String variable that holds some text The text contains words that are separated by a single space The method must return the most frequent word from the text";
  53.        
  54.         System.out.println(mostFrequentWord(str));
  55.     }
  56.    
  57.     static String mostFrequentWord(String str) {
  58.         String[] words = str.split(" ");
  59.        
  60.         int counter = 1;
  61.         int maxCounter = 1;
  62.        
  63.         String res = "";
  64.        
  65.         for (int i = 0; i < words.length; i++) {
  66.             counter = 1;
  67.            
  68.             for (int j = i + 1; j < words.length; j++) {
  69.                 if (words[i].equals(words[j])) {
  70.                     ++counter;
  71.                 }
  72.             }
  73.            
  74.             if (counter > maxCounter) {
  75.                 maxCounter = counter;
  76.                 res = words[i];
  77.             }
  78.         }
  79.        
  80.         return res;
  81.     }
  82.  
  83.     static void Task03() {
  84.        
  85.         System.out.println("------ TASK03 -----");
  86.        
  87.         int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  88.        
  89.         System.out.println(subtraction(arr, 1, arr[0], arr[0]));
  90.     }
  91.    
  92.     static int subtraction(int[] arr, int index, int min, int max) {
  93.         if (index == arr.length) {
  94.             return max - min;
  95.         }
  96.        
  97.         if (arr[index] < min) {
  98.             min = arr[index];
  99.         }
  100.        
  101.         if (arr[index] > max) {
  102.             max = arr[index];
  103.         }
  104.        
  105.        
  106.         return subtraction(arr, index + 1, min, max);
  107.     }
  108.  
  109.     static void Task04() {
  110.        
  111.         System.out.println("------ TASK04 -----");
  112.        
  113.         char[][] matrix = {
  114.                 { 'b', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
  115.                 { 'b', 'b', 'b', 'b', '-', '-', '-', '-', '-', '-', '-', '-' },
  116.                 { '-', '-', '-', 'b', 'b', '-', '-', '-', '-', 'b', 'b', 'b' },
  117.                 { '-', '-', '-', '-', 'b', 'b', '-', '-', 'b', 'b', '-', '-' },
  118.                 { '-', '-', '-', '-', '-', 'b', 'b', 'b', 'b', '-', '-', '-' },
  119.                 { '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
  120.                 { '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
  121.                 { '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
  122.                 { '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
  123.                 { '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
  124.                 { '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
  125.                 { '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-' },
  126.             };
  127.        
  128.         int x, y;
  129.         x = 1;
  130.         y = 6;
  131.        
  132.         matrix[x][y] = 'x';
  133.        
  134.         gas(matrix, x, y);
  135.        
  136.         printMatrix(matrix);
  137.        
  138.     }
  139.    
  140.     static void gas(char[][] matrix, int row, int col) {
  141.        
  142.         if (matrix[row][col] != 'x') {
  143.             matrix[row][col] = 'r';        
  144.         }
  145.        
  146.         for (int i = -1; i < 2; i++) {
  147.             for (int j = -1; j < 2; j++) {
  148.                     if (row + i >= 0 && row + i < matrix.length &&
  149.                             col + j >= 0 && col + j < matrix[0].length &&
  150.                             matrix[row + i][col + j] == '-') {
  151.                        
  152.                         gas(matrix, row + i, col + j);
  153.                     }
  154.             }
  155.         }
  156.     }
  157.    
  158.     static void printMatrix(char[][] matrix) {
  159.         for (int i = 0; i < matrix.length; i++) {
  160.             for (int j = 0; j < matrix[i].length; j++) {
  161.                 System.out.print(matrix[i][j] + " ");
  162.             }
  163.            
  164.             System.out.println();
  165.         }
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement