能處理一般情況下的數組,不知道有什麼遺漏沒有,希望大家建議:
- #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;
- }