一、第二届中国计量大学ACM程序设计竞赛个人赛(同步赛)
1.Little Gyro and Sort
题意:
Little Gyro has just found an integer sequence a1,a2,…,an in his right pocket. As Little Gyro is bored, he decides to sort the sequence.
Now given an integer sequence, and its size is no more than 10^6, please sort it within the ascending order.
输入:
There are multiple test cases. The first line of the input contains an integer T (1 ≤ T ≤ 10), indicating the number of test cases. For each test case:The first line contains an integer n (1 ≤ n ≤ 10^6 ), indicating the length of the sequence.
The second line contains n integers a1,a2,…,an (1 ≤ ai≤ 10^4), indicating the given sequence.
输出:
For each test case output the sequence after sorting.
Note: Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect!
样例输入:
2
5
1 4 8 3 7
6
10000 9999 9998 9997 9996 9995
样例输出:
1 3 4 7 8
9995 9996 9997 9998 9999 10000
解题思路:
刚开始用的sort,超时了;用了冒泡和选择排序,都超时了;后来用了桶排序,结果过了。果然不能想太多。
程序代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e6;
int a[N+5];
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(a,0,sizeof(a));
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
int temp;
scanf("%d",&temp);
a[temp]++;
}
int cnt=0;
for(int i=1;i<=N;i++){
if(a[i