求次大數

能處理一般情況下的數組,不知道有什麼遺漏沒有,希望大家建議:

  1. #include<iostream>
  2. using namespace std;
  3. #define MAXSIZE 10
  4. int Sgn(int *A,int size)
  5. {
  6.     int max=0,semax=-1,i;
  7.     
  8.     for(i=1;i<size;i++)
  9.     {
  10.         if(A[i]==A[max])
  11.             ;
  12.         else 
  13.             if(A[i]>A[max])
  14.         {
  15.             semax=max;
  16.             max=i;
  17.         }
  18.         else 
  19.             if((semax == -1) || (A[i]>A[semax]))
  20.                 semax=i;
  21.     }
  22.     return A[semax];
  23. }
  24. int main()
  25. {
  26.     int A[MAXSIZE]={900,23,45,56,900,45,9,17,8,876};
  27.     cout<<"The array is :";
  28.     for(int i=0;i<10;i++)
  29.         cout<<A[i]<<" ";
  30.     cout<<endl;
  31.     cout<<"The second NO is:"<<Sgn(A,MAXSIZE);
  32.     cout<<endl;
  33.     return 0;
  34. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章