Advertisement
AnaGocevska

Прости_Сложени во две листи

Oct 20th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. package ListsArrays;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ProstiSlozeni {
  6.    
  7.     /*public static SLL<Integer> slozheni (Array<Integer> niza)
  8.     {
  9.         SLL<Integer> slozheni = new SLL<Integer>();
  10.        
  11.         for(int i=0; i<niza.getLength(); i++)
  12.         {
  13.             slozheni.insertLast(niza.get(i));
  14.         }
  15.         return slozheni;
  16.     }*/
  17.    
  18.     public static boolean prost(Integer a)
  19.     {
  20.         for(int i=2; i<a; i++)
  21.         {
  22.             if(a % i== 0)
  23.             {
  24.                 return false;
  25.             }
  26.         }
  27.         return true;
  28.     }
  29.    
  30.     public static SLL<Integer> prosti (Array<Integer> niza)
  31.     {
  32.         SLL<Integer> prosti = new SLL<Integer>();
  33.         for(int i=0; i<niza.getLength(); i++)
  34.         {
  35.             if(prost(niza.get(i)))
  36.             {
  37.                 prosti.insertLast(niza.get(i));
  38.             }
  39.         }
  40.         return prosti;
  41.     }
  42.    
  43.     public static SLL<Integer> slozheni (Array<Integer> niza)
  44.     {
  45.         SLL<Integer> slozheni = new SLL<Integer>();
  46.         for(int i=0; i<niza.getLength(); i++)
  47.         {
  48.             if(!prost(niza.get(i)))
  49.             {
  50.                 slozheni.insertLast(niza.get(i));
  51.             }
  52.         }
  53.         return slozheni;
  54.     }
  55.    
  56.     public static void main(String[] args)
  57.     {
  58.         Scanner s = new Scanner(System.in);
  59.        
  60.         System.out.println("Vnesi n: ");
  61.         int n = s.nextInt();
  62.        
  63.         Array<Integer> niza = new Array<Integer>(n);
  64.        
  65.         System.out.println("Vnesi niza: ");
  66.         for(int i=0; i<niza.getLength(); i++)
  67.         {
  68.             niza.set(i, s.nextInt());
  69.         }
  70.        
  71.         SLL<Integer> prosti = prosti(niza);
  72.         SLL<Integer> slozheni = slozheni(niza);
  73.         System.out.println("Prosti: " + prosti);
  74.         System.out.println("Slozheni: " + slozheni);
  75.     }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement