能处理一般情况下的数组,不知道有什么遗漏没有,希望大家建议:
- #include<iostream>
- using namespace std;
- #define MAXSIZE 10
- int Sgn(int *A,int size)
- {
- int max=0,semax=-1,i;
- for(i=1;i<size;i++)
- {
- if(A[i]==A[max])
- ;
- else
- if(A[i]>A[max])
- {
- semax=max;
- max=i;
- }
- else
- if((semax == -1) || (A[i]>A[semax]))
- semax=i;
- }
- return A[semax];
- }
- int main()
- {
- int A[MAXSIZE]={900,23,45,56,900,45,9,17,8,876};
- cout<<"The array is :";
- for(int i=0;i<10;i++)
- cout<<A[i]<<" ";
- cout<<endl;
- cout<<"The second NO is:"<<Sgn(A,MAXSIZE);
- cout<<endl;
- return 0;
- }
本文介绍了一种在C++中寻找数组中第二大元素的方法。通过遍历数组并维护最大值和次大值来实现,适用于一般情况下的数组查找场景。
1563

被折叠的 条评论
为什么被折叠?



