#include "iostream.h"
#include "string.h"
#define for if(0); else for
int R[]={-1,-2,3,2,1,4,8,2,2,13,3,2,1,4,8,13,-1,-1,1,1,-1,2,2,};
const int rlen=sizeof(R)/sizeof(int);
//选择排序
void SelectSort(int *r,int n)
{
 int tmp ;
 for (int i= 0;i<n;i++)
 {
  int k = i;
  for (int j = k+1; j< n; j++ )
  {
   if (r[j]<r[k])
   {
    k =j;
   }
  }
  if (k!=i)
  {
   tmp = r[i];r[i] = r[k];r[k]= tmp;
  }
 }
}
//确定排序好的数组中第二个小的数共有end -star +1个
void SecondNum(int* R,int n,int *star,int *end)
{
 int tmp = R[0];
 int count = 0;
 for (int i = 0, j=1;j<n-1;j++)
 { 
  if (tmp == R[j] )
  {
   i++;
  }
        else
  {
   count++;
   if (count == 1)
   {
    tmp = R[i+1];
    *star = 1+i;
   }
            if (count == 2)
            {
    *end = j;
            }
  }
 }
}
//输出数组的元素
void output(int *R,int star,int end)
{
 for (int i = star; i < end; i ++)
 {
  cout<<R[i]<<" ";
 }
 cout<<endl;
}
void main()
{
 SelectSort(R,rlen);
 output(R,0,rlen);
 int *star = new int;
 int *end  = new int;
    SecondNum(R,rlen,star,end);
 output(R,*star,*end);
}

rusult:

-2 -1 -1 -1 -1 1 1 1 1 2 2 2 2 2 2 3 3 4 4 8 8 13 13
-1 -1 -1 -1


评论

该日志第一篇评论

发表评论

评论也有版权!