zoukankan      html  css  js  c++  java
  • 数据结构——递归法求解最大值和最小值

    【递归法求解最大值和最小值】
    问题描述:若一个无序的线性表A[MaxSize]采用顺序存储方式,元素类型为整型数。试写出递归算法求出A中的最大元素和最小元素。
    要求: 顺序表的数据通过调用算法initRandomize()随机产生。

    #include <stdio.h>
    #include <stdlib.h>
    #define OK 1
    #define ERROR 0
    #define OVERFLOW -1
    #define TRUE 1
    #define FALSE 0
    #define  MAXSIZE       100
    
    typedef  struct Stack {
    	int    data[MAXSIZE];	    // 栈
    	int         top;            // 栈顶指针(实际是数组的下标值)
    } SqStack;
    
    int InitStack(SqStack &S)
    {
    	S.top=0;
    }
    
    int Push(SqStack &S,int e)
    {
    	if(S.top>=MAXSIZE) return ERROR;
    	S.data[S.top++]=e;
    	return OK;
    }
    
    
    int Pop(SqStack &S,int &e)
    {
    	if(S.top == 0)  return ERROR;
    	e=S.data[--S.top];//e=*(S.data+S.top-1)
    	return OK;
    }
    
    int GetHead(SqStack &S,int &e)
    {
    	if(S.top == 0)  return ERROR;
    	e=S.data[S.top-1];//e=*(S.data+S.top-1)
    	return OK;
    }
    
    int Empty(SqStack &S)
    {
    	if(S.top == 0)  return 1;
    	else return 0;
     } 
    
    int Full(SqStack S)
    {
    	if(S.top == MAXSIZE)  return 1;
    	else return 0;
    }
    
    #include <time.h>
    void initRandomize(int *arr, int n, int min, int max)
    {
        int i = 0;
        srand(time(0));  			/*设置种子,并生成伪随机序列*/
        for (i = 0; i < n; ++i) {
            arr[i] = rand()% (max - min + 1) + min;  /*得到从[min, max]之间的随机数*/
           //arr[i] =rand();
            //printf("%d ", arr[i]);
        }
        //printf("
    ");
    }
    
    int bubble_max(SqStack &S)//这里用引用会导致真实S顺序变化 
    {
    	int i,j,temp;
    	for(i=1;i<=S.top-1;i++)//外层遍历元素长度数-1 
    		for(j=0;j<S.top-i;j++)//内层遍历数从n-1、n-2......2、1(共n-1次,次数有外层控制) 
    		{
    			if(S.data[j]>S.data[j+1])
    			{
    				temp=S.data[j];
    				S.data[j]=S.data[j+1];
    				S.data[j+1]=temp; 
    			}
    		}
    }
    
    int bubble_min(SqStack &S)//这里不用引用会导致真实S顺序不改变 
    {
    	int i,j,temp;
    	for(i=1;i<=S.top-1;i++)//外层遍历元素长度数-1 
    		for(j=0;j<S.top-i;j++)//内层遍历数从n-1、n-2......2、1(共n-1次,次数有外层控制) 
    		{
    			if(S.data[j]<S.data[j+1])
    			{
    				temp=S.data[j];
    				S.data[j]=S.data[j+1];
    				S.data[j+1]=temp; 
    			}
    		}
    }
    int recursion_max(SqStack &S,int i,int max)
    {
    	if(i==S.top) return max;
    	if(S.data[i]>max)
    	{
    		max=S.data[i];
    		return recursion_max(S,++i,max);
    	}
    	return recursion_max(S,++i,max);
    }
    
    
    int recursion_min(SqStack &S,int i,int min)
    {
    	if(i==S.top) return min;
    	if(S.data[i]<min)
    	{
    		min=S.data[i];
    		return recursion_min(S,++i,min);
    	}
    	return recursion_min(S,++i,min);
    }
    
    
    void test_bubble(SqStack S,int a[])
    {
    	//Sleep(1000);
    	//initRandomize(&a[0], 11, 1, 1002);
    	int e;
    	for(int i=0;i<10;i++)
    	{
    		Push(S,a[i]);
    	}
    	bubble_min(S);
    	GetHead(S,e);
    	printf("最小值是%d 
    ",e);
    	bubble_max(S);
    	GetHead(S,e);
    	printf("最大值是%d 
    ",e);
    }
    
    void test_recursion(SqStack S,int a[]) 
    {
    	int e;
    	for(int i=0;i<10;i++)
    	{
    		Push(S,a[i]);
    	}
    		printf("最大值是%d 
    ",recursion_max(S,1,S.data[0]));
    		printf("最小值是%d 
    ",recursion_min(S,1,S.data[0]));
    	while(!Empty(S))
    	{
    		Pop(S,e);
    		printf("%d ",e);
    	}
    	printf("
    ");
    }
    
    
    int main()
    {
    	SqStack S;
    	int a[11],i,e;	
    	initRandomize(&a[0], 10, 1, 1002);	
    	InitStack(S);
    	for(i=0;i<10;i++)
    	{
    		Push(S,a[i]);
    	}	
    	while(!Empty(S))
    	{
    		Pop(S,e);
    		printf("%d ",e);
    	}
    	printf("
    ");
    	//test_bubble(S,a);
        test_recursion(S,a) ;
    }
    
    
  • 相关阅读:
    解决:fatal error LNK1104: 无法打开文件“libc.lib”
    解决:error LNK2026: 模块对于 SAFESEH 映像是不安全的
    相似性度量(Similarity Measurement)与“距离”(Distance)
    按下开机键后,电脑都干了些什么?
    解决win10中chm内容显示为空白的问题
    BootStrap 模态框基本用法
    error CS0016: 未能写入输出文件
    解决网页前端的图片缓存问题
    .net 新闻点击量修改,避免恶意刷新
    使用 jQuery 页面回到顶部
  • 原文地址:https://www.cnblogs.com/vivid-victory/p/10090470.html
Copyright © 2011-2022 走看看