欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-08-31 15:05:00
找出最大值和最小值 题目要求 输入n个数,n numbers[i]) { min = numbers[i]; } } return min; } static void Main(string[] args) { string[] temp = Console.ReadLine().Split(' ......

找出最大值和最小值

题目要求

输入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();
        }
    }
}