Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Random;
- public class Liczby {
- private int tab[] = new int[6];
- private int top;
- public Liczby()
- {
- top = 0;
- }
- private Boolean is_in_range( int x )
- {
- if( x > 0 && x < 50 )
- return true;
- else
- System.out.println("Niepoprawny zakres");
- return false;
- }
- private Boolean is_unique( int x )
- {
- for( int i = 0; i < top; i++ )
- {
- if( x == tab[i] )
- {
- System.out.println("Element juz istnieje");
- return false;
- }
- }
- return true;
- }
- public Boolean is_value( int x )
- {
- for( int i : tab )
- {
- if(i == x)
- {
- return true;
- }
- }
- return false;
- }
- public void push( int x )
- {
- if( is_in_range(x) && is_unique(x) )
- {
- tab[top] = x;
- top++;
- }
- else
- {
- System.out.println("Niepoprawna liczba");
- }
- }
- public int pop()
- {
- int x = tab[top];
- top--;
- return x;
- }
- public int get_top()
- {
- return top;
- }
- public void sort()
- {
- Arrays.sort(tab);
- }
- public void display()
- {
- sort();
- System.out.print("[ ");
- for( int i = 0; i < get_top(); i++ )
- {
- System.out.print(get_tab(i) + "\t");
- }
- System.out.print("]" );
- }
- public int get_tab(int i)
- {
- return tab[i];
- }
- public void rand()
- {
- Random R = new Random();
- while( top < 6 )
- {
- int x = R.nextInt(49) + 1;
- if( is_unique(x) )
- {
- tab[top] = x;
- top++;
- }
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////
- import java.util.Scanner;
- import java.util.Random;
- public class Lab03 {
- // Zadanie 1
- public static void PrintFib( int n )
- {
- int Fn = 0 , Fn1 = 1 , pom = 0;
- for(int i = 0; i <= n; i++)
- {
- System.out.println("F[" + i + "] = " + Fn);
- pom = Fn + Fn1;
- Fn = Fn1;
- Fn1 = pom;
- }
- }
- public static int fib(int n)
- {
- if (n == 0)
- return 0;
- else if( n == 1)
- return 1;
- else
- return fib(n-1)+fib(n-2);
- }
- //Zadanie 2
- public static int ToInt( String str )
- {
- str = str.trim();
- String x = "";
- for( int i = 0; i < str.length(); i++ )
- {
- char pom = str.charAt(i);
- if( Character.isDigit(pom) )
- {
- x += pom;
- }
- }
- if( x.isEmpty() )
- {
- return 0;
- }
- else
- {
- return Integer.parseInt(x);
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner read = new Scanner( System.in );
- /* Zadanie 1 - Fibbonacci
- System.out.println("Ile wyrazów ciągu wypisać?");
- System.out.print("n = ");
- int n = read.nextInt();
- System.out.println("Fibbonacci iteracyjnnie");
- PrintFib(n);
- System.out.println("\nFibbonacci rekurencyjnie");
- for(int i = 0; i < n + 1; i++)
- {
- System.out.println( "F[" + i + "] = " + fib(i) );
- }*/
- //Zadanie 2 - LOTTO
- Liczby L = new Liczby();
- System.out.println("Prosze podać 6 liczb");
- for(; L.get_top() < 6;)
- {
- System.out.print(( L.get_top()+1 ) + ": ");
- String s = read.next();
- L.push( ToInt(s) );
- }
- System.out.println("Losowanie 6 liczb");
- Liczby Los = new Liczby();
- Los.rand();
- L.display();
- System.out.println("<-- Twoje liczby");
- Los.display();
- System.out.println("<-- Wylosowane liczby");
- Liczby Zgodne = new Liczby();
- for(int i = 0; i < 6; i++ )
- {
- if( Los.is_value(L.get_tab(i)) )
- {
- System.out.println(L.get_tab(i));
- int x = L.get_tab(i);
- Zgodne.push( x );
- System.out.println(Zgodne.get_tab( Zgodne.get_top()-1 ) + " , " + x );
- }
- }
- System.out.println("Trafiłeś " + Zgodne.get_top() + " razy");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement