Seeking the maximum and minimum number - cattle-off

Title Description

N pieces (N <= 10000) numbers, obtaining maximum and minimum values ​​of the N numbers. It is not more than the absolute value of each number 1,000,000.

Enter a description:

Test input comprising a plurality of sets, each at the beginning of a test by the integer N, N integers are given the next line.

Output Description:

Output includes two integers, the maximum and minimum for a given number N.


Problem-solving ideas

First, the comparison with a bubbling extra space, find the maximum, minimum.

 1 #include <stdio.h>
 2 int main()
 3 {
 4     int N;
 5     while(scanf("%d",&N)!=EOF)
 6     {
 7         int array[N];
 8         for(int i =0;i<N;i++)
 9         {
10             scanf("%d",&array[i]);
11         }
12         int max = array[0];
13         int min = array[0];
14         for(int i =1;i<N;i++)
15         {
16             if(array[i] > max) max = array[i];
17             if(array[i] < min) min = array[i];
18         }
19         printf("%d %d\n",max,min);
20     }
21 }

Two, C ++ library using the algorithm inside the sort () function

 1 #include <iostream>
 2 #include <algorithm>
 3 
 4 using namespace std;
 5 int main()
 6 {
 7     int N;
 8     while(cin >> N)
 9     {
10         int a[N];
11         for(int i=0;i<N;i++)
12             cin >> a[i];
13         sort(a,a+N);
14         cout << a[N-1]<< " "<< a[0]<<endl;
15     }
16 }

 

Guess you like

Origin www.cnblogs.com/jiashun/p/newcode9.html