Advertisement
MusicFreak

Algoritmi Vjezbe - Drago 23.10.2015

Oct 23rd, 2015
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1. Zadatak 1. Pravougli trougao
  2.  
  3.  
  4. public class PravougliTrougao {
  5.     public static void main(String args[])
  6.     {
  7.         double a, b, c;
  8.         a = Double.parseDouble(args[0]);
  9.         b = Double.parseDouble(args[1]);
  10.        
  11.         if (a>0 && b>0)
  12.         {
  13.             c = Math.sqrt(Math.pow(a, 2)+ Math.pow(b, 2));
  14.             System.out.println(c);
  15.         }
  16.         else
  17.         {
  18.             System.out.println("Brojevi moraju biti pozitivni!");
  19.         }
  20.     }
  21. }
  22.  
  23. Zadatak 2. Minimalni broj od 3 unesena
  24.  
  25. import java.util.Scanner;
  26. public class MinOd3Broja {
  27.     public static void main(String args[])
  28.     {
  29.         int a, b, c, min;
  30.         Scanner scan = new Scanner(System.in);
  31.         System.out.print("A = ");
  32.         a = scan.nextInt();
  33.         System.out.print("B = ");
  34.         b = scan.nextInt();
  35.         System.out.print("C = ");
  36.         c = scan.nextInt();
  37.        
  38.         if (a>= 0 && b>= 0 && c>= 0)
  39.         {
  40.             min = a;
  41.             if (b < min)
  42.                 min = b;
  43.             if (c < min)
  44.                 min = c;
  45.                 System.out.println("Minimalni broj od ("+a+", "+b+", "+c+") = "+min);
  46.         }
  47.             else
  48.         {
  49.             System.out.println("Ne ispravne vrijednosti");
  50.         }
  51.        
  52.        
  53.     }
  54.  
  55. }
  56.  
  57. Zadatak 3. Najmanji broj u nizu brojeva
  58.  
  59. import java.util.Scanner;
  60. public class NajmanjiBrojIzNiza {
  61.     public static void main(String args[])
  62.     {
  63.        
  64.         int niz[] = new int[10];
  65.         //int niz[] = {1, 4, 5, 2, 6, 5, 6}; ---> Drugi nacin unosenja niza
  66.        
  67.         Scanner scan = new Scanner(System.in);
  68.  
  69.         for (int i = 0; i<=9; i++)
  70.         {
  71.             System.out.print(i+1+". broj = ");
  72.             niz[i]= scan.nextInt();
  73.         }
  74.        
  75.         int min = niz[0];
  76.        
  77.         for (int i = 0; i<=9; i++)
  78.         {
  79.             if (niz[i]<min)
  80.                 min = niz[i];
  81.  
  82.         }
  83.         System.out.println("Najmanji broj je "+min);
  84.     }
  85.  
  86. }
  87.  
  88.  
  89. Zadatak 4. Sortiranje 3 broja
  90.  
  91. import java.util.Scanner;
  92.  
  93.  
  94. public class Sortiranje {
  95.  
  96.     public static void main(String[] args)
  97.     {
  98.         int a, b, c, p;
  99.         Scanner scan = new Scanner(System.in);
  100.         System.out.print("A = ");
  101.         a = scan.nextInt();
  102.         System.out.print("B = ");
  103.         b = scan.nextInt();
  104.         System.out.print("C = ");
  105.         c = scan.nextInt();
  106.        
  107.         if (a > b)
  108.         {
  109.             p = a;
  110.             a = b;
  111.             b = p;
  112.         }
  113.         if (a > c)
  114.         {
  115.             p = a;
  116.             a = c;
  117.             c = p;
  118.         }
  119.         if (b > c)
  120.         {
  121.             p = b;
  122.             b = c;
  123.             c = p;
  124.         }
  125.         System.out.println("A = "+a+", B = "+b+", C = "+c);
  126.     }
  127.  
  128. }
  129.  
  130. Zadatak 5. Sortiranje brojeva iz liste
  131.  
  132. import java.util.ArrayList;
  133. import java.util.Collections;
  134. import java.util.Comparator;
  135. import java.util.Scanner;
  136. public class SortiranjePrekoListom {
  137.     public static void main(String[] args)
  138.     {
  139.         ArrayList<Integer> lista = new ArrayList(); --> U < > zagradama navodimo tip podatka koji ce biti napisan u listi
  140.         Scanner scan = new Scanner(System.in);
  141.         int temp = 0;
  142.        
  143.         do
  144.         {
  145.             System.out.print(lista.size()+1+". broj = ");
  146.             temp = scan.nextInt();
  147.             if (temp != 0)
  148.                 lista.add(temp);
  149.             else{
  150.                 System.out.println("Popunjavanje liste je zavrseno");
  151.             }
  152.         }
  153.         while(temp != 0);
  154.         System.out.println("ISPIS ELEMENATA LISTE!");
  155.         for (int i = 0; i < lista.size();i++)
  156.             System.out.println(lista.get(i));
  157.  
  158.         Comparator com = Collections.reverseOrder(); //Za sortiranje u opadajucem nizu
  159.         Collections.sort(lista, com);//(lista) za sortiranje u rastucem
  160.         System.out.println("2. nacin: ISPIS ELEMENATA LISTE POSLE SORTIRANJA!");
  161.         for (int t : lista)
  162.         {
  163.             System.out.println(t);
  164.         }
  165.     }
  166.  
  167. }
  168.  
  169.  
  170. Zadatak 6. Tabeliranje izraza
  171.  
  172. import java.util.Scanner;
  173.  
  174.  
  175. public class TabeliranjeIzraza {
  176.     public static void main(String[] args)
  177.     {
  178.         double xmin, xmax, dx, x, y;
  179.         Scanner scan = new Scanner(System.in);
  180.         System.out.print("xmin = ");
  181.         xmin = scan.nextDouble();
  182.         System.out.print("xmax = ");
  183.         xmax = scan.nextDouble();
  184.         System.out.print("dx = ");
  185.         dx = scan.nextDouble();
  186.        
  187.         System.out.println("==========================");
  188.         System.out.println("X \tY");
  189.        
  190.         for (x = xmin; x <= xmax; x+=dx)
  191.         {
  192.             y = (x*x*-2*x-2)/(x*x+1);
  193.             System.out.println(x+"\t"+y);
  194.         }
  195.     }
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement