地址
http://acm.hdu.edu.cn/showproblem.php?pid=1040
定位
水题
排序
分析
- 最简单的排序,快排直接过。
代码
#include <stdio.h>
#include <stdlib.h>
void Qsort(int a[],int left,int right)
{
int i = left;
int j = right;
int key = a[left];
if(left >= right)
{
return;
}
while(i < j)
{
while(i < j && key <= a[j])
{
j--;
}
a[i] = a[j];
while(i < j && key >= a[i])
{
i++;
}
a[j] = a[i];
}
a[i] = key;
Qsort(a,left,i-1);
Qsort(a,i+1,right);
}
int main()
{
int T;
int n,i;
int src[1000];
scanf("%d*c",&T);
while(T--)
{
memset(src,0,sizeof(src));
scanf("%d*c",&n);
for(i=0;i<n;i++)
{
scanf("%d*c",&src[i]);
}
Qsort(src,0,n-1);
for(i=0;i<n-1;i++)
{
printf("%d ",src[i]);
}
printf("%d\n",src[i]);
}
}
性能
Exe.Time | Exe.Memory | Code Length | Language |
---|---|---|---|
0MS | 1408K | 914B | c |
总结
每行的最后一个元素后不应该输出空格,而应该输出换行符。因此,应对每组排序的最后元素特殊处理。
Thanks everyone