View Code of Problem 10

#include<iostream>
using namespace std;
int main() {
	int n;
	cin >> n;
	while (n--) {
		int a[4], j=-1;

		for (int i = 0; i <4; i++)
		{
			cin >> a[i];
		}
		for (int i = 3; i >=0; i--)//向右相加合并
		{
			if (i!=0&&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&&j<i) {
				j = i;
			}
			if (i != j && a[i] != 0&&j!=-1) {
				a[j] = a[i];
				a[i] = 0;
				j = i;
			}
			
			
		}
		for (int i = 0; i < 4; i++)//输出
		{
			cout << a[i];
			if (i != 3)cout << " ";
			else cout << endl;
		}
	}
}

Double click to view unformatted code.


Back to problem 10