Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FAKULTET
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- public class Homework {
- static int minBrojKazneni(int a[]) {
- // Vasiot kod tuka
- int tmp;
- for (int i = 0; i<a.length;i++){
- for (int j=i+1; j<a.length; j++){
- if (a[i] < a[j]){
- tmp = a[i];
- a[i] = a[j];
- a[j] = tmp;
- }
- }
- }
- int suma =0;
- int pom =0;
- for (int i = 0; i<a.length ; i++){
- suma += a[i];
- for (int j=i+1; j<a.length; j++){
- pom += a[j];
- }
- }
- return suma+pom;
- }
- public static void main(String[] args) throws Exception {
- int i;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int N = Integer.parseInt(br.readLine());
- int a[] = new int[N];
- for (i=0;i<N;i++)
- a[i] = Integer.parseInt(br.readLine());
- int rez = minBrojKazneni(a);
- System.out.println(rez);
- br.close();
- }
- }
- KOMPANIJA
- import java.util.*;
- class DLLNode<E> {
- protected E ID,Plata;
- protected DLLNode<E> succ,pred;
- public DLLNode() {
- this.ID=null;
- this.Plata=null;
- this.succ=null;
- this.pred=null;
- }
- public DLLNode (E ID,E Plata, DLLNode<E> succ,DLLNode<E> pred) {
- this.ID=ID;
- this.Plata=Plata;
- this.succ=succ;
- this.pred=pred;
- }
- @Override
- public String toString() {
- return String.format("%s %s",ID.toString(),Plata.toString());
- }
- public void setData(E ID , E Plata) {
- this.ID=ID;
- this.Plata=Plata;
- }
- };
- class DLL<E extends Comparable <E>> {
- private DLLNode<E> first, last;
- public DLL () {
- //construct empty list
- this.first=null;
- this.last=null;
- }
- public void insertFirst(E o,E o1) {
- DLLNode<E> ins = new DLLNode<E>(o,o1,null,first);
- if(first == null) {
- first=ins;
- } else {
- first.pred=ins;
- first=ins;
- }
- }
- public int getSize() {
- int ret;
- if (first != null) {
- DLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- public void insertLast(E o , E o1) {
- if (first != null) {
- DLLNode<E> tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- DLLNode<E> ins = new DLLNode<E>(o,o1 , last,null);
- tmp.succ = ins;
- } else {
- insertFirst(o,o1);
- }
- }
- public DLLNode<E> getFirst() {
- return first;
- }
- public DLLNode<E> getLast() {
- return last;
- }
- };
- public class DLLKompanija {
- public static DLL<Integer> getGreaterThan(DLL<Integer> list ,int Greater) {
- DLLNode<Integer> tmp = list.getFirst();
- DLL<Integer> list1 = new DLL<Integer>();
- while(tmp!=null) {
- if(tmp.Plata>=Greater) {
- list1.insertLast(tmp.ID, tmp.Plata);
- }
- tmp=tmp.succ;
- }
- return list1;
- }
- //@SuppressWarnings("unused")
- public static void sort(DLL<Integer> list) {
- int i = 0;
- if(list.getFirst()==null)
- return;
- DLLNode<Integer> currentNode = list.getFirst();
- DLLNode<Integer> auxNode=new DLLNode<Integer>();
- boolean foundChange =true;
- while(foundChange) {
- if(currentNode.succ==null)
- break;
- foundChange = false;
- for(i=0; i<list.getSize()-1; i++) {
- if (currentNode.succ.ID>currentNode.ID) {
- auxNode.setData(currentNode.ID,currentNode.Plata);
- currentNode.setData(currentNode.succ.ID,currentNode.succ.Plata);
- currentNode.succ.setData(auxNode.ID,auxNode.Plata);
- foundChange = true;
- }
- if(currentNode.succ!=null)
- currentNode = currentNode.succ;
- }
- }
- }
- /* public static void sort(DLL<Integer> list){
- for(int i =0;i<list.getSize()-1;i++){
- DLLNode<Integer> temp= list.getFirst();
- for(int j=0;j<list.getSize()-1-i;j++){
- if(temp.ID < temp.succ.ID ){
- DLLNode<Integer>tmp=temp;
- temp=temp.succ;
- temp.succ=tmp;
- }
- temp=temp.succ;
- }
- }
- }
- */
- public static void print(DLL<Integer> list ) {
- DLLNode<Integer> tmp=list.getFirst();
- if (tmp==null){
- System.out.print("nema");
- return;
- }
- while(tmp!=null) {
- System.out.print(tmp.ID+" "+tmp.Plata+"\n");
- //;
- tmp=tmp.succ;
- }
- }
- public static void main (String[] args) {
- Scanner s = new Scanner(System.in);
- DLL<Integer> list1 = new DLL<Integer>();
- int n = s.nextInt();
- int [] ID = new int [n];
- int [] Plata = new int [n];
- for(int i =0; i<n; i++) {
- ID[i] = s.nextInt();
- Plata[i]=s.nextInt();
- list1.insertLast(ID[i],Plata[i]);
- }
- int plata;
- plata=s.nextInt();
- DLL<Integer> result = new DLL<Integer>();
- result = getGreaterThan(list1,plata);
- //print(result);
- // System.out.println();
- sort(result);
- sort(result);
- sort(result);
- sort(result);
- sort(result);
- sort(result);
- print(result);
- s.close();
- }
- };
- Dvostrano povrzana lista
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- class DLL<E> {
- private DLLNode<E> first, last;
- public DLL() {
- // Construct an empty SLL
- this.first = null;
- this.last = null;
- }
- public void deleteList() {
- first = null;
- last = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- DLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- public DLLNode<E> find(E o) {
- if (first != null) {
- DLLNode<E> tmp = first;
- while (tmp.element != o&&tmp.succ != null)
- tmp = tmp.succ;
- if (tmp.element == o) {
- return tmp;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- return first;
- }
- public void insertFirst(E o) {
- DLLNode<E> ins = new DLLNode<E>(o, null, first);
- if (first == null)
- last = ins;
- else
- first.pred = ins;
- first = ins;
- }
- public void insertLast(E o) {
- if (first == null)
- insertFirst(o);
- else {
- DLLNode<E> ins = new DLLNode<E>(o, last, null);
- last.succ = ins;
- last = ins;
- }
- }
- public void insertAfter(E o, DLLNode<E> after) {
- if(after==last){
- insertLast(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
- after.succ.pred = ins;
- after.succ = ins;
- }
- public void insertBefore(E o, DLLNode<E> before) {
- if(before == first){
- insertFirst(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
- before.pred.succ = ins;
- before.pred = ins;
- }
- public E deleteFirst() {
- if (first != null) {
- DLLNode<E> tmp = first;
- first = first.succ;
- if (first != null) first.pred = null;
- if (first == null)
- last = null;
- return tmp.element;
- } else
- return null;
- }
- public E deleteLast() {
- if (first != null) {
- if (first.succ == null)
- return deleteFirst();
- else {
- DLLNode<E> tmp = last;
- last = last.pred;
- last.succ = null;
- return tmp.element;
- }
- }
- // else throw Exception
- return null;
- }
- public E delete(DLLNode<E> node) {
- if(node==first){
- deleteFirst();
- return node.element;
- }
- if(node==last){
- deleteLast();
- return node.element;
- }
- node.pred.succ = node.succ;
- node.succ.pred = node.pred;
- return node.element;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- DLLNode<E> tmp = first;
- ret += tmp + "<->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public String toStringR() {
- String ret = new String();
- if (last != null) {
- DLLNode<E> tmp = last;
- ret += tmp + "<->";
- while (tmp.pred != null) {
- tmp = tmp.pred;
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public DLLNode<E> getFirst() {
- return first;
- }
- public DLLNode<E> getLast() {
- return last;
- }
- public void izvadiDupliIPrebroj(){
- }
- }
- class DLLNode<E> {
- protected E element;
- protected DLLNode<E> pred, succ;
- public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
- this.element = elem;
- this.pred = pred;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- public class DivideOddEven {
- public static DLL<Integer> parna(DLL<Integer> lista){
- DLL<Integer> par = new DLL<Integer> ();
- DLLNode<Integer> tmp = lista.getFirst();
- while (tmp !=null){
- if (tmp.element % 2 == 0){
- par.insertLast(tmp.element);
- }
- tmp = tmp.succ;
- }
- return par;
- }
- public static DLL<Integer> neparna(DLL<Integer> lista){
- DLL<Integer> nep = new DLL<Integer> ();
- DLLNode<Integer> tmp = lista.getFirst();
- while (tmp !=null){
- if (tmp.element % 2 != 0){
- nep.insertLast(tmp.element);
- }
- tmp = tmp.succ;
- }
- return nep;
- }
- public static void pecati(DLL<Integer> lista){
- DLLNode<Integer> tmp = lista.getFirst();
- while (tmp != null){
- if (tmp.succ == null){
- System.out.print(tmp.element);
- break;
- }
- System.out.print(tmp.element + " ");
- tmp = tmp.succ;
- }
- }
- public static void main(String[] args) throws IOException {
- DLL<Integer> lista = new DLL<Integer> ();
- BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
- String s = br.readLine();
- int N = Integer.parseInt(s);
- String a[] = br.readLine().split(" ");
- for (int i=0;i<N;i++){
- lista.insertLast(Integer.parseInt(a[i]));
- }
- parna(lista);
- neparna(lista);
- pecati(neparna(lista));
- System.out.print("\n");
- pecati(parna(lista));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement