计数排序



#include<stdio.h>
#include<string.h>
int nTestCase[300001];
int nNumbers[32001];
int main()
{
	freopen("sample_input.txt","r",stdin);

	int i, j, test, currrank, n, max;
	scanf("%d", &test);
	while (test--){
		currrank = 1; max = 0;
		scanf("%d", &n);
		memset(nNumbers, 0, 32000 * sizeof(int));
		for (i = 1; i <= n; i++){
			scanf("%d", &nTestCase[i]);
			nNumbers[nTestCase[i]]++;
			max = max<nTestCase[i] ?

nTestCase[i] : max; } for (i = max; i>0; i--){ if (nNumbers[i]){ j = nNumbers[i]; nNumbers[i] = currrank; currrank += j; } } for (i = 1; i <= n; i++) printf("%d ", nNumbers[nTestCase[i]]); printf(" "); } return 0; }



原文地址:https://www.cnblogs.com/gavanwanggw/p/7221007.html