Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package zad4tablice;
  7.  
  8. import java.util.Random;
  9. import java.util.Scanner;
  10.  
  11. /**
  12.  *
  13.  * @author Shineko
  14.  */
  15. public class Zad4tablice {
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     public static void main(String[] args)
  21.     {
  22.         Scanner in = new Scanner(System.in);
  23.         Random r = new Random();
  24.        
  25.         int wymiar;
  26.        
  27.         do
  28.         {
  29.         //System.out.println(wymiar);
  30.        
  31.         wymiar = (r.nextInt(51)) + 50;
  32.        
  33.         }
  34.         while(wymiar%4 != 0);
  35.         //System.out.println(wymiar);
  36.        
  37.         int tab [][] = new int [wymiar][wymiar];
  38.        
  39.         System.out.println("Podaj początek zakresu losowań:");
  40.         System.out.print("> ");
  41.        
  42.         int a = in.nextInt();
  43.        
  44.         System.out.println("Podaj koniec zakresu losowań:");
  45.         System.out.print("> ");
  46.        
  47.         int b = in.nextInt();
  48.        
  49.         if(b <= a)
  50.         {
  51.         do
  52.         {
  53.             System.out.println("Nieprawdiłowa wartość. Koniec musi być większy od początku.");
  54.             System.out.println("Podaj koniec zakresu losowań:");
  55.             System.out.print("> ");
  56.        
  57.             b = in.nextInt();
  58.         }
  59.         while( b <= a);
  60.         }
  61.        
  62.         System.out.println("A: " + a + ", B: " + b);
  63.        
  64.         int minus = wymiar / 4;
  65.         int plus = 3 * minus;
  66.        
  67.                
  68.         for(int i = 0; i < wymiar; i++)
  69.         {
  70.             for(int j = 0; j < wymiar; j++)
  71.             {
  72.                
  73.                 if( j != i)
  74.                 {
  75.                    
  76.                     tab [i][j] = (r.nextInt(b - a + 1) + b);
  77.                    
  78.                    
  79.                 }
  80.                 else
  81.                 {
  82.                
  83.                     Boolean c = r.nextBoolean();
  84.                    
  85.                     if( c == true)
  86.                     {
  87.                         plus--;
  88.                        
  89.                         if(plus < 0)
  90.                         {
  91.                             tab [i][j] = -1;
  92.                         }
  93.                         else
  94.                         {
  95.                             tab [i][j] = 1;
  96.                         }
  97.                        
  98.                     }
  99.                     else
  100.                     {
  101.                         minus--;
  102.                        
  103.                         if(minus < 0)
  104.                         {
  105.                             tab[i][j] = 1;
  106.                         }
  107.                         else
  108.                         {
  109.                             tab[i][j] = -1;
  110.                         }
  111.                        
  112.                        
  113.                     }
  114.                    
  115.                    
  116.                    
  117.                    
  118.                    
  119.                 }
  120.                        
  121.                
  122.                
  123.             }
  124.         }
  125.        
  126.         int liczKom = 0;
  127.        
  128.         for(int i = 0; i < wymiar; i++)
  129.         {
  130.             for(int j = 0; j < wymiar; j++)
  131.             {
  132.                
  133.                 if( tab [i][j] < i * j)
  134.                 {
  135.                     liczKom++;
  136.                 }
  137.            
  138.             }
  139.         }
  140.        
  141.        
  142.         System.out.println("Liczba komórek, których wartość jest mniejsza od iloczynu indeksu wiersza i kolumny tych komórek wynosi: " + liczKom);
  143.        
  144.        
  145.        
  146.        
  147.    
  148.    
  149.     }
  150.    
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement