2013
12-13

# 九度-1185-特殊排序[解题代码]

4
1 3 4 2

4
1 2 3

java 代码如下：

import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner s = new Scanner(new BufferedInputStream(System.in));
while(s.hasNextInt()){
int n = s.nextInt();
int arr[] = new int[n];
for(int i=0; i<n; i++){
arr[i] = s.nextInt();
}
Arrays.sort(arr);
System.out.println(arr[n-1]);
for(int i=0; i<n-1; i++){
if(i != n-2)
System.out.print(arr[i]+" ");
else
System.out.print(arr[i]);

}
if( n==1)
System.out.print(-1);
System.out.println();
}
}

}

/**************************************************************
Problem: 1185
User: coder
Language: Java
Result: Accepted
Time:2450 ms
Memory:82380 kb
****************************************************************/

C语言代码：

#include<stdlib.h>
#include<stdio.h>

int main()
{
int n,i,j,x[1001],tmp;
while(scanf("%d",&n)!=EOF && n>=1 &&n<=1000)
{
for(i=0;i<n;i++)
scanf("%d",&x[i]);
if(n==1)
{
printf("%d\n",x[0]);
printf("-1\n");
}
else
{

for(i=0;i<n-1;i++)
{
if(x[i]>x[i+1])
{
tmp=x[i+1];
x[i+1]=x[i];
x[i]=tmp;
}
}
printf("%d\n",x[n-1]);
for(i=0;i<n-2;i++)
{
for(j=0;j<n-i-2;j++)
{
if(x[j]>x[j+1])
{
tmp=x[j+1];
x[j+1]=x[j];
x[j]=tmp;
}
}
}
printf("%d",x[0]);
for(i=1;i<n-1;i++)
printf(" %d",x[i]);
printf("\n");
}

}
return 0;
}

/**************************************************************
Problem: 1185
User: 苏黎狼
Language: C
Result: Accepted
Time:80 ms
Memory:912 kb
****************************************************************/