UVA 11462 Age Sort 计数排序 .

本文提供了一种解决UVA-11462问题的有效算法实现,通过使用计数数组来统计输入数据,并以升序方式输出这些数据。文中包括了两种不同的C++代码实现方案,一种采用标准输入输出,另一种则优化了整数的读写过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址:http://vjudge.net/problem/UVA-11462

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<=(b);++i)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
const int maxn=100+5;
int c[maxn];
int main(int argc, char const *argv[])
{
	int n;
	while(scanf("%d",&n)==1&&n){
		REP(i,1,n){
			int x;
			scanf("%d",&x);
			c[x]++;
		}
		bool first=true;
		REP(i,1,100) REP(j,1,c[i]) {
			if(!first) putchar(' ');
			first=false;
			printf("%d", i);
		}
		putchar('\n');
		memset(c,0,sizeof(c));
	}
	return 0;
}



开外挂如下:

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b)  for(int i=a;i<=(b);++i)
#define REPD(i,a,b) for(int i=a;i>=(b);--i)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
const int maxn=100+5;
int c[maxn];
int inline readInt(){
	char ch;
	do{ch=getchar();}while(!isdigit(ch));
	int x=0;
	do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
	return x;
}
int buf[10];
int inline printInt(int n){
	int p=0;
	if(n==0) p++;
	while(n){buf[p++]=n%10;n/=10;}
	REPD(i,p-1,0) putchar('0'+buf[i]);
}
int main(int argc, char const *argv[])
{
	int n;
	while(n=readInt()){
		REP(i,1,n) c[readInt()]++;
		bool first=true;
		REP(i,1,100) REP(j,1,c[i]) {
			if(!first) putchar(' ');
			first=false;
			printInt(i);
		}
		putchar('\n');
		memset(c,0,sizeof(c));
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值