View Code of Problem 10

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <iomanip>
#include<vector>
#include<math.h>
using namespace std;
int main()
{
	int n;
	int a[4];
	cin >> n;
	while (n--) {
		for (int i = 0; i < 4; i++) {
			cin >> a[i];
		}
		for (int i = 3; i > 0; i--) {
			if (a[i] == 0) {
				for (int j = i - 1; j >= 0; j--) {
					if (a[j] > 0) {
						a[i] = a[j];
						a[j] = 0;
						i--;
					}
				}
			}
		}
		for (int i = 3; i > 0; i--) {
				if (a[i] == a[i-1]) {
					a[i] = 2 * a[i];
					a[i-1] = 0;
				}
		}
			for (int i = 3; i > 0; i--) {
				if (a[i] == 0) {
					for (int j = i - 1; j >= 0; j--) {
						if (a[j] > 0) {
							a[i] = a[j];
							a[j] = 0;
							i--;
						}
					}
				}
			}
			for (int i = 0; i < 4; i++) {
				if (i == 3)
					cout << a[i] << endl;
				else
					cout << a[i] << " ";
			}
		}
}

Double click to view unformatted code.


Back to problem 10