View Code of Problem 10

#include <stdio.h>
int main()
{
	int n,i,j;
	scanf("%d", &n);
	while (n--)
	{
		int a[4];
		for (i = 0; i < 4; i++)
			scanf("%d", &a[i]);
		for (i = 3; i > 0; i--)
			for (j = i - 1; j >= 0; j--)
				if (a[i] == 0 && a[j] != 0)
				{
					a[i] = a[j];
					a[j] = 0;
				}
		for(i=3;i>0;i--)
			if (a[i] == a[i - 1] && a[i] != 0)
			{
				a[i] *= 2;
				a[i - 1] = 0;
			}
		for(i=3;i>0;i--)
			for(j=i-1;j>=0;j--)
				if (a[i] == 0 && a[j] != 0)
				{
					a[i] = a[j];
					a[j] = 0;
				}
		for (i = 0; i < 4; i++)
		{
			printf("%d", a[i]);
			if (i == 3)
				printf("\n");
			else
				printf(" ");
		}
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 10