View Code of Problem 10

#include<iostream>
using namespace std;
int main(void){
	int n;
	cin>>n;
	while(n--){
		int a[4],k=0;
		cin>>a[0]>>a[1]>>a[2]>>a[3];
		for(int j=3;j>=0;j--){
			if(a[j]==0){
				k++;
			}else if(a[j]!=0&&k!=0){
				a[j+k]=a[j];
				a[j]=0;
			}
		}
		for(int j=3;j>=0;j--){
			if(a[j]==a[j-1]){
				a[j]*=2;
				a[j-1]=0;
				for(k=j-1;k>=0;k--){
					a[k]=a[k-1];
					a[k-1]=0;
				}
			}
		}
		printf("%d %d %d %d\n",a[0],a[1],a[2],a[3]);
	}
}

Double click to view unformatted code.


Back to problem 10