在c#中查找连续数字

时间:2017-06-03 04:50:30

标签: c#

这是我的练习..

编写程序并要求用户输入一些用连字符分隔的数字。算出数字是否连续。

例如,如果输入为5-6-7-8-9,则会显示消息:Consecutive。如果输入5-1-8-2显示Not Consecutive

这是我的方法。

namespace ConsoleApp7
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter numbers separate by hypen : ");
            var name = Console.ReadLine();
            var numarray = name.Split('-');
            for (var i = 0;i<=numarray.Length-1;i++ )
            {
                if (i>1 && (Convert.ToInt32(numarray[i]) != Convert.ToInt32(numarray[i - 1])+1))
                {
                    Console.WriteLine("Not Consecutive");
                    break;
                }
                if (i == numarray.Length-1)
                {
                    Console.WriteLine("Consecutive");
                }

            }
        }
    }
}

它有效。有没有更好/简化的方法。

5 个答案:

答案 0 :(得分:2)

你可以像这样简化,

 static void Main(string[] args)
    {
        Console.WriteLine("Enter numbers separate by hypen : ");
        var name = Console.ReadLine();
        int[] numarray = Array.ConvertAll(name.Split('-'), int.Parse); 
        if (IsSequential(numarray))
        {
            Console.WriteLine("Consecutive");
        }
        else
        {
            Console.WriteLine("Not Consecutive");                 
        }
    }
    static bool IsSequential(int[] array)
    {
        return array.Zip(array.Skip(1), (a, b) => (a + 1) == b).All(x => x);
    }

答案 1 :(得分:2)

这是最有效的方法:

using System;

namespace ConsoleApp7
{
    public class Program
    {
        public static void Main(string[] args)
        {
        Console.WriteLine("Enter numbers separate by hypen : ");
        var name = Console.ReadLine();
        var numarray = name.Split('-');
        int firstValue = Convert.ToInt32(numarray[0]);

        bool cons = true;
        for (var i = 0;i<numarray.Length;i++ )
        {
            if (Convert.ToInt32(numarray[i])-i != firstValue)
            {
                cons = false;                   
                break;
            }
        }
        if (cons)
        {
            Console.WriteLine("Consecutive");
        }
        else
        {
            Console.WriteLine("Not Consecutive");
        }
    }
}

}

https://dotnetfiddle.net/P8VGjG

答案 2 :(得分:2)

考虑这样的方法:

    static void Main(string[] args)
    {
        Console.WriteLine("Enter numbers separate by hypen : ");
        var name = Console.ReadLine();
        var numarray = name.Split('-');
        var lower = int.Parse(numarray[0]);
        Console.WriteLine(Enumerable.Range(lower, numarray.Length)
            .Select(z => z.ToString()).SequenceEqual(numarray)
            ? "Consecutive"
            : "Not");

        Console.ReadLine();
    }

(更好的是,如果第一个条目不是数字,请使用TryParse)

答案 3 :(得分:0)

我尝试了一下,效果很好,它是连续的向前和向后。 这也很简单。看起来很长,因为我喜欢评论。

 static void Main(string[] args)
    {
        //This Variables checks if the secuence continues
        bool secuence = true;

        //Ask Receive Input
        Console.Write("Enter dashed numbers: ");
        string[] input = Console.ReadLine().Split('-');

        //Convert to Integer
        int[] numbers = Array.ConvertAll(input, int.Parse);

        //Loop Through all the values
        for(int i = 1; i<numbers.Length; i++)
        {
            //check if current value is +1 greater than last value
            if (numbers[i] + 1 == numbers[i - 1] || numbers[i]-1 == numbers[i - 1] && secuence == true)
            {   //continue counting if consecutive
                secuence = true;
            }
            else
            {   //Stop Counting if not consecutive
                secuence = false;
            }
        }
        //Workout the result
        if (secuence)
        {
            Console.WriteLine("The Numbers are consecutive");
        }
        else 
        { 
            Console.WriteLine("They are not consecutive"); 
        }
    }

答案 4 :(得分:0)

这是我对问题的回答。它适用于场景

  1. 3- ==> 不连续
  2. 4 => 不连续
  3. 3-4-5-6 => 连续
  4. 3-4-5- => 连续
  5. 3-4-5-5-6 => N
using System;

namespace ConsecutiveNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the number seperated by hyphen : ");
            var input = Console.ReadLine();
            var numArray = input.Split('-');
            var firstValue = numArray[0];
            var output = "Consecutive";

            if (numArray.Length > 2)
            {
                for (var i = 0; i < numArray.Length; i++)
                {
                    if (!string.IsNullOrWhiteSpace(numArray[i]))
                    {
                        if (i != 0)
                        {
                            if (int.Parse(numArray[i]) - int.Parse(firstValue) == 1)
                            {
                                //Console.WriteLine(int.Parse(numArray[i]) - int.Parse(firstValue));
                                firstValue = numArray[i];
                            }
                            else
                            {
                                output = "Not Consecutive";
                                break;
                            }
                        }
                        
                    }
                }
            }
            else
            {
                output = "Not Consecutive";
            }

            Console.WriteLine(output);
        }
        
    }
}