c# 求数组的最大值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _08求数组的最大值
{
public delegate int DelCompare(object o1, object o2);
class Program
{
static void Main(string[] args)
{
object[] nums = { , , , , , , };
object[] names = { "张三", "李FatSoFat shit", "王五" }; object[] pers = { new Person() { Name = "张三", Age = }, new Person() { Name = "李四", Age = }, new Person() { Name = "王五", Age = } }; //object:任意传任意类型
//object[]:只能object类型的数组
//object max = GetMax(nums, C1);
//object max = GetMax(names, C2);
//object max = GetMax(pers, C3);
//Console.WriteLine(((Person)max).Age);
//Console.WriteLine(((Person)max).Name);
//Console.ReadKey(); object max = GetMax(names, (o1, o2) => { return ((string)o1).Length - ((string)o2).Length; });
Console.WriteLine(max); object max2 = GetMax(pers, (o1, o2) => { return ((Person)o1).Age - ((Person)o2).Age; });
Console.WriteLine(((Person)max2).Name);
Console.WriteLine(((Person)max2).Age);
Console.ReadKey(); } static object GetMax(object[] nums, DelCompare del)//外面传进来一个比较的方式
{
object max = nums[];
for (int i = ; i < nums.Length; i++)
{
//委托 : max-nums[i]
if (del(max, nums[i]) < )//比较的方式 if(nums[i]>max)
{
max = nums[i];
}
}
return max;
} //static int C1(object o1, object o2)
//{
// int n1 = (int)o1;
// int n2 = (int)o2;
// return n1 - n2;
//} //static int C2(object o1, object o2)
//{
// string s1 = (string)o1;
// string s2 = (string)o2;
// return s1.Length - s2.Length;
//}
//static int C3(object o1, object o2)
//{
// Person p1 = (Person)o1;
// Person p2 = (Person)o2;
// return p1.Age - p2.Age;
//} #region MyRegion
//static object GetMax(object[] names)
//{
// object max = names[0];
// for (int i = 0; i < names.Length; i++)
// {
// if (names[i].Length > max.Length)
// {
// max = names[i];
// }
// }
// return max;
//}
//static object GetMax(object[] pers)
//{
// object pMax = pers[0];
// for (int i = 0; i < pers.Length; i++)
// {
// if (pers[i].Age > pMax.Age)
// {
// pMax = pers[i];
// }
// }
// return pMax;
//}
#endregion } class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
}
c# 求数组的最大值的更多相关文章
- js求数组的最大值--奇技淫巧和笨方法
		
写这篇文章的原因 我目前做的项目很少用到算法,于是这方面的东西自然就有点儿生疏.最近的一次编码中遇到了从数组中获取最大值的需求,当时我不自觉的想到了js的sort()函数,现在想来真是有些“罪过”,当 ...
 - YTU 2642: 填空题:类模板---求数组的最大值
		
2642: 填空题:类模板---求数组的最大值 时间限制: 1 Sec 内存限制: 128 MB 提交: 646 解决: 446 题目描述 类模板---求数组的最大值 找出一个数组中的元 ...
 - 使用JavaScript·求数组的最大值和最小值
		
前言 在数组中并没有提供arr.max()和arr.min()这样的方法.那么是不是可以通过别的方式实现类似这样的方法呢?那么今天我们就来整理取出数组中最大值和最小值的一些方法. 法一:其实利用 ...
 - 【RMQ问题】求数组区间最大值,NYOJ-1185-最大最小值
		
转自:http://blog.csdn.net/lilongherolilong/article/details/6624390 先挖好坑,明天该去郑轻找虐 RMQ(Range Minimum/Max ...
 - C#中求数组的子数组之和的最大值
		
<编程之美>183页,问题2.14——求子数组的字数组之和的最大值.(整数数组) 我开始以为可以从数组中随意抽调元素组成子数组,于是就有了一种想法,把最大的元素抽出来,判断是大于0还是小于 ...
 - 求数组的最小数、最大值,求一组数的平均数,sort函数详解,类数组转数组
		
求数组的最小值和最大值 //求数组当中最大值和最小值 var arr=[3,2,6,1,45,23,456,23,2,6,3,45,37,89,30]; //第一种方法 根据排序方法来求最大值和最小值 ...
 - JAVA 求数组中的最大值
		
package Code411;//求数组的最大值public class CodeArrayMax { public static void main(String[] args) { int ar ...
 - class 3 求数组中的最大值(单元测试)
		
1.问题引出: int Largest(int list[], int length) { int i,max; ; i < (length – ); i ++ ) { if(list[i] & ...
 - 【编程题目】一个整数数组,长度为 n,将其分为 m 份,使各份的和相等,求 m 的最大值★★ (自己没有做出来!!)
		
45.雅虎(运算.矩阵): 2.一个整数数组,长度为 n,将其分为 m 份,使各份的和相等,求 m 的最大值 比如{3,2,4,3,6} 可以分成 {3,2,4,3,6} m=1; {3,6}{2,4 ...
 
随机推荐
- yum 报错
			
Loaded plugins: fastestmirror, prestoLoading mirror speeds from cached hostfileCould not retrieve mi ...
 - 配置和启动脚本(bash shell学习01)
			
bash是 Bourne Again Shell简称 ,从unix系统的sh发展而来 查看当前shellecho $SHELL查看系统支持的shellcat /etc/shells cd /binls ...
 - 【转】使用JMeter 完成常用的压力测试(一)
			
本文介绍了 JMeter 相关的基本概念.并以 JMeter 为例,介绍了使用它来完成最常用的三种类型服务器,即 Web服务器.数据库服务器和消息中间件,压力测试的方法.步骤以及注意事项. 讲到测试, ...
 - Git使用点滴记录: You have no permission to access this repo.
			
代码托管在https://coding.net上面,之前Git用https的方式都好好的,没有出什么问题.结果今天git pull代码的时候一直提示以下信息: remote: Coding.net T ...
 - 如何安装pip
			
1.安装python之后进入到python的安装目录,里面有个Scripts文件夹,打开文件夹里面有个easy_install.exe的文件 2.打开控制台,到easy_install.exe的文件地 ...
 - postgresql 9.5 pgpool 主从复制 以及错误解决
			
PostgreSQL+pgpool-II复制方案 这里不做功能的描述,只写搭建的过程和遇到的一些问题 1 系统 [root@mysqlhq ~]# cat /etc/redhat-release Ky ...
 - (转) Docker - Docker1.12服务发现,负载均衡和Routing Mesh
			
看到一篇介绍 Docker swarm以及如何编排的好文章,挪放到这里,自己学习的同时也分享出来. 原文链接: http://wwwbuild.net/dockerone/414200.html -- ...
 - 执行make出现“Warning: File `xxx.c' has modification time 2.6e+04 s in the future“警告的解决方法
			
错误描述: 执行make命令时出现"make[2]: Warning: File `xxx.c' has modification time 1.6e+05 s in the future ...
 - ansible里的item和with_items
			
################################## 变量命名 变量名仅能由字母.数字和下划线组成且只能以字母开头. ################################# ...
 - day-7心得
			
面向对象高级语法部分 经典类vs新式类 把下面代码用python2 和python3都执行一下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 ...