題目描述
輸入一系列整數,將其中最大的數挑出,並將剩下的數進行排序。
輸入
輸入第一行包括1個整數N,1<=N<=1000,代表輸入數據的個數。
接下來的一行有N個整數。
輸出
可能有多組測試數據,對於每組數據,
第一行輸出一個整數,代表N個整數中的最大值,並將此值從數組中去除,將剩下的數進行排序。
第二行將排序的結果輸出。
樣例輸入
5
5 3 2 4 1
樣例輸出
5
1 2 3 4
提示
如果數組中只有一個數,當第一行將其輸出後,第二行請輸出"-1"。
思路:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin >> n)
{
vector<int>ans(n);
for(int i=0;i<n;i++)
{
cin >> ans[i];
}
sort(ans.begin(), ans.end());
if(n==1)
{
cout<<ans[0]<<endl<<-1<<endl;
}
else
{
cout<<ans[n-1]<<endl;
for(int i=0;i<n-1;i++)
{
cout<<ans[i] ;
if(i!=n-2)
cout<<" ";
else
cout<<endl;
}
}
}
return 0;
}