• 分治法求最大最小元


    用分治法求n个元素集合S中的最大、最小元素(要求每次平分成2个子集)

    void maxmin(int A[], int &e_max, int &e_min ,int low, int high)
    {
    int mid, x1, y1, x2, y2;
    if (high - low <= 1) {             //<=,可能为1个
    if (A[high] > A[low]) {
    e_max = A[high];
    e_min = A[low];
    }
    else {
    e_max = A[low];
    e_min = A[high];
    }
    }    //特征1: 小到容易求解
      else {                     
         mid = (low + high) / 2;           //特征2:可分解 
         maxmin(A, x1, y1, low, mid); //特征4: 子问题独立
        
    maxmin(A, x2, y2, mid + 1, high);
        e_max = max(x1, x2); //特征3: 可合并
         e_min = min(y1, y2);
       }
    }

    分析如下

    #include <stdio.h>
    #include<stdlib.h>
    
    int max(int a, int b)
    {
        return (a > b) ? a : b;
    }
    
    int min(int a, int b)
    {
        return (a < b) ? a : b;
    }
    
    void maxmin(int A[], int &e_max, int &e_min ,int low, int high)
     {
         int mid, x1, y1, x2, y2;
         if (high - low <= 1) {                //<=,可能为1个
             if (A[high] > A[low]) {
                 e_max = A[high];
                 e_min = A[low];
             }
             else {
                 e_max = A[low];
                 e_min = A[high];
             }
         }                                                //特征1:小到容易求解
         else {
            mid= (low + high) / 2;            //特征2:可分解 
            maxmin(A, x1, y1, low, mid);    //特征4:子问题独立
            maxmin(A, x2, y2, mid + 1, high);
            e_max = max(x1, x2);            //特征3:可合并
            e_min =  min(y1, y2);
        }
    }
    
    int main()
    {
        int init[11], max, min;
        for(int i = 1; i <= 10; i ++){
            scanf("%d", &init[i]);
        }
        maxmin(init, max, min, 1, 10);
        printf("%d %d", max, min);
        system("pause");
    }
    
    /* 
    (0 1 3 5 2 4 6 8 9 7),ENTER LEVEL ONE
     1          5                    ENTER LEVEL TWO
     1    3                        ENTER LEVEL THREE
     1 2                            ENTER LEVEL FOUR
     e_max = 1;
     e_min = 0;
     1 2                            EXIT LEVEL FOUR            x1 = 1; y1 = 0;
    
           3
           e_max = e_min =3;
           3                        EXIT LEVEL FOUR            x2 = y2 = 3;
    
           e_max = 3;  -> x1;
           e_min = 0;   -> y1;
     1    3                        EXIT LEVEL THREE            x1 = 1; y1 = 3;
    
              4 5                    ENTER  LEVEL THREE
              x2 = 5;
              y2 = 2;
              e_max = 5;  -> x1;
              e_min = 0;   -> y1;
              4 5                     EXIT LEVEL THREE        x2 = 5; y2 = 2;
    
              e_max = 5;
              e_min = 0;
    1           5                  EXIT LEVEL TWO            x1 = 5; y1 = 0;
    
                    6          10 ENTER LEVEL TWO
                    6    8         ENTER LEVEL THREE
                    6 7            ENTER LEVEL FOUR
                    e_max = 6;
                    e_min = 4;
                    6 7            EXIT LEVEL FOUR            x1 = 6; y1 = 4;
                          8         ENTER LEVEL FOUR
                          e_max = e_min = 8;
                          8         EXIT LEVEL FOUR            x2 = y2 = 8;
                          e_max = 8;
                          e_min = 4;
                    6    8         EXIT LEVEL THREE        x1 = 8; y1 = 4;
                             9 10 ENTER LEVEL THREE
                             e_max = 9;
                             e_min = 7;
                             9 10 EXIT LEVEL THREE        x2 = 9; y2 = 7;
                    6          10 EXIT LEVEL TWO            x2 = 9; y2 = 4;
    
                    e_max = 9;
                    e_min = 0;
    1                           10  EXIT LEVEL ONE        e_max = 9; e_min = 0;
    
    */
  • 相关阅读:
    oracle的分析函数over 及开窗函数
    ASP.NET中分布式事务的使用
    后台实现显示欢迎用户登陆的方法
    AjaxHelper的get和post请求的封装类
    登陆权限验证Session和Cookie用法及BasePage类使用
    四个常用.NET的SqlHelper的方法
    ASP.NET在实际开发中验证码的用法
    SQL Server事务的存储过程
    利用JQuery实现全选和反选的几种方法
    JS中表格的全选和删除要注意的问题
  • 原文地址:https://www.cnblogs.com/tomctx/p/2435887.html
Copyright © 2020-2023  润新知