Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.List;
- class SLLNode {
- public int val;
- public SLLNode succ = null;
- SLLNode(int x) {
- val = x;
- }
- }
- class SLL {
- SLLNode first = null;
- SLLNode last = null;
- SLLNode getFirst() {
- return first;
- }
- SLLNode getLast() {
- return last;
- }
- boolean isEmpty() {
- return (first == null && last == null);
- }
- void addLast(int x) {
- SLLNode new_last = new SLLNode(x);
- if (isEmpty()) {
- first = new_last;
- last = new_last;
- return;
- }
- last.succ = new_last;
- last = new_last;
- }
- void print() {
- SLLNode current = first;
- while (current != null) {
- System.out.print(current.val + " ");
- current = current.succ;
- }
- }
- }
- public class Decading {
- public static ArrayList<SLL> split(SLL list, int N) {
- ArrayList<SLL> result = new ArrayList<SLL>();
- int every = N / 10;
- int first_exceptions = N % 10;
- SLLNode current = list.getFirst();
- for (int i = 0; i < 10; i++) { // for each of the 10 result lists
- result.add(new SLL()); // I add an empty list
- // 23 -> every = 2, first_excep = 3
- // 0 1 2 3 4 5 6 7 8
- // x x x . . . . . .
- // x x x . . . . . .
- // fill the i-th list
- for (int j = 0; j < every + (i < first_exceptions ? 1 : 0); j++) {
- result.get(i).addLast(current.val);
- current = current.succ;
- }
- }
- return result;
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int N = Integer.parseInt(scanner.nextLine());
- SLL list = new SLL();
- String[] elements = scanner.nextLine().split(" ");
- for (int i = 0; i < N; i++) {
- int x = Integer.parseInt(elements[i]);
- list.addLast(x);
- }
- ArrayList<SLL> splitted = split(list, N);
- for (int i = 0; i < splitted.size(); i++) {
- System.out.print("[");
- splitted.get(i).print();
- System.out.println("]");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement