Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.72 KB | None | 0 0
  1. 1.
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5.  
  6. public class Array<E> {
  7.  
  8.  
  9.     public static int brojDoProsek(int niza[],int n ){
  10.         int suma=0;
  11.         int tmp=0;
  12.         float min=0;
  13.         int poz=0;
  14.         float pom;
  15.         float razlika1=0;
  16.         for(int i=0;i<n;i++)
  17.         {
  18.             suma+=niza[i];
  19.         }
  20.         pom=suma/n;
  21.        min=Math.abs(niza[0]-pom);
  22.         poz=0;
  23.         for(int i=0;i<n;i++)
  24.         {
  25.            
  26.             razlika1=Math.abs(niza[i]-pom);
  27.             if(min>razlika1)
  28.             {
  29.                 min=razlika1;
  30.                 poz=i;
  31.                 tmp=niza[i];
  32.             }
  33.             else if(min==razlika1)
  34.             {
  35.                 if(niza[poz]<=niza[i])
  36.                 {
  37.                     tmp=niza[poz];
  38.                 }
  39.                 else tmp=niza[i];
  40.             }
  41.             razlika1=0;
  42.            
  43.         }
  44.         return tmp;
  45.     }
  46.    
  47.     public static void main(String[] args) throws IOException{
  48.         BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in));
  49.         String s = stdin.readLine();
  50.         int N = Integer.parseInt(s);
  51.        
  52.         int niza[]=new int[N];
  53.         for(int i=0;i<N;i++)
  54.         {
  55.             s = stdin.readLine();
  56.             niza[i]=Integer.parseInt(s);
  57.         }
  58.  
  59.        
  60.         System.out.println(brojDoProsek(niza,N));      
  61.     }
  62.    
  63.    
  64.  
  65. }
  66.  
  67. lab 2
  68. 1.
  69. import java.io.BufferedReader;
  70. import java.io.IOException;
  71. import java.io.InputStreamReader;
  72. import java.util.Iterator;
  73. import java.util.NoSuchElementException;
  74. public class DivideOddEven<E extends Comparable <E>> {
  75. public static class DLLNode<E> {
  76.     protected E element;
  77.     protected DLLNode<E> pred, succ;
  78.  
  79.     public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  80.         this.element = elem;
  81.         this.pred = pred;
  82.         this.succ = succ;
  83.     }
  84. }
  85.  
  86. public static class DLL<E> {
  87.     private DLLNode<E> first, last;
  88.  
  89.     public DLL() {
  90.         // Construct an empty SLL
  91.         this.first = null;
  92.         this.last = null;
  93.     }
  94.  
  95.     public void deleteList() {
  96.         first = null;
  97.         last = null;
  98.     }
  99.  
  100.     public int length() {
  101.         int ret;
  102.         if (first != null) {
  103.             DLLNode<E> tmp = first;
  104.             ret = 1;
  105.             while (tmp.succ != null) {
  106.                 tmp = tmp.succ;
  107.                 ret++;
  108.             }
  109.             return ret;
  110.         } else
  111.             return 0;
  112.  
  113.     }
  114.  
  115.     public DLLNode<E> find(E o) {
  116.         if (first != null) {
  117.             DLLNode<E> tmp = first;
  118.             while (tmp.element != o&&tmp.succ != null)
  119.                 tmp = tmp.succ;
  120.             if (tmp.element == o) {
  121.                 return tmp;
  122.             } else {
  123.                 System.out.println("Elementot ne postoi vo listata");
  124.             }
  125.         } else {
  126.             System.out.println("Listata e prazna");
  127.         }
  128.         return first;
  129.     }
  130.  
  131.     public void insertFirst(E o) {
  132.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  133.         if (first == null)
  134.             last = ins;
  135.         else
  136.             first.pred = ins;
  137.         first = ins;
  138.     }
  139.  
  140.     public void insertLast(E o) {
  141.         if (first == null)
  142.             insertFirst(o);
  143.         else {
  144.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  145.             last.succ = ins;
  146.             last = ins;
  147.         }
  148.     }
  149.  
  150.     public void insertAfter(E o, DLLNode<E> after) {
  151.         if (after == last) {
  152.             insertLast(o);
  153.             return;
  154.         }
  155.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  156.         after.succ.pred = ins;
  157.         after.succ = ins;
  158.     }
  159.  
  160.     public void insertBefore(E o, DLLNode<E> before) {
  161.         if (before == first) {
  162.             insertFirst(o);
  163.             return;
  164.         }
  165.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  166.         before.pred.succ = ins;
  167.         before.pred = ins;
  168.     }
  169.  
  170.     public E deleteFirst() {
  171.         if (first != null) {
  172.             DLLNode<E> tmp = first;
  173.             first = first.succ;
  174.             if (first != null)
  175.                 first.pred = null;
  176.             if (first == null)
  177.                 last = null;
  178.             return tmp.element;
  179.         } else
  180.             return null;
  181.     }
  182.  
  183.     public E deleteLast() {
  184.         if (first != null) {
  185.             if (first.succ == null)
  186.                 return deleteFirst();
  187.             else {
  188.                 DLLNode<E> tmp = last;
  189.                 last = last.pred;
  190.                 last.succ = null;
  191.                 return tmp.element;
  192.             }
  193.         }
  194.         // else throw Exception
  195.         return null;
  196.     }
  197.  
  198.     public E delete(DLLNode<E> node) {
  199.         if (node == first) {
  200.             deleteFirst();
  201.             return node.element;
  202.         }
  203.         if (node == last) {
  204.             deleteLast();
  205.             return node.element;
  206.         }
  207.         node.pred.succ = node.succ;
  208.         node.succ.pred = node.pred;
  209.         return node.element;
  210.  
  211.     }
  212.  
  213.     public String toStringR() {
  214.         String ret = new String();
  215.         if (last != null) {
  216.             DLLNode<E> tmp = last;
  217.             ret += tmp + "<->";
  218.             while (tmp.pred != null) {
  219.                 tmp = tmp.pred;
  220.                 ret += tmp + "<->";
  221.             }
  222.         } else
  223.             ret = "Prazna lista!!!";
  224.         return ret;
  225.     }
  226.  
  227.     public DLLNode<E> getFirst() {
  228.         return first;
  229.     }
  230.  
  231.     public DLLNode<E> getLast() {
  232.  
  233.         return last;
  234.     }
  235.  
  236.     @Override
  237.     public String toString() {
  238.         String ret = new String();
  239.         if (first != null) {
  240.             DLLNode<E> tmp = first;
  241.             ret += tmp + "<->";
  242.             while (tmp.succ != null) {
  243.                 tmp = tmp.succ;
  244.                 ret += tmp + "<->";
  245.             }
  246.         } else
  247.             ret = "Prazna lista!!!";
  248.         return ret;
  249.     }
  250.     private class LRIterator<E> implements Iterator<E>{
  251.         private DLLNode<E> place;
  252.         private DLLNode<E> prev;
  253.         private DLLNode<E> curr;
  254.         private LRIterator(){
  255.             place=(DLLNode<E>) first;
  256.             curr=null;
  257.             prev=null;
  258.         }
  259.         public boolean hasNext(){
  260.             return (place != null);
  261.         }
  262.         public E next(){
  263.             if(place==null){
  264.                 throw new NoSuchElementException();
  265.             }
  266.             if(place==last){
  267.                 E nextElem=place.element;
  268.                 prev=curr;
  269.                 curr=place;
  270.                 place=null;
  271.                 return nextElem;
  272.             }
  273.             E nextElem=place.element;
  274.             prev=curr;
  275.             curr=place;
  276.             place=place.succ;
  277.             return nextElem;
  278.         }
  279.         @Override
  280.         public void remove() {
  281.             // TODO Auto-generated method stub
  282.            
  283.         }
  284.     }
  285.    
  286.     public Iterator<E> iterator(){
  287.         return new LRIterator<E>();
  288.     }
  289.  
  290. }
  291.  
  292. public static  DLL funkcija(DLL lista) {
  293.     DLL rezP = new DLL<>();
  294.     DLLNode jazol1 =  lista.getFirst();
  295.     //System.out.println("Razgleduvam"+jazol1.element);
  296.     while (jazol1 != null) {
  297.        
  298.         Integer tmp;
  299.         tmp=(int)jazol1.element;
  300.         if (tmp % 2 == 0) {
  301.             //System.out.println("Ova e"+jazol1.element);
  302.             rezP.insertLast(jazol1.element);
  303.             jazol1 = jazol1.succ;
  304.             //System.out.println("Printam"+rezP);
  305.         }
  306.         else {
  307.             jazol1=jazol1.succ;
  308.        
  309.         }
  310.     }
  311.  
  312.     return rezP;
  313. }
  314.  
  315. public static  DLL funkcija1(DLL lista) {
  316.     DLL rezN = new DLL<>();
  317.     DLLNode jazol1 = lista.getFirst();
  318.     //System.out.println("Razgleduvam"+jazol1.element);
  319.     while (jazol1 != null) {
  320.         Integer tmp1;
  321.         tmp1=(int)jazol1.element;
  322.         if (tmp1 % 2 != 0) {
  323.             rezN.insertLast(jazol1.element);
  324.             jazol1 = jazol1.succ;
  325.         }
  326.         else
  327.             {
  328.             jazol1=jazol1.succ;
  329.             }
  330.     }
  331.  
  332.     return rezN;
  333. }
  334.  
  335.     public static void main(String[] args) throws IOException {
  336.         DLL lista = new DLL();
  337.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  338.                 System.in));
  339.         String s = stdin.readLine();
  340.         int N = Integer.parseInt(s);
  341.         s = stdin.readLine();
  342.         String[] pomniza = s.split(" ");
  343.         for (int i = 0; i < N; i++) {
  344.             lista.insertLast(Integer.parseInt(pomniza[i]));
  345.         }
  346.         DLL pom=new DLL();
  347.         pom=funkcija1(lista);
  348.          Iterator<Integer> it2 = pom.iterator();
  349.             while (it2.hasNext()) {
  350.                     System.out.print(it2.next());
  351.             if(it2.hasNext())
  352.                 System.out.print(" ");
  353.                 }
  354.         System.out.println();
  355.        
  356.         DLL pom1=new DLL();
  357.         pom1=funkcija(lista);
  358.         Iterator<Integer> it = pom1.iterator();
  359.         while (it.hasNext()) {
  360.                 System.out.print(it.next());
  361.         if(it.hasNext()){
  362.             System.out.print(" ");
  363.             }
  364.         }
  365.     System.out.println();
  366.     }
  367.  
  368. }
  369.  
  370. 2.
  371. import java.io.BufferedReader;
  372. import java.io.InputStreamReader;
  373.  
  374. public class Homework {
  375.    
  376.     static int minBrojKazneni(int a[],int n) {
  377.        
  378.        int suma=0;
  379.         int tmp;
  380.             for(int i=0;i<n-1;i++)
  381.         {
  382.             for(int j=i+1;j<n;j++)
  383.             {
  384.                 if(a[i]>a[j])
  385.                 {
  386.                     tmp=a[i];
  387.                     a[i]=a[j];
  388.                     a[j]=tmp;
  389.                    
  390.                 }
  391.             }
  392.                
  393.         }
  394.         int j;
  395.         for(int i=0;i<n;i++)
  396.         {  
  397.             j=i;
  398.            
  399.             while(j<n)
  400.             {
  401.                 suma+=a[i];
  402.                 j++;
  403.             }
  404.            
  405.         }
  406.         return suma;
  407.     }
  408.    
  409.     public static void main(String[] args) throws Exception {
  410.         int i;
  411.        
  412.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  413.         int N = Integer.parseInt(br.readLine());
  414.         int a[] = new int[N];
  415.         int pom[];
  416.         for (i=0;i<N;i++)
  417.             a[i] = Integer.parseInt(br.readLine());
  418.        
  419.         int rez = minBrojKazneni(a,N);
  420.        
  421.         System.out.println(rez);
  422.        
  423.         br.close();
  424.     }
  425.    
  426. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement