给你一个无规则的整数序列,选择号码的顺序如下:最大的,最小的,然后是第二大,第二小...直到所有的数都被选出来。例如:给你1 3 2 5 4,你应该输出5 1 4 2 3
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
using std::sort;
int main()
{
int m,n,i,j;
int a[100000];
while( scanf("%d",&n) != EOF)
{
#include <cstdio>
#include <algorithm>
using namespace std;
using std::sort;
int main()
{
int m,n,i,j;
int a[100000];
while( scanf("%d",&n) != EOF)
{
for(i=0; i<n; i++)
scanf("%d", &a[i]);
sort(a, a+n);
if(n%2 == 0)
{
m = n/2;
for( j = 0; j<m-1; j++)
printf("%d %d ", a[n-1-j], a[j]);
printf("%d %d", a[n-m], a[m-1]); //最后一组要另行输出,且格式不同,最后无空格
printf("\n");
}
else
{
m = n/2;
for( j = 0; j<m; j++)
printf("%d %d ", a[n-1-j], a[j]);
printf("%d", a[m]); //同上,另行标明,最后无空格
printf("\n");
scanf("%d", &a[i]);
sort(a, a+n);
if(n%2 == 0)
{
m = n/2;
for( j = 0; j<m-1; j++)
printf("%d %d ", a[n-1-j], a[j]);
printf("%d %d", a[n-m], a[m-1]); //最后一组要另行输出,且格式不同,最后无空格
printf("\n");
}
else
{
m = n/2;
for( j = 0; j<m; j++)
printf("%d %d ", a[n-1-j], a[j]);
printf("%d", a[m]); //同上,另行标明,最后无空格
printf("\n");
}
}
return 0;
}
return 0;
}
上面的程序要格外注意输出的最后一组的格式,与其他的不同,最后并没有空格。若多加了空格,运行时看不出错误,但提交后会出现Presentation Error。
对于一些数组中需要输出空格或其他格式的程序,要注意最后输出时的格式,注意最后输出的有没有空格或其他符号,否则有时候运行是正确的,但提交后仍然显示Presentation Error.
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
using std::sort;
int main()
{
int m,n,i,j;
int a[100000];
while( scanf("%d",&n) != EOF)
{
for(i=0; i<n; i++)
scanf("%d", &a[i]);
sort(a, a+n);
if(n%2 == 0)
{
m = n/2;
for( j = 0; j<m-1; j++)
printf("%d %d ", a[n-1-j], a[j]);
printf("%d %d", a[n-m], a[m-1]); //最后一组要另行输出,且格式不同,最后无空格
printf("\n");
}
else
{
m = n/2;
for( j = 0; j<m; j++)
printf("%d %d ", a[n-1-j], a[j]);
printf("%d", a[m]); //同上,另行标明,最后无空格
printf("\n");
}
}
return 0;
}