一个数组,找出其中具有这样一个特性的数字:1、比它左边的数都大2、比它右边的数都小

 #include <stdio.h>
#include <windows.h>
#include <time.h>

#define MAX 10
int InintArrary(int *arrary);
int FromLeft(int *arrary, int *fromLeftToRight);
int FromRight(int *arrary, int *fromRightToLeft);
int FindNumber(int *fromRightToLeft, int *fromLeftToRight, int *pos, int *find, int *num);
int PrintArrary(int *ar);

int main(int argc, char* argv[])
{
 int arrary[MAX] = {0};
    int fromLeftToRight[MAX] = {0};
 int fromRightToLeft[MAX] = {0};
 int pos[MAX] = {0};
 int find[MAX] = {0};
 int num = 0;
    int ret = 0;
    int flag = 1;

 int times = 0;
 while (flag == 1)
 {
  ret = InintArrary(arrary);
  ret = PrintArrary(arrary);
  
  ret = FromLeft(arrary, fromLeftToRight);
  //ret = PrintArrary(fromLeftToRight);
  
  ret = FromRight(arrary, fromRightToLeft);
  //ret = PrintArrary(fromRightToLeft);
  
  ret = FindNumber(fromRightToLeft, fromLeftToRight, pos, find, &num);
  if (num > 0)
  {
   for (int j=0; j<num; j++)
   {
    printf("position %d =%d  ", pos[j], find[j]);
   }
   flag = 0;
  }else
  {
   times++;
   printf("can not find! Try again for the %d times/n/n/n", times);       
  }
 }

 return 0;
}

int InintArrary(int *arrary)
{
 if (arrary == NULL)
 {
  return 0;
 }
 
 Sleep(1000);
 srand((unsigned)(time(NULL)));
 for (int i=0; i<MAX; i++)
 {
    
  arrary[i] = 1 + (int)(100*rand()/(RAND_MAX + 1.0));
 } 
 return 1;
}
 
int PrintArrary(int *ar)
{
    if (ar == NULL)
 {
  return 0;
 }

 for (int i=0; i<MAX; i++)
 {
  printf("arrary %d =%d  ", i, ar[i]);
  if (i%(MAX/10) == 0 && i != 0)
  {
   printf("/n");
   printf("/n");
   printf("/n");
  }
 }
 printf("/n");
 printf("/n");
 return 1;
}

int FromLeft(int *arrary, int *fromLeftToRight)
{
 if (arrary == NULL || fromLeftToRight == NULL)
 {
  return 0;
 }

 int currentMAX = arrary[0];
 fromLeftToRight[0] = arrary[0];

 for (int i=1; i<MAX; i++)
 {
  if (arrary[i] <= currentMAX)
  {
   fromLeftToRight[i] = currentMAX;
  }else
  {
   currentMAX = arrary[i];
   fromLeftToRight[i] = arrary[i];
  }
 }
 
 return 1;
}

int FromRight(int *arrary, int *fromRightToLeft)
{
 if (arrary == NULL || fromRightToLeft == NULL)
 {
  return 0;
 }

 int tail = MAX - 1;
 int currentMIN = arrary[tail];
 fromRightToLeft[tail] = arrary[tail];

 for (int i=tail-1; i>=0; i--)
 {
  if (arrary[i] >= currentMIN)
  {
   fromRightToLeft[i] = currentMIN;
  }else
  {
   currentMIN = arrary[i];
   fromRightToLeft[i] = arrary[i];
  }
 }

 return 1;
}

int FindNumber(int *fromRightToLeft, int *fromLeftToRight,
      int *pos, int *find, int *num)
{
 if (fromRightToLeft == NULL || fromLeftToRight == NULL ||
  pos == NULL || find == NULL)
 {
  return 0;
 }

 int count = 0;
 *num = 0;
 for (int i=0; i<MAX; i++)
 {
  if (fromLeftToRight[i] == fromRightToLeft[i])
  {
   pos[count] = i;
   find[count] = fromRightToLeft[i];
   count++;
   *num = *num + 1;
  }
 }

 if (count == 0)
 {
  return 0;
 }else
 {
  return 1;
 }
}

//没有贴成代码的形式,自己复制之后,到VC中用Alt+F8,可以看到整齐的代码了。

你可能感兴趣的:(一个数组,找出其中具有这样一个特性的数字:1、比它左边的数都大2、比它右边的数都小)