利用委托求任意数组的最大值
来源:互联网 发布:广寒仙子皎月淘宝价格 编辑:程序博客网 时间:2024/06/05 17:29
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespace _05_利用委托求任意数组的最大值
{
public delegate int DelGetMax(object o1,object o2);
class Program
{
static void Main(string[] args)
{
object[] nums = { 123, 456, 789, 147, 258, 369 };
object[] names = { "123", "sdsdsd", "456789", "qwertyuiop" };
//求年龄最大人名
object[] persons = { new Person() { Name = "张三", Age = 18 }, new Person() { Name = "李四", Age = 21 } };
//objectmax = DelGetMax(nums, C1).ToString();
//objectmax = DelGetMax(names, C2);
//objectmax= DelGetMax(nums,delegate(object o1,object o2) { return (int)o1-(int)o2; });
//objectmax = DelGetMax(nums,(o1,o2)=> { return (int)o1 - (int)o2; });
//objectmax= DelGetMax(names, delegate (object o1, object o2) { return((string)o1).Length - ((string)o2).Length; });//匿名函数(函数的简写形式,只调用一次可以这样写)
//objectmax = DelGetMax(names,(o1,o2)=> { return ((string)o1).Length -((string)o2).Length; });//lambda表达式(匿名函数的简写形式)
//objectmax = DelGetMax(persons, delegate (object o1, object o2) { return((Person)o1).Age-((Person)o2).Age; });
object max = DelGetMax(persons,(o1,o2)=>{return ((Person)o1).Age-((Person)o2).Age; });
Console.WriteLine(((Person)max).Name);//输出年龄最大人的姓名
Console.ReadKey();
}
///<summary>
///普通方法求整数数组的最大值
///</summary>
///<param name="nums"></param>
///<returns></returns>
static int GetMax(int[] nums)
{
//假设第一个元素最大
int max = nums[0];
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] > max)
{
max = nums[i];
}
}
return max;
}
///<summary>
///普通方法求字符串数组的最大值
///</summary>
///<param name="nums"></param>
///<returns></returns>
static string GetMax(string[] nums)
{
string max = nums[0];
for (int i = 0; i < nums.Length; i++)
{
if (nums[i].Length > max.Length)
{
max = nums[i];
}
}
return max;
}
///<summary>
///利用委托求任意数组的最大值
///</summary>
///<param name="nums"></param>
///<returns></returns>
static object DelGetMax(object[] nums, DelGetMax del)
{
object max = nums[0];
for (int i = 0; i < nums.Length; i++)
{
//由于只有比较的方式不同,所以可以传一个比较的方式 即传一个委托进来
if (del(max, nums[i]) < 0)
{
max = nums[i];
}
}
return max;
}
///<summary>
///求整形最大值
///</summary>
///<param name="o1"></param>
///<param name="o2"></param>
///<returns></returns>
static int C1(object o1, object o2)
{
int n1 = (int)o1;
int n2 = (int)o2;
return n1 - n2;
}
///<summary>
///求字符串类型最大值
///</summary>
///<param name="o1"></param>
///<param name="o2"></param>
///<returns></returns>
static int C2(object o1, object o2)
{
string s1 = (string)o1;
string s2 = (string)o2;
return s1.Length - s2.Length;
}
}
///<summary>
///创建一个Person类,利用委托求年龄最大人的姓名
///</summary>
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
- 利用委托求任意数组的最大值
- 求任意多个数的最大值(利用可变参数列表)
- 关于任意数字类型的数组求最大值解决办法
- 求数组中任意连续位置元素和的最大值
- 求数组的最大值
- C#之使用委托查找任意单一类型数组的最大值
- 利用递归方法求一个数组的最大值
- Java利用递归的方法求整数数组中的最大值
- poj2479-在一个数组中求任意不相交的两个子数组之和的最大值
- 求任意多个数的最大值
- 利用c++模板实现求数组最大值
- javascript利用apply求数组最大值
- 使用委托(或匿名函数)求一个未知类型的数组最大值
- 求子数组的最大值
- 求子数组的最大值
- 求数组的最大值,次大值
- 求数组最大值的下标
- 利用数组作参数求最大值(感觉学了数组能很快的解决问题)
- 如何将本地代码使用Git上传至Github
- HDU 6156 2016ICPC网络赛 G: Palindrome Function(数位DP)
- 修改Ubuntu的aptget源为阿里源的方法
- 类型基础
- 树备份
- 利用委托求任意数组的最大值
- hdu6108 小C的倍数问题
- 最小生成树--kruskal
- Spring Security实战
- 关联查询是否需要先缩小查询范围?
- Python爬取知乎回答信息碰到:Max retries exceeded with URL
- 浅淡JSP(一)
- Java的设计模式-适配器和单例模式
- Android 网格视图(GridView) 实践