View Code of Problem 10

#include<iostream>
#include<algorithm>
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 >=1; i--)//向右相加合并
		{
			for (int k = i-1; k >=0; k--)
			{
				if (a[i] == a[k]) {
					a[i] = 2 * a[i];
					a[k] = 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