Nth Largest Value

Problem 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

 1 #include <stdio.h>
 2
 3 int main(){
 4     int T;
 5     int n;
 6     int number;
 7     int i;
 8     int max;
 9     int middle;
10     int min;
11
12     scanf("%d",&T);
13
14     while(T--){
15         max=1;     //对最大的三个数进行初始化
16         middle=1;
17         min=1;
18         scanf("%d",&n);
19
20         for(i=0;i<10;i++){
21             scanf("%d",&number);
22
23             if(number>max){    //如果找到当前最大的数,舍去最小数
24                 min=middle;
25                 middle=max;
26                 max=number;
27             }
28
29             else if(number>middle){  //如果找到当前第二大的数,舍去最小数
30                 min=middle;
31                 middle=number;
32             }
33
34             else if(number>min)    //如果找到当前第三大的数,舍去最小数
35                 min=number;
36         }
37
38         printf("%d %d\n",n,min);
39
40     }
41     return 0;
42 }
时间: 2024-12-18 23:41:18

Nth Largest Value的相关文章

HDU- 3279 - Nth Largest Value

Nth Largest Value Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1140    Accepted Submission(s): 892 Problem Description For this problem, you will write a program that prints the Nth largest

stl_algo.h

// Algorithm implementation -*- C++ -*- // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, // 2010, 2011 // Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you c

ural 1218. Episode N-th: The Jedi Tournament

1218. Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Knights to organize a tournament once. To know, accumulates who the largest amount of Force. Brought each Jedi his lightsaber with him to the tourn

(单调栈)poj-2559 Largest Rectangle in a Histogram

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the

215. Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.Note:You may assume k is always valid, 1 ≤ k ≤ array's le

LeetCode 19. Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n

19 Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head. For example,    Given linked list: 1->2->3->4->5, and n = 2.    After removing the second node from the end, the linked list becomes 1->2->3->5. Note:G

Leetcode: Largest Number

Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of an i

HDU 1056 Largest Rectangle in a Histogram(dp)(求最大的矩形面积)

Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of