Matt23

Matura 2015 zadanie 4

May 16th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4.  
  5. public class Zadanie
  6. {
  7.     public static int moreZerosThanOnes = 0;
  8.     public static int divisibleBy2 = 0;
  9.     public static int divisibleBy8 = 0;
  10.     public static String theGreatestNumber = "0";
  11.     public static String theSmallestNumber = "100000000000000000000000";
  12.     public static int rowOfTheGreatestNumber = 0;
  13.     public static int rowOfTheSmallestNumber = 0;
  14.  
  15.     public static void main(String[] args) throws IOException
  16.     {
  17.         FileReader file = new FileReader("path\to\the\text\file");
  18.         BufferedReader reader = new BufferedReader(file);
  19.         String line = reader.readLine();
  20.  
  21.         int row = 1;
  22.         while (line != null)
  23.         {
  24.             hasMoreZerosThanOnes(line);
  25.             isDivisibleBy2or8(line);
  26.             findExtremeNumbers(line, row);
  27.  
  28.             line = reader.readLine();
  29.             row++;
  30.         }
  31.         reader.close();
  32.  
  33.         System.out.println("Liczb gdzie więcej jest zer niz jedynek: " + moreZerosThanOnes);
  34.         System.out.println("Liczb podzielnych przez 2: " + divisibleBy2);
  35.         System.out.println("Liczb podzielnych przez 8: " + divisibleBy8);
  36.         System.out.println("Numer wiersza z największą liczbą: " + rowOfTheGreatestNumber);
  37.         System.out.println("Numer wiersza z najmniejszą liczbą: " + rowOfTheSmallestNumber);
  38.     }
  39.  
  40.     public static void hasMoreZerosThanOnes(String line)
  41.     {
  42.         int replaced = line.replace("1", "").length();
  43.         if (2 * replaced > line.length()) moreZerosThanOnes++;
  44.     }
  45.  
  46.     public static void isDivisibleBy2or8(String line)
  47.     {
  48.         if (line.endsWith("0")) divisibleBy2++;
  49.         if (line.endsWith("000")) divisibleBy8++;
  50.     }
  51.  
  52.     public static void findExtremeNumbers(String line, int row)
  53.     {
  54.         if (line.length() > theGreatestNumber.length())
  55.         {
  56.             theGreatestNumber = line;
  57.             rowOfTheGreatestNumber = row;
  58.         }
  59.         else if (line.length() == theGreatestNumber.length())
  60.         {
  61.             if (line.compareTo(theGreatestNumber) == 1)
  62.             {
  63.                 theGreatestNumber = line;
  64.                 rowOfTheGreatestNumber = row;
  65.             }
  66.         }
  67.  
  68.         if (line.length() < theSmallestNumber.length())
  69.         {
  70.             theSmallestNumber = line;
  71.             rowOfTheSmallestNumber = row;
  72.         }
  73.         else if (line.length() == theSmallestNumber.length())
  74.         {
  75.             if (line.compareTo(theSmallestNumber) == -1)
  76.             {
  77.                 theSmallestNumber = line;
  78.                 rowOfTheSmallestNumber = row;
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment