由用户输入一列整数,以空格分隔,写一个程序来判断该数列是否已经按降序排列,输出判断结果。

C#用字符串输入分割读数组

using System;



namespace ConsoleApp1

{

    class Program

    {

        static void Main(string[] args)

        {

            string str = Console.ReadLine();

            string[] a = str.Split(" ");

            bool flag = true;

            

            for(int i = 1; i < a.Length; i++)

            {

               

                if (Convert.ToInt32(a[i]) > Convert.ToInt32(a[i-1]))

                {

                    flag = false;

                }   

            }

            if (true == flag)

            {

                Console.WriteLine("该数列已排序");

            }

            else

            {

                Console.WriteLine("该数列未排列");

            }

        }

    }

}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/114801452