多种方法求最大值(委托方法,重载)

  1 namespace ConsoleAppLearningCeshi
  2 {
  3     /// <summary>
  4     /// 不同打招呼
  5     /// </summary>
  6     /// <param name="name"></param>
  7     public delegate int deleMaxCompara<T>(T one, T two);//泛型委托
  8     public delegate int deleMaxCompara(object one, object two);
  9     class Program
 10     {
 11         static void Main(string[] args)
 12         {
 13
 14         }
 15
 16         #region 泛型委托终极版
 17         //int[] intarray = { 1,33,11,34,54,2,45,55};
 18         //   string[] strarray = { "adfasdf","adfasdfasdfas","afsdfasdfasdfasddfas","fnvncvncvbn"};
 19         //   int intmax= MaxCompara<int>(intarray,comparaIntOrStr);
 20         //   string strmax = MaxCompara<string>(strarray, comparaIntOrStr);
 21         //   Console.WriteLine("最大int值"+intmax);
 22         //   Console.WriteLine("最长string值"+strmax);
 23         //   Console.ReadKey();
 24         public static T MaxCompara<T>(T[] obj, deleMaxCompara<T> delemaxcompara)
 25         {
 26             T maxobj = obj[0];//初始化初值
 27             for (int i = 0; i < obj.Length; i++)
 28             {
 29                 if ((int)(delemaxcompara(maxobj, obj[i])) < 0)
 30                 {
 31                     maxobj = obj[i];
 32                 }
 33             }
 34             return maxobj;
 35         }
 36         public static int comparaIntOrStr(int one, int two)
 37         {
 38             return one - two;
 39         }
 40         //委托指向string的函数
 41         public static int comparaIntOrStr(string one, string two)
 42         {
 43             return one.ToString().Length - two.ToString().Length;
 44         }
 45
 46         #endregion
 47         #region 普通委托方法实现
 48         //object[] intarray = { 14,37,32,32,11};
 49         //  object[] strarray = { "ssssss","aaaaasdsd","adadadadadad","asdafsgsvsfgfdhfgkgkk"};
 50         //  int intmax= (int)MaxCompara(intarray, comparaOneInt);
 51         //  string strmax = MaxCompara(strarray, comparaOneStr).ToString();
 52         //  Console.WriteLine("int:"+intmax);
 53         //  Console.WriteLine("string.length:"+strmax);
 54         //  Console.ReadKey();
 55         public static object MaxCompara(object[] obj, deleMaxCompara delemaxcompara)
 56         {
 57             object maxobj = obj[0];//初始化初值
 58             for (int i = 0; i < obj.Length; i++)
 59             {
 60                 if ((int)(delemaxcompara(maxobj, obj[i])) < 0)
 61                 {
 62                     maxobj = obj[i];
 63                 }
 64             }
 65             return maxobj;
 66         }
 67         //委托指向int的函数
 68         public static int comparaOneInt(int one, int two)
 69         {
 70             return (one) - two;
 71         }
 72         public static int comparaOneInt(string one, string two)
 73         {
 74             return one.Length - two.Length;
 75         }
 76         //委托指向string的函数
 77         public static int comparaOneStr2(object one, object two)
 78         {
 79             return one.ToString().Length - two.ToString().Length;
 80         }
 81         #endregion
 82         #region 重载的方法
 83
 84         //object str = "dfdsaf";
 85         //   int[] intarray = { 1,44,55,67,33,6,7};
 86         //   string[] strarray = { "ddfddfd", "dsfasfasfd", "sdfasdfsadfasfasf", "sdfasfasfasdfasfsafasfasdfas" };
 87         //   int maxint;
 88         //    bool maxinbool=int.TryParse( GetMax(intarray).ToString(),out maxint);
 89         //    string maxstr = GetMax(strarray).ToString();
 90         //    if (maxinbool==true)
 91         //    {
 92         //        Console.WriteLine(maxint);
 93
 94         //    }
 95         //    Console.WriteLine(maxstr);
 96         //    Console.ReadKey();
 97         public static object GetMax(int[] intarray)
 98         {
 99             int intmax = intarray[0];//赋初值
100             //循环遍历获得最大值
101             for (int i = 0; i < intarray.Length; i++)
102             {
103                 if (intmax < intarray[i])
104                 {
105                     intmax = intarray[i];
106                 }
107
108             }
109             return intmax;
110
111         }
112         public static object GetMax(string[] strarray)
113         {
114             string strmax = strarray[0];
115             for (int i = 0; i < strarray.Length; i++)
116             {
117                 if (strmax.Length < strarray[i].Length)
118                 {
119                     strmax = strarray[i];
120
121                 }
122
123             }
124             return strmax;
125         }
126         #endregion
127         #region 普通方法实现方法
128         //int[] intarray = { 1, 23, 4, 55, 57, 622 };
129         //  string[] strarray = { "xiaobing", "dddddd", "sssssssssss", "ddddddddd", "sssssssssss", "eeeeeeeeee", "gggggggggggggggggggg" };
130         //  int maxint = GetIntMax(intarray);
131         //  string maxstr = GetStringMax(strarray);
132         //  Console.WriteLine("最大值int:" + maxint);
133         //  Console.WriteLine("最小值string:" + maxstr);
134         //  Console.ReadKey();
135         /// <summary>
136         /// 获得int最大值
137         /// </summary>
138         /// <param name="intarray"></param>
139         /// <returns></returns>
140         public static object GetIntMax(int[] intarray)
141         {
142             int intmax = intarray[0];//赋初值
143             //循环遍历获得最大值
144             for (int i = 0; i < intarray.Length; i++)
145             {
146                 if (intmax < intarray[i])
147                 {
148                     intmax = intarray[i];
149                 }
150             }
151             return intmax;
152
153         }
154         /// <summary>
155         /// 获得字符串最大值
156         /// </summary>
157         /// <param name="strarray"></param>
158         /// <returns></returns>
159         public static object GetStringMax(string[] strarray)
160         {
161             string strmax = strarray[0];
162             for (int i = 0; i < strarray.Length; i++)
163             {
164                 if (strmax.Length < strarray[i].Length)
165                 {
166                     strmax = strarray[i];
167
168                 }
169
170             }
171             return strmax;
172         }
173         #endregion
174     }
175
176
177 }

