2013年北邮网研院上机真题(A)

论坛 期权论坛 脚本     
匿名技术用户   2021-1-2 02:49   35   0

A 最值问题

Accept:210

Submit:633

Time Limit:1000MS

Memory Limit:65536KB

Description

给出N个数,求出这N个数中最大值和次大值。注意这里的次大值必须严格小于最大值。输入保证N个数中至少存在两个不同的数。

InputFormat

第一行为测试数据的组数T(T≤20)。请注意,任意两组测试数据之间是相互独立的。

每组数据包括两行:

第一行为一个整数N(1≤N≤1000)

第二行为N个正整数,每个整数均不大于106

OutputFormat

每组数据输出两个数,即N个数的最大值和次大值,中间用空格隔开。

SampleInput

2

5

1 2 3 4 5

4

2 2 2 3

SampleOutput

5 4

3 2

# include <stdio.h> 
 # include <stdlib.h> 
  
 int comp(const void *a,const void *b) 
 { 
     return *(int *)a-*(int *)b; 
 }  
  
 int main() 
 { 
     int t,n; 
     int i,j; 
     int str[1001]; 
  
     scanf("%d",&t); 
  
     for(i=0;i<t;i++) 
     { 
         scanf("%d",&n); 
  
         for(j=0;j<n;j++) 
         { 
             scanf("%d",&str[j]);     
         } 
          
         qsort(str,n,sizeof(int),comp); 
          
         printf("%d ",str[n-1]); 
  
         for(j=n-2;j>=0;j--) 
             if(str[j]!=str[n-1]) 
             {printf("%d\n",str[j]);break;} 
     } 
     return 0; 
 }

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP