Solução: Problema das seqüências alternadas (Eduardo Ramos)

From AdonaiMedrado.Pro.Br
Revision as of 17:46, 5 June 2009 by 200.17.147.2 (Talk) (New page: <code lang="c"> #include <stdio.h> #include <stdlib.h> int comparar(const void *a, const void *b) { return *(int *)a-*(int *)b; } int compararInverso(const void *a, const void *b) { ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#include <stdio.h>
#include <stdlib.h>
 
int comparar(const void *a, const void *b)
{
	return *(int *)a-*(int *)b;
}
 
int compararInverso(const void *a, const void *b)
{
	return *(int *)b-*(int *)a;
}
 
int main(void) {
 
	int n,seq;
	scanf("%d",&n);
 
	while (n-- > 0) {
		scanf("%d",&seq);
		int i, v[seq];
		for(i=0;i<seq;i++)
			scanf("%d",v+i);
 
		if (n % 2 != 0) 
			qsort(v,seq,sizeof(int),comparar);
		else
			qsort(v,seq,sizeof(int),compararInverso);
 
		for(i=0;i<seq;i++)
			printf("%d ",*(v+i));
		printf("\n");
	}
 
	return 0;
}