Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3. import java.util.LinkedList;
  4.  
  5. class IntegerList {
  6. LinkedList<Integer> list;
  7.  
  8. public IntegerList() {
  9. list = new LinkedList<>();
  10. }
  11.  
  12. public IntegerList(Integer... numebrs) {
  13. this();
  14. for(Integer i : numebrs) {
  15. list.add(i);
  16. }
  17. }
  18.  
  19. public void add(int el, int idx) {
  20. if(idx > list.size()) {
  21. int n = idx - list.size();
  22. for(int i = 0; i < n; ++i) {
  23. list.add(0);
  24. }
  25. list.add(el);
  26. } else {
  27. list.add(idx, el);
  28. }
  29. }
  30.  
  31. public void remove(int idx) {
  32. list.remove(idx);
  33. }
  34.  
  35. public void set(int el, int idx) {
  36. list.set(idx, el);
  37. }
  38.  
  39. public int get(int idx) {
  40. return list.get(idx);
  41. }
  42.  
  43. public int size() {
  44. return list.size();
  45. }
  46.  
  47. public int count(int el) {
  48. int tmp = 0;
  49. for(int i : list) {
  50. if(i == el)
  51. ++tmp;
  52. }
  53. return tmp;
  54. }
  55.  
  56. public void removeDuplicates() {
  57. LinkedList<Integer> tmp = list;
  58. list = new LinkedList<>();
  59. for(int i = tmp.size() - 1; i >= 0; --i) {
  60. if(!list.contains(tmp.get(i))) {
  61. list.addFirst(tmp.get(i));
  62. }
  63. }
  64. }
  65.  
  66. public int sumFirst(int k) {
  67. int sum = 0;
  68. for(int i = 0; i < k&&i < list.size(); ++i) {
  69. sum += list.get(i);
  70. }
  71. return sum;
  72. }
  73.  
  74. public int sumLast(int k) {
  75. int sum = 0;
  76. for(int i = 0; i < k; ++i) {
  77. sum += list.get(list.size() - i - 1);
  78. }
  79. return sum;
  80. }
  81.  
  82. public void shiftRight(int idx, int k) {
  83. int indeks = (idx + k) % list.size();
  84. int el = list.get(idx);
  85. list.remove(idx);
  86. list.add(indeks, el);
  87. /*for(int i = 0; i < k; ++i) {
  88. int idk1 = (idx + i) % list.size();
  89. int idk2 = (idx + i + 1) % list.size();
  90. int tmp1 = list.get(idk1);
  91. int tmp2 = list.get(idk2);
  92. list.set(idk1, tmp2);
  93. list.set(idk2, tmp1);
  94. }*/
  95. }
  96.  
  97. public void shiftLeft(int idx, int k) {
  98. int indeks =(idx - k) % list.size();
  99. if(indeks < 0) {
  100. indeks = list.size() + indeks;
  101. }
  102. int el = list.get(idx);
  103. list.remove(idx);
  104. list.add(indeks, el);
  105. }
  106.  
  107. public IntegerList addValue(int value) {
  108. IntegerList tmp = new IntegerList();
  109. for(int i = 0; i < list.size(); ++i) {
  110. tmp.add(list.get(i) + value, i);
  111. }
  112. return tmp;
  113. }
  114. }
  115.  
  116. public class IntegerListTest {
  117.  
  118. public static void main(String[] args) {
  119. Scanner jin = new Scanner(System.in);
  120. int k = jin.nextInt();
  121. if ( k == 0 ) { //test standard methods
  122. int subtest = jin.nextInt();
  123. if ( subtest == 0 ) {
  124. IntegerList list = new IntegerList();
  125. while ( true ) {
  126. int num = jin.nextInt();
  127. if ( num == 0 ) {
  128. list.add(jin.nextInt(), jin.nextInt());
  129. }
  130. if ( num == 1 ) {
  131. list.remove(jin.nextInt());
  132. }
  133. if ( num == 2 ) {
  134. print(list);
  135. }
  136. if ( num == 3 ) {
  137. break;
  138. }
  139. }
  140. }
  141. if ( subtest == 1 ) {
  142. int n = jin.nextInt();
  143. Integer a[] = new Integer[n];
  144. for ( int i = 0 ; i < n ; ++i ) {
  145. a[i] = jin.nextInt();
  146. }
  147. IntegerList list = new IntegerList(a);
  148. print(list);
  149. }
  150. }
  151. if ( k == 1 ) { //test count,remove duplicates, addValue
  152. int n = jin.nextInt();
  153. Integer a[] = new Integer[n];
  154. for ( int i = 0 ; i < n ; ++i ) {
  155. a[i] = jin.nextInt();
  156. }
  157. IntegerList list = new IntegerList(a);
  158. while ( true ) {
  159. int num = jin.nextInt();
  160. if ( num == 0 ) { //count
  161. System.out.println(list.count(jin.nextInt()));
  162. }
  163. if ( num == 1 ) {
  164. list.removeDuplicates();
  165. }
  166. if ( num == 2 ) {
  167. print(list.addValue(jin.nextInt()));
  168. }
  169. if ( num == 3 ) {
  170. list.add(jin.nextInt(), jin.nextInt());
  171. }
  172. if ( num == 4 ) {
  173. print(list);
  174. }
  175. if ( num == 5 ) {
  176. break;
  177. }
  178. }
  179. }
  180. if ( k == 2 ) { //test shiftRight, shiftLeft, sumFirst , sumLast
  181. int n = jin.nextInt();
  182. Integer a[] = new Integer[n];
  183. for ( int i = 0 ; i < n ; ++i ) {
  184. a[i] = jin.nextInt();
  185. }
  186. IntegerList list = new IntegerList(a);
  187. while ( true ) {
  188. int num = jin.nextInt();
  189. if ( num == 0 ) { //count
  190. list.shiftLeft(jin.nextInt(), jin.nextInt());
  191. }
  192. if ( num == 1 ) {
  193. list.shiftRight(jin.nextInt(), jin.nextInt());
  194. }
  195. if ( num == 2 ) {
  196. System.out.println(list.sumFirst(jin.nextInt()));
  197. }
  198. if ( num == 3 ) {
  199. System.out.println(list.sumLast(jin.nextInt()));
  200. }
  201. if ( num == 4 ) {
  202. print(list);
  203. }
  204. if ( num == 5 ) {
  205. break;
  206. }
  207. }
  208. }
  209. }
  210.  
  211. public static void print(IntegerList il) {
  212. if ( il.size() == 0 ) System.out.print("EMPTY");
  213. for ( int i = 0 ; i < il.size() ; ++i ) {
  214. if ( i > 0 ) System.out.print(" ");
  215. System.out.print(il.get(i));
  216. }
  217. System.out.println();
  218. }
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement