DS:using Stack to arrange the array to get max number.

来源:互联网 发布:mac 重新安装jenkins 编辑:程序博客网 时间:2024/05/22 14:18

what follows is code to demonstrate:

#include "stdafx.h"#include <malloc.h>#include <stdio.h>#include <string.h>#include<time.h>#include<stdlib.h> #define N 5  //control number of arrayint clic =1;int max(int a[],int sub){int t;if(sub==1)return a[0];else t= max(a,sub-1);{printf("\nt%d:%d\n",clic,t);clic=clic+1;}if(t>a[sub-1])return t;else return a[sub-1];}int main(int argc, char* argv[]){int a[N];srand(time(NULL));       for(int i = 0; i < N; i ++)              {         a[i]=rand()/100;          printf("a[%d]:%d\t",i,a[i]);       }  printf("\nmax:%d\n",max(a,N));printf("\nclick:%d\n",clic);return 0;}

the word,"last in first out ",is most imagic in the stack,i think the way that make up the array is a good example .
the process by order I think like this:

5th step:   return a[0]
4th step:   t=max(a,1)--->return t or a[1]
3rd step:   t=max(a,2)--->return t or a[2]
       2nd step:   t=max(a,3)--->return t or a[3]
1st step:   t=max(a,4)--->return t or a[4]
Simplely,the hypothesis i think is that the member of array,a[0],compare the rest one by order to return t max number. 


原创粉丝点击