Posted on 2010-08-04 21:32
MiYu 阅读(419)
评论(0) 编辑 收藏 引用 所属分类:
C/C++ 、
ACM ( 排序 )
//MiYu原创, 转帖请注明 : 转载自 ______________白白の屋
题目地址 :
http://acm.hdu.edu.cn/showproblem.php?pid=2673
基本就是 当前最大值 和 最小值 的交替输出~
很简单的水题, 直接代码:
//MiYu原创, 转帖请注明 : 转载自 ______________白白の屋
#include <stdio.h>
#include <stdlib.h>
int num[10001];
int cmp ( const void *a, const void *b )
{
return *( ( int * )a ) - *( ( int * )b );
}
int main ()
{
int N,i,j,k;
while ( scanf ( "%d",&N ) != EOF )
{
for ( i = 0; i != N; ++ i )
{
scanf ( "%d",num + i );
}
qsort ( num , N ,sizeof (int), cmp );
i = N - 1, j = 0;
for ( k = 0; k != N; ++ k )
{
if ( k % 2 == 0 )
{
printf ( !j ? "%d" : " %d",num[i--] );
}
else
{
printf ( " %d",num[j++] );
}
}
putchar ('\n');
}
return 0;
}
另外没事做用 map 做了一次, 时间上增加了 46 MS .
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
map < int ,int > mp;
int main ()
{
int N,i,k;
while ( scanf ( "%d",&N ) != EOF )
{
int num;
mp.clear();
for ( i = 0; i != N; ++ i )
{
scanf ( "%d",&num );
mp[ num ] = 1;
}
map < int ,int >::iterator beg = mp.begin();
map < int ,int >::iterator end = mp.end();
end --;
for ( k = 0; k != N; ++ k )
{
if ( k % 2 == 0 )
{
printf ( !k ? "%d" : " %d",end->first );
end --;
}
else
{
printf ( " %d",beg->first );
beg ++;
}
}
putchar ('\n');
}
return 0;
}