zoukankan      html  css  js  c++  java
  • 二分查找求最大数最小数

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication10
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] intArr = { 8,2,5,9,4,1,3,7,-1};
                MaxMin result = FindMaxAndMin(intArr, 0, intArr.Length - 1);
                Console.WriteLine("max is {0}, min is {1}",result.max, result.min);
            }
    
            static MaxMin FindMaxAndMin(int[] input, int start, int end)
            {
                MaxMin result;
                if (end - start <=1)
                {
                    if (input[start]>input[end])
                    {
                        result.max = input[start];
                        result.min = input[end];
                    }
                    else
                    {
                        result.max = input[end];
                        result.min = input[start];
                    }
                    return result;
                }
    
                MaxMin front = FindMaxAndMin(input, start, start + (end - start) / 2);
                MaxMin rear = FindMaxAndMin(input, start + (end - start) / 2 + 1, end);
    
                if (front.max > rear.max)
                {
                    result.max = front.max;
                }
                else
                {
                    result.max = rear.max;
                }
    
                if (front.min<rear.min)
                {
                    result.min = front.min;
                }
                else
                {
                    result.min = rear.min;
                }
    
                return result;
    
            }
        }
    
        struct MaxMin
        {
            public int max;
            public int min;
        }
    }
    View Code
  • 相关阅读:
    hive默认配置 .hiverc
    hive 行列转换
    hive 全表全字段对比
    shell 获取hive表结构
    粘包现象与解决方案
    win 关闭正在使用的端口
    pycharm格式报错: Remove redundant parentheses
    博客系统作业
    django中间件
    django的用户认证组件
  • 原文地址:https://www.cnblogs.com/Ligeance/p/3537892.html
Copyright © 2011-2022 走看看