P - 次大和次小
Description
对于一个数组,次大的数指数组中第二大的数,相似地,次小的数指数组中第二小的数。
给定一个含有 n 个数的数组(数组中的数互不相同),求其中次大的数和次小的数。
Input
首先输入一个整数 T (1 <= T <= 200),表示数据组数。
对于每组数据:
- 第 1 行输入一个整数 n (2 <= n <= 1000),表示数组中元素的数量。
- 第 2 行输入 n 个用空格隔开的整数 Ai (-10000 <= Ai <= 10000),表示数组中每一个元素的值。
Output
对于每组数据,输出一行,包含 2 个整数 a, b,分别表示次大和次小的数。
Sample
Input
1 5 3 1 2 4 5
Output
4 2
代码如下:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include <stdlib.h>
int main()
{
int n,m,i,j,a[1005],t;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
for(i=0;i<m;i++)
scanf("%d",&a[i]);
for(i=1;i<m;i++)
{
for(j=0;j<=m-i-1;j++)
{
if(a[j]<a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf("%d %d\n",a[1],a[m-2]);
}
return 0;
}