Description
There are n (2<=n<=50) integers. Arrange them in a row so that the maximal difference between adjacent numbers is minimal.
Input
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
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.
Sample Input
Sample Output
1 3 5
1 2 4 8
参考代码如下:
#include
#include
#include
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&#43;ch-
‘0‘
;
ch=
getchar
();
}
i&#43;&#43;;
}
if
(ch==EOF)
break
;
sort(d,d&#43;i);
for
(
int
j=0;j
printf
(
"%d "
,d[j]);
printf
(
"%d\n"
,d[i-1]);
}
return
0;
}