HDU 2561

时间:2023-03-08 19:37:56
HDU 2561

F - 第二第二

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit

Status

Practice

HDU 2561

Description

求n个整数中倒数第二小的数。

每一个整数都独立看成一个数,比如,有三个数分别是1,1,3,那么,第二小的数就是1。

Input

输入包含多组测试数据。

输入的第一行是一个整数C,表示有C测试数据;

每组测试数据的第一行是一个整数n,表示本组测试数据有n个整数(2<=n<=10),接着一行是 n个整数 (每个数均小于100);

Output

请为每组测试数据输出第二小的整数,每组输出占一行。

Sample Input

2

2

1 2

3

1 1 3

Sample Output

2

1

AC代码:

#include <stdio.h>
int main(){
int n,j,r;
int i,x,m,temp;
int array[105];
scanf("%d",&n);
for (i=1;i<=n;i++){
scanf("%d",&m);
for (x=0;x<m;x++){
scanf("%d",&array[x]);
}
for(j=0;j<=m-2;j++){
for (r=0;r<=m-2-j;r++){
if (array[r]>array[r+1]){
temp=array[r];
array[r]=array[r+1];
array[r+1]=temp;
} }
}
printf("%d\n",array[1]); }
return 0;
}

方法:冒泡法排序

思路:使用一个一维数组保存整数,然后将数组中的元素从小到大冒泡排序,最小的元素存在a[0],第二小的数存在a[1],我们只需要输出a[1],即可