Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.Scanner;
- class ArrayIndexOutOfBoundsException extends Exception{
- public ArrayIndexOutOfBoundsException(){
- super();
- }
- }
- class IntegerList{
- LinkedList<Integer> list;
- public IntegerList(){
- list = new LinkedList<Integer>();
- }
- public IntegerList(Integer[] niza){
- list = new LinkedList<Integer>();
- for(int i=0;i<niza.length;i++){
- list.add(niza[i]);
- }
- }
- public void add(int broj, int index){
- list.add(index, broj);
- }
- public void remove(int index) throws ArrayIndexOutOfBoundsException{
- if (index>list.size()-1 || index<0) throw new ArrayIndexOutOfBoundsException();
- list.remove(index);
- }
- public void set(int broj, int index) throws ArrayIndexOutOfBoundsException{
- if (index>list.size()-1 || index<0) throw new ArrayIndexOutOfBoundsException();
- list.set(index, broj);
- }
- public int get(int index) throws ArrayIndexOutOfBoundsException{
- if (index>list.size()-1 || index<0) throw new ArrayIndexOutOfBoundsException();
- return list.get(index);
- }
- public int count(int broj){
- int brojac = 0;
- for(int i=0;i<list.size();i++){
- if(list.get(i)==broj) brojac++;
- }
- return brojac;
- }
- public void removeDuplicates(){
- for(int i=list.size()-1;i>0;i--){
- for(int j=i-1;j>=0;j--){
- if(list.get(j)==list.get(i)) list.remove(j);
- }
- }
- }
- public int size(){
- return list.size();
- }
- public int sumFirst(int k) throws ArrayIndexOutOfBoundsException{
- if (k>list.size()-1 || k<0) throw new ArrayIndexOutOfBoundsException();
- int suma = 0;
- for(int i=0;i<k;i++){
- suma+=list.get(i);
- }
- return suma;
- }
- public int sumLast(int k) throws ArrayIndexOutOfBoundsException{
- if (k>list.size()-1 || k<0) throw new ArrayIndexOutOfBoundsException();
- int suma = 0;
- for(int i=list.size()-1;i>list.size()-1-k;i--){
- suma+=list.get(i);
- }
- return suma;
- }
- public IntegerList addValue(int n){
- IntegerList nov = new IntegerList();
- for(int i=0;i<list.size();i++){
- nov.add(i, list.get(i)+n);
- }
- return nov;
- }
- public void shiftRight(int index, int k){
- if(index+k<=list.size()-1){
- list.add(index+k, list.get(index));
- list.remove(index);
- }
- else{
- int novapozicija = (index+k)%list.size();
- list.add(novapozicija, list.get(index));
- if(novapozicija<=index) list.remove(index+1);
- else list.remove(index);
- }
- }
- public void shiftLeft(int index, int k){
- if(k<=list.size()){
- shiftRight(index, k-list.size());
- }
- else{
- shiftRight(index, k%list.size());
- }
- }
- }
- public class IntegerListTest {
- public static void main(String[] args) {
- Scanner jin = new Scanner(System.in);
- int k = jin.nextInt();
- if ( k == 0 ) { //test standard methods
- int subtest = jin.nextInt();
- if ( subtest == 0 ) {
- IntegerList list = new IntegerList();
- while ( true ) {
- int num = jin.nextInt();
- if ( num == 0 ) {
- list.add(jin.nextInt(), jin.nextInt());
- }
- if ( num == 1 ) {
- try {
- list.remove(jin.nextInt());
- } catch (ArrayIndexOutOfBoundsException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if ( num == 2 ) {
- print(list);
- }
- if ( num == 3 ) {
- break;
- }
- }
- }
- if ( subtest == 1 ) {
- int n = jin.nextInt();
- Integer a[] = new Integer[n];
- for ( int i = 0 ; i < n ; ++i ) {
- a[i] = jin.nextInt();
- }
- IntegerList list = new IntegerList(a);
- print(list);
- }
- }
- if ( k == 1 ) { //test count,remove duplicates, addValue
- int n = jin.nextInt();
- Integer a[] = new Integer[n];
- for ( int i = 0 ; i < n ; ++i ) {
- a[i] = jin.nextInt();
- }
- IntegerList list = new IntegerList(a);
- while ( true ) {
- int num = jin.nextInt();
- if ( num == 0 ) { //count
- System.out.println(list.count(jin.nextInt()));
- }
- if ( num == 1 ) {
- list.removeDuplicates();
- }
- if ( num == 2 ) {
- print(list.addValue(jin.nextInt()));
- }
- if ( num == 3 ) {
- list.add(jin.nextInt(), jin.nextInt());
- }
- if ( num == 4 ) {
- print(list);
- }
- if ( num == 5 ) {
- break;
- }
- }
- }
- if ( k == 2 ) { //test shiftRight, shiftLeft, sumFirst , sumLast
- int n = jin.nextInt();
- Integer a[] = new Integer[n];
- for ( int i = 0 ; i < n ; ++i ) {
- a[i] = jin.nextInt();
- }
- IntegerList list = new IntegerList(a);
- while ( true ) {
- int num = jin.nextInt();
- if ( num == 0 ) { //count
- list.shiftLeft(jin.nextInt(), jin.nextInt());
- }
- if ( num == 1 ) {
- list.shiftRight(jin.nextInt(), jin.nextInt());
- }
- if ( num == 2 ) {
- try {
- System.out.println(list.sumFirst(jin.nextInt()));
- } catch (ArrayIndexOutOfBoundsException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if ( num == 3 ) {
- try {
- System.out.println(list.sumLast(jin.nextInt()));
- } catch (ArrayIndexOutOfBoundsException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if ( num == 4 ) {
- print(list);
- }
- if ( num == 5 ) {
- break;
- }
- }
- }
- }
- public static void print(IntegerList il) {
- if ( il.size() == 0 ) System.out.print("EMPTY");
- for ( int i = 0 ; i < il.size() ; ++i ) {
- if ( i > 0 ) System.out.print(" ");
- try {
- System.out.print(il.get(i));
- } catch (ArrayIndexOutOfBoundsException e) {
- System.out.println("ArrayIndexOutOfBoundsException");
- }
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement