View Code of Problem 10

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        while (n-- > 0) {
            String[] strings = scanner.nextLine().split(" ");
            int[] ints = new int[4];
            Arrays.fill(ints,0);
            int l=3;
            for (int i = 3; i >= 0; i--) {
                int t=Integer.parseInt(strings[i]);
                if(t!=0){
                    ints[l--] = t;
                }
            }
            for(int i=3;i>0;i--) {
                if(ints[i]==ints[i-1]){
                    ints[i]+=ints[i-1];
                    ints[i-1]=0;
                }
            }
            int[] ans=new int[4];
            l=3;
            for (int i = 3; i >= 0; i--) {
                int t=ints[i];
                if(t!=0){
                    ans[l--] = t;
                }
            }
            for (int i = 0; i < 4; i++) {
                if (i == 0) {
                    System.out.print(ans[i]);
                } else {
                    System.out.print(" " + ans[i]);
                }
            }
            System.out.println();
        }
    }
}

Double click to view unformatted code.


Back to problem 10