C#基础算法题 找出最大值和最小值

找出最大值和最小值

题目要求

输入n个数,n<=100,找到其中最小的数和最大的数

实现代码

using System;

namespace _1.求最大最小
{
    class Program
    {
        public static int GetMax(int[] numbers)
        {
            int max = numbers[0];
            for (int i = 0; i < numbers.Length; i++)
            {
                if (max < numbers[i])
                {
                    max = numbers[i];
                }
            }
            return max;
        }
        public static int GetMin(int[] numbers)
        {
            int min = numbers[0];
            for (int i = 0; i < numbers.Length; i++)
            {
                if (min > numbers[i])
                {
                    min = numbers[i];
                }
            }
            return min;
        }
        static void Main(string[] args)
        {
            string[] temp = Console.ReadLine().Split(' ');
            int[] numbers = new int[temp.Length];
            for (int i = 0; i < temp.Length; i++)
            {
                numbers[i] = Convert.ToInt32(temp[i]);
            }

            Console.WriteLine("Max = " + GetMax(numbers));
            Console.WriteLine("Min = " + GetMin(numbers));
            Console.ReadKey();
        }
    }
}

相关文章

项目中经常遇到CSV文件的读写需求,其中的难点主要是CSV文件...
简介 本文的初衷是希望帮助那些有其它平台视觉算法开发经验的...
这篇文章主要简单记录一下C#项目的dll文件管理方法,以便后期...
在C#中的使用JSON序列化及反序列化时,推荐使用Json.NET——...
事件总线是对发布-订阅模式的一种实现,是一种集中式事件处理...
通用翻译API的HTTPS 地址为https://fanyi-api.baidu.com/api...