Nth Largest Value

Nth Largest Value

Description

For this problem, you will write a program that prints the Nth largest value in a fixed sized array of integers. To make things simple, N will be 3 and the array will always be have 10 decimal integer values.

Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set consists of a single line containing the data set number, followed by a space, followed by 10 space separated decimal integers whose values are between 1 and 1000 inclusive.

Output

For each data set, generate one line of output with the following values: The data set number as a decimal integer, a space, and the 3rd largest value of the corresponding 10 integers.

Sample Input

4 
1 1 2 3 4 5 6 7 8 9 1000 
2 338 304 619 95 343 496 489 116 98 127 
3 931 240 986 894 826 640 965 833 136 138 
4 940 955 364 188 133 254 501 122 768 408

Sample Output

1 8 
2 489 
3 931 
4 768
Hint
Source
Greater New York Region 2009

找出第三大数,两个思路:全部排序找第三大,或者直接排n-3--n;

思路一:

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int i=0,b[11],n,s;
 cin>>n;
 while(n--)
 {
   cin>>s;
    for(i=0;i<10;i++)
     cin>>b[i];
   sort(b,b+10);
   cout<<s<<" "<<b[7]<<endl;
 }

         return 0;
}

思路二:

#include<iostream>
#include<algorithm>
using namespace std;

int b[11];
int main()
{
       int w;
       cin>>w;
       while(w--)
       {
            int n=10,ss;
              cin>>ss;
              int i=0;
           while(i<n)
          {
             cin>>b[i++];
           }
          nth_element(b,b+7,b+10);
         cout<<ss<<" "<<b[7]<<endl;;
         }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值