原文地址:https://www.cnblogs.com/xiaobing1/p/11263351.html

时间: 2024-08-13 13:20:35

多种方法求最大值(委托方法,重载)的相关文章

数组中简便方法求最大值,最小值,平均值,求和,和个数

//获取数组中的最大值和最小值 @min.self     NSArray *array = @[@4, @84, @2];     NSLog(@"max = %@", [array valueForKeyPath:@"@max.self"]);         //平均值和求和    NSLog(@"%@", [array valueForKeyPath:@"@sum.self"]); //平均值和求平均值    NSLo

shell编程基础一(多种方法求值1+2+..+100)

#SHELL编程基础一(多种方法求值1+2+..+100)##为什么要学好shell shell脚本语言是实现linux系统管理及自动化运维所必备的重要工具,linux系统的底层及基础应用软件的核心大都涉及shell脚本的内容. 每一个合格的linux系统管理员或运维工程师,都需要能够熟练地编写shell脚本语言,并能够阅读系统及各类软件附带的shell脚本内容. 只有这样才能提升运维人员的工作效率,适应日益复杂的工作环境,减少不必要的工作,从而为个人的职场发展奠定较好的基础.# 本文的宗旨是熟

&lt;28&gt;【了解】10-枚举类型介绍及定义+【掌握】11-枚举变量变量定义和使用+【掌握】13-typedef定义新的类型+【掌握】15-宏的概念及无参宏定义方法+【掌握】16-有参宏定义和使用方法+【掌握】17-应用:使用有参宏求最大值+【掌握】18-typedef和#define的区别

[了解]10-枚举类型介绍及定义 枚举类型: C语言提供了一个种类型,这种类型的变量的取值被限定在一定的范围之内了 枚举类型的定义: enum 枚举类型名{ 枚举值1,枚举值2,.... }; 举例: 定义一个变量,保存一周的第几天 enum weekday{ zhouyi,zhouer,zhousan,zhousi,zhouwu ,zhouliu,zhouri }; 定义iPhone手机的颜色 关于枚举类型元素的命名习惯 enum iColor{kIcolorWhite,kIcolorBlac

java求最大值以及定义方法调用

class ArrayDome { public static void main(String[] args) { int[] arr = {-12,-51,-12,-11}; int max = getMax(arr); int max_2 = getMax_2(arr); System.out.println(max); System.out.println(max_2); } //求最大值 方法一 public static int getMax(int[] arr) { int max

C# - 委托_求定积分通用方法

代码: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 委托_例子 8 { 9 static class Program 10 { 11 // 定义委托(Double类型) 12 delegate double Integand(double x); 13 14 //

三种方法求最大子数组的和

这是一道考的烂的不能再烂的题目,但是依然有很多公司乐于将这样的题目作为笔试或面试题,足见其经典. 问题是这样的:一个整数数组中的元素有正有负,在该数组中找出一个连续子数组,要求该子数组中各元素的和最大,这个子数组便被称作最大子数组.比如数组{2,4,-7,5,2,-1,2,-4,3}的最大子数组为{5,2,-1,2},最大子数组的和为5+2-1+2=8. 下面按照时间复杂度逐步优化的顺序依次给出这三种算法. 暴力求解法 该方法的思想非常简单,先找出从第1个元素开始的最大子数组,而后再从第2个元素

求质数的方法解析

质数又称素数.指在一个大于1的自然数中,除了1和此整数自身外,没法被其他自然数整除的数.换句话说,只有两个正因数(1和自己)的自然数即为素数.比1大但不是素数的数称为合数.1和0既非素数也非合数.合数是由若干个质数相乘而得到的.所以,质数是合数的基础,没有质数就没有合数. 求素数的方法有很多种,最简单的方法是根据素数的定义来求.对于一个自然数N,用大于1小于N的各个自然数都去除一下N,如果都除不尽,则N为素数,否则N为合数. 但是,如果用素数定义的方法来编制计算机程序,它的效率一定是非常低的,其

用递归的方法求数组中的最大数

思路: 得到全部5个中最大的数--> 比较第5个数与前四个中最大数的值-> 得到前四个中最大的数--> 比较第四个数与前三个中最大数的值-->得到前三个数中的最大值-->比较第三个数与前两个中最大数的值-->得到前两个数中最大的值-->比较第二个数与第一个数中的最大值 但实际运算是从最右端往左端逐步(和上面的执行路径正好相反)比较的. 1 package test; 2 3 public class ArrayMax { 4 5 public static voi

数据结构(一)-----4种方法求最大子列和

数据结构(一)-----4种方法求最大子列和 1.暴力算法 /* 作者:mys 功能:求最大子列和 日期:2018/7/23 */ #include<stdio.h> #include<stdlib.h> #define N 1000 int maxSubseSum(int a[], int n); void main() { int a[N] = { 0 },i; for (i = 0; i < N; i++) a[i] = rand() % 10000; printf(&