View Code of Problem 10

#include<stdio.h>
int main(){
	int n;
	scanf("%d",&n);
	while(n--){
	int a[4];
	for(int i=0;i<4;i++){
		scanf("%d",&a[i]);
	}  
	//2 2 2 2 ---->2 2 0 4
	for(int i=3;i>=0;i--){
		for(int j=i-1;j>=0;j--){//思想 先遍历数组  
			if(a[j]==a[i]){  // 2 2 2 2----》0 4 0 4 
				a[i]=2*a[i];
				a[j]=0;
			} 
		}	
	}
	//只要遇到0  且0前面是非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;
				}
			} 
			
			
		}
	}
	
	
	for(int i=0;i<=2;i++)
		printf("%d ",a[i]);
		
	printf("%d\n",a[3]);
		
	} 
	
	return 0;
} 

Double click to view unformatted code.


Back to problem 10