Age Sort UVA - 11462 年龄排序 计数排序

版权声明:本文为博主原创文章,未经博主允许不得转载,欢迎添加友链。 https://blog.csdn.net/qq_42835910/article/details/89791313

You are given the ages (in years) of all people of a country with at least 1 year of age. You know that no individual in that country lives for 100 or more years. Now, you are given a very simple task of sorting all the ages in ascending order.

分析:数据太大,内存限制太紧。但是数据范围小,可以用计数排序。

#include <cstdio>
#include <cstring>
int ages[105];
int main(int argc, char** argv) {
	int n;
	while( ~scanf("%d",&n) && n){
		memset(ages, 0, sizeof(ages));
		for(int i = 0; i < n; i++){
			int x;
			scanf("%d",&x);
			ages[x]++;
		}
		bool flag = false;
		for(int i = 0; i < 100; i++){
			for(int j = 0; j < ages[i]; j++){
				if(!flag){
					flag = true;
					printf("%d",i);
					continue;
				}
				printf(" %d",i);
			}
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42835910/article/details/89791313
今日推荐