用c++如何编写输入10个数字找出其中第二大的数字

来源:百度知道 编辑:UC知道 时间:2024/06/11 04:42:22
program that prompts for 'n' natural numbers and prints the second largest number in the set?

#include <iostream.h>
#define MAX_LEN 10
#define MAX_INT -99999
int main()
{
int m, max = MAX_INT, sec_max = MAX_INT;
int a[MAX_LEN];
cout<<"input 10 numbers:";
for(m=0;m<MAX_LEN;m++)
{
cin>>a[m];
}

for (m = 0; m < MAX_LEN; m++)
{
if (a[m] > max)
{
sec_max = max;
max = a[m];
}
else if (a[m] > sec_max)
{
sec_max = a[m];
}
}
cout<<"sec_max="<<sec_max<<endl;
return 0;
}

int main()
{
int max,smax,t;
cin>>t;
max = smax = t;
for(int i=0;i<9;i++){
cin>>t;
if(max<t)max=t;
if(t>smax&&t<max)smax=t;
}
cout<<"Max:"<<max<<"\t\tSMax:"<<smax<<endl;
cout<<"\n";
return 0;
}

//给你经典的冒泡