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();
        }
    }
}
posted @ 2019-07-28 19:54  Tuzilow  阅读(4001)  评论(2编辑  收藏  举报
TOP