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

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 array
int 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. 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值