There are n (2<=n<=50) integers. Arrange them in a row so that the maximal difference between adjacent numbers is minimal.
The first line contains one integer, indicating the number of test cases. For each test case, there is one line containing the n numbers.
Output one line for each test case containing an arrangement of the numbers which minimizes the maximal difference. Numbers should be separated by one space. If there are several arrangements that meet the requirement, output the lexicographically smallest one.
2 5 1 3 1 8 2 4
1 3 5 1 2 4 8
参考代码如下:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<algorithm>
using
namespace
std;
int
main()
{
char
ch;
int
i,t,d[55];
scanf
(
"%d"
,&t);
getchar
();
while
(t--)
{
memset
(d,0,
sizeof
(d));
i=0;
ch=
getchar
();
while
(1)
{
d[i]=0;
if
(ch==
‘\n‘
||ch==EOF)
break
;
while
(ch==
‘ ‘
) ch=
getchar
();
while
(
isalnum
(ch))
{
d[i]=d[i]*10+ch-
‘0‘
;
ch=
getchar
();
}
i++;
}
if
(ch==EOF)
break
;
sort(d,d+i);
for
(
int
j=0;j<i-1;j++)
printf
(
"%d "
,d[j]);
printf
(
"%d\n"
,d[i-1]);
}
return
0;
}
原文:http://blog.csdn.net/u011292087/article/details/19400749