Upload
marjana1702
View
1.626
Download
49
Embed Size (px)
Citation preview
8/11/2019 Zadaci Za Prv Kolokvium
1/30
Zad1
- Java 1 (2 / 3) N (). : , .
N, . 1 , -1 . : PalindromeDLL
import java.util.Scanner;
class DLLNode {
protected E element;
protected DLLNode pred, succ;
public DLLNode(E elem, DLLNode pred, DLLNode succ) {
this.element = elem;
this.pred = pred;
this.succ = succ;
}
@Override
public String toString() {
return "";
}
}
class DLL {
private DLLNode 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 tmp = first;
ret = 1;
while (tmp.succ != null) {
tmp = tmp.succ;
ret++;
8/11/2019 Zadaci Za Prv Kolokvium
2/30
}
return ret;
} else
return 0;
}
public void insertFirst(E o) {
DLLNode ins = new DLLNode(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 ins = new DLLNode(o, last, null);
last.succ = ins;
last = ins;
}
}
public void insertAfter(E o, DLLNode after) {
if (after == last) {
insertLast(o);return;
}
DLLNode ins = new DLLNode(o, after, after.succ);
after.succ.pred = ins;
after.succ = ins;
}
public void insertBefore(E o, DLLNode before) {
if (before == first) {
insertFirst(o);
return;
}
DLLNode ins = new DLLNode(o, before.pred, before);
before.pred.succ = ins;
before.pred = ins;
}
public E deleteFirst() {
if (first != null) {
8/11/2019 Zadaci Za Prv Kolokvium
3/30
DLLNode 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 tmp = last;
last = last.pred;
last.succ = null;return tmp.element;
}
}
// else throw Exception
return null;
}
@Override
public String toString() {
String ret = new String();
if (first != null) {DLLNode tmp = first;
ret += tmp + "";
while (tmp.succ != null) {
tmp = tmp.succ;
ret += tmp + "";
}
} else
ret = "Prazna lista!!!";
return ret;
}
public DLLNode getFirst() {
return first;
}
public DLLNode getLast() {
return last;
}
8/11/2019 Zadaci Za Prv Kolokvium
4/30
8/11/2019 Zadaci Za Prv Kolokvium
5/30
: . ID - . , ID ID . , ID ID .: (.. ID-a) 1: .
. 2: . : DLLVojska
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class DLLNode {
protected E element;
protected DLLNode pred, succ;
public DLLNode(E elem, DLLNode pred, DLLNode succ) {
this.element = elem;
this.pred = pred;
this.succ = succ;
}
@Override
public String toString() {
return "";
}
}
class DLL {
private DLLNode 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) {
8/11/2019 Zadaci Za Prv Kolokvium
6/30
DLLNode tmp = first;
ret = 1;
while (tmp.succ != null) {
tmp = tmp.succ;
ret++;
}
return ret;
} else
return 0;
}
public void insertFirst(E o) {
DLLNode ins = new DLLNode(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 ins = new DLLNode(o, last, null);
last.succ = ins;
last = ins;
}}
public void insertAfter(E o, DLLNode after) {
if(after==last){
insertLast(o);
return;
}
DLLNode ins = new DLLNode(o, after, after.succ);
after.succ.pred = ins;
after.succ = ins;
}
public void insertBefore(E o, DLLNode before) {
if(before == first){
insertFirst(o);
return;
}
DLLNode ins = new DLLNode(o, before.pred, before);
before.pred.succ = ins;
8/11/2019 Zadaci Za Prv Kolokvium
7/30
before.pred = ins;
}
public E deleteFirst() {
if (first != null) {
DLLNode 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 tmp = last;
last = last.pred;
last.succ = null;
return tmp.element;
}
}
// else throw Exception
return null;
}
@Override
public String toString() {
String ret = new String();
if (first != null) {
DLLNode tmp = first;
ret += tmp + "";
while (tmp.succ != null) {
tmp = tmp.succ;
ret += tmp + "";
}
} else
ret = "Prazna lista!!!";
return ret;
}
public DLLNode getFirst() {
return first;
8/11/2019 Zadaci Za Prv Kolokvium
8/30
}
public DLLNode getLast() {
return last;
}
/* public void swap(int o,int p){
DLLNode tmp=first;
DLLNode tmp2=first;
while(tmp.succ!=null){
if((Integer)tmp.succ.element==o){
while(tmp.succ!=null){
if((Integer)tmp2.succ.element==p){
}
}
}
}
}*/
}
class DLLVojska {
public static void main(String[] args) throws IOException {
DLL lista = new DLL();
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String s = stdin.readLine();
int N = Integer.parseInt(s);
s = stdin.readLine();
String[] ids = s.split(" ");
for (int i = 0; i < N; i++) {
lista.insertLast(Integer.parseInt(ids[i]));
}
s = stdin.readLine();
String interval[] = s.split(" ");
int a = Integer.parseInt(interval[0]);
8/11/2019 Zadaci Za Prv Kolokvium
9/30
int b = Integer.parseInt(interval[1]);
s = stdin.readLine();
interval = s.split(" ");
int c = Integer.parseInt(interval[0]);
int d = Integer.parseInt(interval[1]);
DLL result = vojska(lista, a, b, c, d);
DLLNode node = result.getFirst();
System.out.print(node.element);
node = node.succ;
while(node != null){
System.out.print(" "+node.element);
node = node.succ;
}}
private static DLL vojska(DLL lista, int a, int b, int c, int d) {
DLLNode tmp=lista.getFirst();
DLLNode tmp2=null;
DLL novo=new DLL();
DLLNode pocetok=null;
DLLNode kraj=null;
DLLNode pocetok2=null;
DLLNode kraj2=null;
int kljuc=0;while(tmp!=null){
if(tmp.element==a) pocetok=tmp;
if(tmp.element==b) kraj=tmp;
if(tmp.element==c) pocetok2=tmp;
if(tmp.element==d) kraj2=tmp;
tmp=tmp.succ;
}
tmp=lista.getFirst();
tmp2=pocetok2;
while(tmp!=null){
if(tmp.element!=a&&tmp.element!=c){
novo.insertLast(tmp.element);
}
if(tmp.element==a){
while(tmp2.element!=d && tmp2!=null){
novo.insertLast(tmp2.element);
tmp2=tmp2.succ;
}
novo.insertLast(tmp2.element);
8/11/2019 Zadaci Za Prv Kolokvium
10/30
// kljuc=1;
tmp=kraj;
}
if(tmp.element==c){
while(pocetok.element!=b){
novo.insertLast(pocetok.element);
pocetok=pocetok.succ;
}
novo.insertLast(pocetok.element);
tmp=kraj2;
}
tmp=tmp.succ;
}
return novo;
}
}
Zad3
- Java 3 (1 / 2) . , : N 1
8/11/2019 Zadaci Za Prv Kolokvium
11/30
public void insertFirst(E o){
DLLNode tmp=new DLLNode(o, null, null);
if(first==null){
first=tmp;
last=tmp;
}
else if(first!=null){
tmp.succ=first;
first.pred=tmp;
first=tmp;
}
}
public void insertLast(E o){
DLLNode tmp=new DLLNode(o, null, last);
if(first==null){
insertFirst(o);
}else{
tmp.pred=last;
last.succ=tmp;
tmp.succ=null;
last=tmp;
}
}
public void pecati(){
DLLNode tmp=first;
while(tmp!=null){System.out.print(tmp.element);
System.out.print("");
tmp=tmp.succ;
}
System.out.println();
}
public Iterator iterator () {
// vraka iterator koj gi posetuva site elementi na listata od levo na desno
return new LRIterator();
}
private class LRIterator implements Iterator {
private DLLNode prev,curr,place;
public LRIterator(){
place=(DLLNode)first;
curr=null;
}
public boolean hasNext() {
8/11/2019 Zadaci Za Prv Kolokvium
12/30
return (place != null);
}
public E next(){
//if(place==null) throw new NoSuchFieldException();
E nextelem =place.element;
curr=place;
place=place.succ;
return nextelem;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
class ListaOdListi {public static long findMagicNumber(DLL list) {
int suma=0;
long proizvod=1;
DLLNode tmp=list.first;
while(tmp!=null){
suma=0;
DLLNode tmp2=tmp.element.first;
while(tmp2!=null){
suma+=tmp2.element;
tmp2=tmp2.succ;
}proizvod*=suma;
tmp=tmp.succ;
}
return proizvod;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
DLL list = new DLL();
for (int i = 0; i < n; i++) {
DLL tmp = new DLL();
for (int j = 0; j < m; j++) {
tmp.insertLast(in.nextInt());
}
8/11/2019 Zadaci Za Prv Kolokvium
13/30
list.insertLast(tmp);
}
in.close();
System.out.println(findMagicNumber(list));
}
}
Zad4
- Java 4 (2 / 24) : [ x^2+s(x)+200\cdot x=N ] (x), (N) , (s(x)) (x). (N) (A) (B), (A\leq B) (A, B\leq 1,000,000,000). (x) ([A, B]) , . (x) ([A, B]) , -1. : Range
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
class Range {
static long zbir_na_cifri(long n) {
long c = 0, suma = 0;
while (n != 0) {
c = n % 10;
suma = suma + c;
n /= 10;
}return suma;
}
/* static long proveri(long N, long A, long B) {
long X = 0, i;
for (i = A; i < B; i++) {
long c = 0, suma = 0;
long n=i;
while (n != 0) {
c = n % 10;
suma = suma + c;n /= 10;
}
X = i * i + suma + 200 * i;
if (X == N) {
break;
}
// System.out.println("\t"+i);
8/11/2019 Zadaci Za Prv Kolokvium
14/30
}
if(i==B) return -1;
else return i;
}*/
/* static long proveripola(long N, long A, long B) {
long X = 0, i;
for (i = A; i < B; i++) {
long c = 0, suma = 0;
long n=i;
while (n != 0) {
c = n % 10;
suma = suma + c;
n /= 10;
}
X = i * i + suma + 200 * i;
if (X == N) {
break;}
}
if(i==B) return -1;
else return i;
}
static long proveri(long N, long A, long B,int abs) {
long pola;
// if(BN&&abs==0){//B/2>1milion
proveri(N,A,B/2,0);
}
while(pola
8/11/2019 Zadaci Za Prv Kolokvium
15/30
long l,d,mid;
l=A;
d=B;
mid = (l + d) / 2;
pola=(mid*mid)+zbir_na_cifri(mid)+200*mid;
int vlez=0;
if(pola==N){
// System.out.println(proveripola(N, l, d));
System.out.println(mid);
// return mid;
// vlez=1;
return ;
}
if(A==B){
System.out.println("-1");
// return -1;
// vlez=1;return ;
}
if(pola>N){
proveri(N,l,mid);
}
else if(pola
8/11/2019 Zadaci Za Prv Kolokvium
16/30
N M . 100 . k , k -1 ( ). , , . . N.
M. . . : Bus
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Bus {
public static void main(String[] args) throws Exception {
int i,j,k;
int minSuma=0;int maxSuma=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int M = Integer.parseInt(br.readLine());
br.close();
if(M==0) maxSuma=N*100;
else maxSuma=(N+M-1)*100;if(N==M)
minSuma=N*100;
if(N>M)
minSuma=N*100;
else
minSuma=M*100;
System.out.println(minSuma);
System.out.println(maxSuma);
}
}
Zad6
- Java 6 (3 / 6) . .
8/11/2019 Zadaci Za Prv Kolokvium
17/30
: LDS
import java.util.Scanner;
class LDS {
private static int najdolgaOpagackaSekvenca(int[] a) {
int[] b=new int[a.length];
int max=1;
for(int i=0;i
8/11/2019 Zadaci Za Prv Kolokvium
18/30
interface Stack {
// Elementi na stekot se objekti od proizvolen tip.
// Metodi za pristap:
public boolean isEmpty ();
// Vrakja true ako i samo ako stekot e prazen.
public E peek ();
// Go vrakja elementot na vrvot od stekot.
// Metodi za transformacija:
public void clear ();
// Go prazni stekot.
public void push (E x);
// Go dodava x na vrvot na stekot.
public E pop ();
// Go otstranuva i vrakja elementot shto e na vrvot na stekot.
}
class ArrayStack implements Stack {
private E[] elems;
private int depth;
@SuppressWarnings("unchecked")
public ArrayStack (int maxDepth) {
// Konstrukcija na nov, prazen stek.
elems = (E[]) new Object[maxDepth];
depth = 0;
}
public boolean isEmpty () {
// Vrakja true ako i samo ako stekot e prazen.
return (depth == 0);
}
public E peek () {
// Go vrakja elementot na vrvot od stekot.
return elems[depth-1];
}
8/11/2019 Zadaci Za Prv Kolokvium
19/30
public void clear () {
// Go prazni stekot.
for (int i = 0; i < depth; i++) elems[i] = null;
depth = 0;
}
public void push (E x) {
// Go dodava x na vrvot na stekot.
elems[depth++] = x;
}
public E pop () {
// Go otstranuva i vrakja elementot shto e na vrvot na stekot.
E topmost = elems[--depth];
elems[depth] = null;
return topmost;
}
}
public class StackBukvi {
static int proveri_t_posle_s(char [] St)
{
ArrayStack stek=new ArrayStack(St.length);
for(int i=0;i
8/11/2019 Zadaci Za Prv Kolokvium
20/30
}
public static void main(String[] args) throws IOException {
char [] niza=new char[100];
Scanner f=new Scanner(System.in);
String st=f.next();
niza=st.toCharArray();
int rez= proveri_t_posle_s(niza);
System.out.println(rez);
}
}
Zad8
- Java 8 (2 / 7) () (+) (*).: . : ExpressionEvaluator
//package mathizraz;
import java.io.BufferedReader;
import java.util.Scanner;
interface Stack {
// Elementi na stekot se objekti od proizvolen tip.
// Metodi za pristap:
public boolean isEmpty ();
// Vrakja true ako i samo ako stekot e prazen.
public E peek ();
// Go vrakja elementot na vrvot od stekot.
// Metodi za transformacija:
public void clear ();
// Go prazni stekot.
public void push (E x);
8/11/2019 Zadaci Za Prv Kolokvium
21/30
// Go dodava x na vrvot na stekot.
public E pop ();
// Go otstranuva i vrakja elementot shto e na vrvot na stekot.
}
class ArrayStack implements Stack {
private E[] elems;
private int depth;
@SuppressWarnings("unchecked")
public ArrayStack (int maxDepth) {
// Konstrukcija na nov, prazen stek.
elems = (E[]) new Object[maxDepth];
depth = 0;
}
public boolean isEmpty () {
// Vrakja true ako i samo ako stekot e prazen.
return (depth == 0);
}
public E peek () {
// Go vrakja elementot na vrvot od stekot.
return elems[depth-1];
}
public void clear () {
// Go prazni stekot.
for (int i = 0; i < depth; i++) elems[i] = null;
depth = 0;
}
public void push (E x) {
// Go dodava x na vrvot na stekot.
elems[depth++] = x;
}
public E pop () {
// Go otstranuva i vrakja elementot shto e na vrvot na stekot.
E topmost = elems[--depth];
8/11/2019 Zadaci Za Prv Kolokvium
22/30
elems[depth] = null;
return topmost;
}
}
class ExpressionEvaluator {
public static int evaluateExpression(char[] a){
ArrayStack stek=new ArrayStack(a.length);
ArrayStack stek2=new ArrayStack(a.length);
int m=0;
int vlez=0;
int mnozenje=0;
for(int i=0;i
8/11/2019 Zadaci Za Prv Kolokvium
23/30
int proizvod=1;
while(!stek2.isEmpty()){
vkupno+=stek2.pop();
}
while(!stek.isEmpty()){
vkupno+=stek.pop();
}
return vkupno;
}
public static void main(String[] args) {
Scanner f=new Scanner(System.in);
char [] niza=new char[100];
String st=f.next();
niza=st.toCharArray();
System.out.println(evaluateExpression(niza));
}
}
Zad9
- Java 9 (1 / 1) . 51- ( ) . , , , . , , (. 1 2 3 4 5 6 7 7 6 5 4 3 2 1),
, . . , , N- .. 1
8/11/2019 Zadaci Za Prv Kolokvium
24/30
public int size () {
return length;
}
public E peek () {
if (length > 0)
return elems[front];
else{
System.out.println("Redicata e prazna");
return null;
}
}
public void clear () {
length = 0;
front = rear = 0;
}
public void enqueue (E x) {
elems[rear++] = x;
if (rear == elems.length) rear = 0;length++;
}
public E dequeue () {
if (length > 0) {
E frontmost = elems[front];
elems[front++] = null;
if (front == elems.length) front = 0;
length--;
return frontmost;
} else{
System.out.println("Redicata e prazna");return null;
}
}
}
interface Stack{
public void clear();
public E pop();
public void push(E x);
public E peek();
public boolean isEmpty();
}
class ArrayStack implements Stack {
protected E[] elem;
protected int depth;
public ArrayStack(int golemina){
elem=(E[])new Object[golemina];
depth=0;
8/11/2019 Zadaci Za Prv Kolokvium
25/30
}
@Override
public void clear() {
depth=0;
}
@Override
public E pop() {
E element;
if(depth==0){
throw new NoSuchElementException("nema takov elem.");
}
else{
element=elem[depth-1];
elem[depth-1]=null;depth--;
}
return element;
}
@Override
public void push(E x) {
if(depth==elem.length){
System.out.println("Listata e polna");
}else {
elem[depth]=x;
depth++;
}
}
@Override
public E peek() {
E element;
if(depth==0){
throw new NoSuchElementException("nema takov elem.");
}
else{
element=elem[depth-1];
}
return element;
}
@Override
8/11/2019 Zadaci Za Prv Kolokvium
26/30
public boolean isEmpty() {
if(depth==0) return true;
else return false;
}
/* static ArrayQueue mesaj(ArrayQueue spil){
int x,y;
ArrayStack stekpomosan=new ArrayStack(7);
for(int i=0;i
8/11/2019 Zadaci Za Prv Kolokvium
27/30
}
}
}
Zad10
- Java 10 (1 / 3) . ID . , ID -. , ID . (ID, ) ID . : nema : SLLKompanija
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class SLLNode {
protected int id;
protected int plata;
protected SLLNode succ;
public SLLNode(int id,int plata, SLLNode succ) {
this.id = id;
this.plata=plata;
this.succ = succ;}
}
class SLL {
private SLLNode first;
public SLL() {
this.first = null;
}
public void deleteList() {
first = null;
}
public int length() {
int ret;
if (first != null) {
SLLNode tmp = first;
ret = 1;
while (tmp.succ != null) {
tmp = tmp.succ;
ret++;
8/11/2019 Zadaci Za Prv Kolokvium
28/30
8/11/2019 Zadaci Za Prv Kolokvium
29/30
tmp2=tmp.succ;
PLATA=tmp.plata;
maxID=tmp.id;
kljuc=0;
while(tmp2!=null){
if(maxID
8/11/2019 Zadaci Za Prv Kolokvium
30/30
s = stdin.readLine();
lista1=lista1.brisi_pomali_od(Integer.parseInt(s));
SLLNode tmp=lista1.getFirst();
if(tmp==null) System.out.println("nema");
else if(tmp!=null)
{
lista1=lista1.sortiraj_opagacki();
lista1.pecati(lista1);
}
}
}