Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.21 KB | None | 0 0
  1. FAKULTET
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Homework {
  6.  
  7. static int minBrojKazneni(int a[]) {
  8. // Vasiot kod tuka
  9. int tmp;
  10.  
  11. for (int i = 0; i<a.length;i++){
  12. for (int j=i+1; j<a.length; j++){
  13. if (a[i] < a[j]){
  14. tmp = a[i];
  15. a[i] = a[j];
  16. a[j] = tmp;
  17. }
  18. }
  19. }
  20.  
  21. int suma =0;
  22. int pom =0;
  23. for (int i = 0; i<a.length ; i++){
  24. suma += a[i];
  25. for (int j=i+1; j<a.length; j++){
  26. pom += a[j];
  27. }
  28. }
  29. return suma+pom;
  30. }
  31.  
  32. public static void main(String[] args) throws Exception {
  33. int i;
  34.  
  35. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  36. int N = Integer.parseInt(br.readLine());
  37. int a[] = new int[N];
  38.  
  39. for (i=0;i<N;i++)
  40. a[i] = Integer.parseInt(br.readLine());
  41.  
  42. int rez = minBrojKazneni(a);
  43.  
  44. System.out.println(rez);
  45.  
  46. br.close();
  47. }
  48.  
  49. }
  50. KOMPANIJA
  51. import java.util.*;
  52.  
  53.  
  54. class DLLNode<E> {
  55. protected E ID,Plata;
  56. protected DLLNode<E> succ,pred;
  57. public DLLNode() {
  58. this.ID=null;
  59. this.Plata=null;
  60. this.succ=null;
  61. this.pred=null;
  62. }
  63. public DLLNode (E ID,E Plata, DLLNode<E> succ,DLLNode<E> pred) {
  64. this.ID=ID;
  65. this.Plata=Plata;
  66. this.succ=succ;
  67. this.pred=pred;
  68. }
  69. @Override
  70. public String toString() {
  71. return String.format("%s %s",ID.toString(),Plata.toString());
  72. }
  73. public void setData(E ID , E Plata) {
  74. this.ID=ID;
  75. this.Plata=Plata;
  76. }
  77.  
  78.  
  79. };
  80.  
  81. class DLL<E extends Comparable <E>> {
  82. private DLLNode<E> first, last;
  83. public DLL () {
  84. //construct empty list
  85. this.first=null;
  86. this.last=null;
  87. }
  88. public void insertFirst(E o,E o1) {
  89. DLLNode<E> ins = new DLLNode<E>(o,o1,null,first);
  90. if(first == null) {
  91. first=ins;
  92. } else {
  93. first.pred=ins;
  94. first=ins;
  95. }
  96.  
  97. }
  98. public int getSize() {
  99. int ret;
  100. if (first != null) {
  101. DLLNode<E> tmp = first;
  102. ret = 1;
  103. while (tmp.succ != null) {
  104. tmp = tmp.succ;
  105. ret++;
  106. }
  107. return ret;
  108. } else
  109. return 0;
  110.  
  111. }
  112.  
  113. public void insertLast(E o , E o1) {
  114. if (first != null) {
  115. DLLNode<E> tmp = first;
  116. while (tmp.succ != null)
  117. tmp = tmp.succ;
  118. DLLNode<E> ins = new DLLNode<E>(o,o1 , last,null);
  119. tmp.succ = ins;
  120. } else {
  121. insertFirst(o,o1);
  122. }
  123.  
  124. }
  125. public DLLNode<E> getFirst() {
  126. return first;
  127. }
  128. public DLLNode<E> getLast() {
  129. return last;
  130. }
  131. };
  132. public class DLLKompanija {
  133. public static DLL<Integer> getGreaterThan(DLL<Integer> list ,int Greater) {
  134. DLLNode<Integer> tmp = list.getFirst();
  135. DLL<Integer> list1 = new DLL<Integer>();
  136. while(tmp!=null) {
  137. if(tmp.Plata>=Greater) {
  138. list1.insertLast(tmp.ID, tmp.Plata);
  139. }
  140. tmp=tmp.succ;
  141. }
  142.  
  143. return list1;
  144. }
  145. //@SuppressWarnings("unused")
  146. public static void sort(DLL<Integer> list) {
  147. int i = 0;
  148. if(list.getFirst()==null)
  149. return;
  150. DLLNode<Integer> currentNode = list.getFirst();
  151. DLLNode<Integer> auxNode=new DLLNode<Integer>();
  152. boolean foundChange =true;
  153. while(foundChange) {
  154. if(currentNode.succ==null)
  155. break;
  156. foundChange = false;
  157. for(i=0; i<list.getSize()-1; i++) {
  158. if (currentNode.succ.ID>currentNode.ID) {
  159. auxNode.setData(currentNode.ID,currentNode.Plata);
  160. currentNode.setData(currentNode.succ.ID,currentNode.succ.Plata);
  161. currentNode.succ.setData(auxNode.ID,auxNode.Plata);
  162. foundChange = true;
  163. }
  164.  
  165. if(currentNode.succ!=null)
  166. currentNode = currentNode.succ;
  167. }
  168.  
  169. }
  170. }
  171.  
  172. /* public static void sort(DLL<Integer> list){
  173.  
  174. for(int i =0;i<list.getSize()-1;i++){
  175. DLLNode<Integer> temp= list.getFirst();
  176. for(int j=0;j<list.getSize()-1-i;j++){
  177. if(temp.ID < temp.succ.ID ){
  178. DLLNode<Integer>tmp=temp;
  179. temp=temp.succ;
  180. temp.succ=tmp;
  181. }
  182. temp=temp.succ;
  183. }
  184.  
  185. }
  186.  
  187.  
  188. }
  189. */
  190. public static void print(DLL<Integer> list ) {
  191. DLLNode<Integer> tmp=list.getFirst();
  192. if (tmp==null){
  193. System.out.print("nema");
  194. return;
  195. }
  196. while(tmp!=null) {
  197. System.out.print(tmp.ID+" "+tmp.Plata+"\n");
  198.  
  199. //;
  200. tmp=tmp.succ;
  201. }
  202.  
  203. }
  204.  
  205.  
  206. public static void main (String[] args) {
  207.  
  208. Scanner s = new Scanner(System.in);
  209. DLL<Integer> list1 = new DLL<Integer>();
  210. int n = s.nextInt();
  211. int [] ID = new int [n];
  212. int [] Plata = new int [n];
  213. for(int i =0; i<n; i++) {
  214. ID[i] = s.nextInt();
  215. Plata[i]=s.nextInt();
  216. list1.insertLast(ID[i],Plata[i]);
  217. }
  218. int plata;
  219. plata=s.nextInt();
  220. DLL<Integer> result = new DLL<Integer>();
  221. result = getGreaterThan(list1,plata);
  222. //print(result);
  223. // System.out.println();
  224. sort(result);
  225. sort(result);
  226. sort(result);
  227. sort(result);
  228. sort(result);
  229. sort(result);
  230. print(result);
  231.  
  232. s.close();
  233. }
  234.  
  235.  
  236.  
  237. };
  238. Dvostrano povrzana lista
  239. import java.io.BufferedReader;
  240. import java.io.IOException;
  241. import java.io.InputStreamReader;
  242.  
  243. class DLL<E> {
  244. private DLLNode<E> first, last;
  245.  
  246. public DLL() {
  247. // Construct an empty SLL
  248. this.first = null;
  249. this.last = null;
  250. }
  251.  
  252. public void deleteList() {
  253. first = null;
  254. last = null;
  255. }
  256.  
  257. public int length() {
  258. int ret;
  259. if (first != null) {
  260. DLLNode<E> tmp = first;
  261. ret = 1;
  262. while (tmp.succ != null) {
  263. tmp = tmp.succ;
  264. ret++;
  265. }
  266. return ret;
  267. } else
  268. return 0;
  269.  
  270. }
  271.  
  272. public DLLNode<E> find(E o) {
  273. if (first != null) {
  274. DLLNode<E> tmp = first;
  275. while (tmp.element != o&&tmp.succ != null)
  276. tmp = tmp.succ;
  277. if (tmp.element == o) {
  278. return tmp;
  279. } else {
  280. System.out.println("Elementot ne postoi vo listata");
  281. }
  282. } else {
  283. System.out.println("Listata e prazna");
  284. }
  285. return first;
  286. }
  287.  
  288. public void insertFirst(E o) {
  289. DLLNode<E> ins = new DLLNode<E>(o, null, first);
  290. if (first == null)
  291. last = ins;
  292. else
  293. first.pred = ins;
  294. first = ins;
  295. }
  296.  
  297. public void insertLast(E o) {
  298. if (first == null)
  299. insertFirst(o);
  300. else {
  301. DLLNode<E> ins = new DLLNode<E>(o, last, null);
  302. last.succ = ins;
  303. last = ins;
  304. }
  305. }
  306.  
  307. public void insertAfter(E o, DLLNode<E> after) {
  308. if(after==last){
  309. insertLast(o);
  310. return;
  311. }
  312. DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  313. after.succ.pred = ins;
  314. after.succ = ins;
  315. }
  316.  
  317. public void insertBefore(E o, DLLNode<E> before) {
  318. if(before == first){
  319. insertFirst(o);
  320. return;
  321. }
  322. DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  323. before.pred.succ = ins;
  324. before.pred = ins;
  325. }
  326.  
  327. public E deleteFirst() {
  328. if (first != null) {
  329. DLLNode<E> tmp = first;
  330. first = first.succ;
  331. if (first != null) first.pred = null;
  332. if (first == null)
  333. last = null;
  334. return tmp.element;
  335. } else
  336. return null;
  337. }
  338.  
  339. public E deleteLast() {
  340. if (first != null) {
  341. if (first.succ == null)
  342. return deleteFirst();
  343. else {
  344. DLLNode<E> tmp = last;
  345. last = last.pred;
  346. last.succ = null;
  347. return tmp.element;
  348. }
  349. }
  350. // else throw Exception
  351. return null;
  352. }
  353.  
  354. public E delete(DLLNode<E> node) {
  355. if(node==first){
  356. deleteFirst();
  357. return node.element;
  358. }
  359. if(node==last){
  360. deleteLast();
  361. return node.element;
  362. }
  363. node.pred.succ = node.succ;
  364. node.succ.pred = node.pred;
  365. return node.element;
  366.  
  367. }
  368.  
  369. @Override
  370. public String toString() {
  371. String ret = new String();
  372. if (first != null) {
  373. DLLNode<E> tmp = first;
  374. ret += tmp + "<->";
  375. while (tmp.succ != null) {
  376. tmp = tmp.succ;
  377. ret += tmp + "<->";
  378. }
  379. } else
  380. ret = "Prazna lista!!!";
  381. return ret;
  382. }
  383.  
  384. public String toStringR() {
  385. String ret = new String();
  386. if (last != null) {
  387. DLLNode<E> tmp = last;
  388. ret += tmp + "<->";
  389. while (tmp.pred != null) {
  390. tmp = tmp.pred;
  391. ret += tmp + "<->";
  392. }
  393. } else
  394. ret = "Prazna lista!!!";
  395. return ret;
  396. }
  397.  
  398. public DLLNode<E> getFirst() {
  399. return first;
  400. }
  401.  
  402. public DLLNode<E> getLast() {
  403.  
  404. return last;
  405. }
  406.  
  407. public void izvadiDupliIPrebroj(){
  408.  
  409. }
  410. }
  411.  
  412.  
  413. class DLLNode<E> {
  414. protected E element;
  415. protected DLLNode<E> pred, succ;
  416.  
  417. public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  418. this.element = elem;
  419. this.pred = pred;
  420. this.succ = succ;
  421. }
  422.  
  423. @Override
  424. public String toString() {
  425. return element.toString();
  426. }
  427. }
  428.  
  429.  
  430.  
  431.  
  432.  
  433. public class DivideOddEven {
  434.  
  435.  
  436. public static DLL<Integer> parna(DLL<Integer> lista){
  437. DLL<Integer> par = new DLL<Integer> ();
  438. DLLNode<Integer> tmp = lista.getFirst();
  439.  
  440. while (tmp !=null){
  441. if (tmp.element % 2 == 0){
  442. par.insertLast(tmp.element);
  443.  
  444. }
  445. tmp = tmp.succ;
  446.  
  447. }
  448.  
  449. return par;
  450. }
  451.  
  452.  
  453. public static DLL<Integer> neparna(DLL<Integer> lista){
  454. DLL<Integer> nep = new DLL<Integer> ();
  455. DLLNode<Integer> tmp = lista.getFirst();
  456.  
  457. while (tmp !=null){
  458. if (tmp.element % 2 != 0){
  459. nep.insertLast(tmp.element);
  460.  
  461. }
  462. tmp = tmp.succ;
  463.  
  464. }
  465.  
  466. return nep;
  467. }
  468.  
  469. public static void pecati(DLL<Integer> lista){
  470. DLLNode<Integer> tmp = lista.getFirst();
  471. while (tmp != null){
  472. if (tmp.succ == null){
  473. System.out.print(tmp.element);
  474. break;
  475. }
  476. System.out.print(tmp.element + " ");
  477. tmp = tmp.succ;
  478.  
  479. }
  480. }
  481.  
  482.  
  483. public static void main(String[] args) throws IOException {
  484.  
  485. DLL<Integer> lista = new DLL<Integer> ();
  486.  
  487. BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
  488.  
  489. String s = br.readLine();
  490. int N = Integer.parseInt(s);
  491. String a[] = br.readLine().split(" ");
  492. for (int i=0;i<N;i++){
  493. lista.insertLast(Integer.parseInt(a[i]));
  494. }
  495.  
  496. parna(lista);
  497. neparna(lista);
  498. pecati(neparna(lista));
  499. System.out.print("\n");
  500. pecati(parna(lista));
  501.  
  502.  
  503.  
  504.  
  505. }
  506.  
  507. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement