zoukankan      html  css  js  c++  java
  • 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 = { 1, 2, 3, 4, 5, 6, 7 };
                object[] names = { "张三", "李FatSoFat shit", "王五" };
    
                object[] pers = { new Person() { Name = "张三", Age = 19 }, new Person() { Name = "李四", Age = 20 }, new Person() { Name = "王五", Age = 22 } };
    
                //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[0];
                for (int i = 0; i < nums.Length; i++)
                {
                    //委托 : max-nums[i]
                    if (del(max, nums[i]) < 0)//比较的方式 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; }
        }
    }
  • 相关阅读:
    linux里忘记root密码解决办法
    Oracle怎样方便地查看报警日志错误
    清空文件内容
    channel c3 disabled, job failed on it will be run on another channel
    未能启动虚拟电脑,由于下述物理网卡找不到,你可修改虚拟电脑的网络设置或停用之
    DG下手工处理v$archive_gap方法
    Vue.js + Element.ui 从搭建环境到打包部署
    js判断json对象是否为空
    获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
    DIV的内容自动换行
  • 原文地址:https://www.cnblogs.com/suanshun/p/7001674.html
Copyright © 2011-2022 走看看