C# 在c中查找连续数#
这是我的练习 编写一个程序,让用户输入几个用连字符分隔的数字。计算出这些数字是否连续 例如,如果输入为C# 在c中查找连续数#,c#,C#,这是我的练习 编写一个程序,让用户输入几个用连字符分隔的数字。计算出这些数字是否连续 例如,如果输入为5-6-7-8-9则显示一条消息:连续。如果输入5-1-8-2显示不连续 这是我的方法 namespace ConsoleApp7 { class Program { static void Main(string[] args) { Console.WriteLine("Enter numbers separate by
5-6-7-8-9
则显示一条消息:连续
。如果输入5-1-8-2
显示不连续
这是我的方法
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");
}
}
}
}
}
名称空间控制台App7
{
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“输入由hypen分隔的数字:”);
var name=Console.ReadLine();
var numarray=name.Split('-');
对于(var i=0;i1&&(Convert.ToInt32(numarray[i])!=Convert.ToInt32(numarray[i-1])+1))
{
控制台。写入线(“非连续”);
打破
}
if(i==numaray.Length-1)
{
控制台。写入线(“连续”);
}
}
}
}
}
它成功了。有没有更好/更简单的方法 你可以这样简化
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);
}
你可以这样简化
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);
}
这是最有效的方法:
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");
}
}
}
使用系统;
名称空间控制台App7
{
公共课程
{
公共静态void Main(字符串[]args)
{
Console.WriteLine(“输入由hypen分隔的数字:”);
var name=Console.ReadLine();
var numarray=name.Split('-');
int firstValue=Convert.ToInt32(numarray[0]);
bool-cons=true;
对于(var i=0;i而言,这是最有效的方法:
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");
}
}
}
使用系统;
名称空间控制台App7
{
公共课程
{
公共静态void Main(字符串[]args)
{
Console.WriteLine(“输入由hypen分隔的数字:”);
var name=Console.ReadLine();
var numarray=name.Split('-');
int firstValue=Convert.ToInt32(numarray[0]);
bool-cons=true;
对于(var i=0;i考虑如下方法:
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)考虑以下方法:
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,以防第一个条目不是数字)我尝试了这个方法,效果很好,前后连续。
它也很简单。它看起来很长,因为我喜欢评论
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");
}
}
static void Main(字符串[]args)
{
//此变量检查是否继续
布尔余弦=真;
//请求接收输入
控制台。写入(“输入虚线数字:”);
string[]input=Console.ReadLine().Split('-');
//转换为整数
int[]numbers=Array.ConvertAll(输入,int.Parse);
//循环遍历所有值
对于(inti=1;i我尝试了这个,它工作得很好,前后连续。
它也很简单。它看起来很长,因为我喜欢评论
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");
}
}
static void Main(字符串[]args)
{
//此变量检查是否继续
布尔余弦=真;
//请求接收输入
控制台。写入(“输入虚线数字:”);
string[]input=Console.ReadLine().Split('-');
//转换为整数
int[]numbers=Array.ConvertAll(输入,int.Parse);
//循环遍历所有值
对于(int i=1;i为什么需要for循环。您只需检查第一个值和下一个值是否为first value+1,然后我们就可以确定它是否连续i,因为指令5-7-8-9是否连续?@ISHIDA-这只适用于两个数字或固定数量的值。但在这里,用户可以输入任意数量的值values@Christos-不…<公司de>6
应该位于5
和7
的中间位置,这要视情况而定。什么构成“更好/简化”?您当然可以用更少的代码行来完成它(如下所示)但是,如果结果更难理解/调试/维护,它真的更好吗?为什么需要for循环。您只需检查第一个值和下一个值是否为first value+1,然后我们就可以确定它是连续的还是非连续的,因为instance 5-7-8-9是连续的还是非连续的?@ISHIDA-这只适用于两个数字或固定数字o但在这里用户可以输入任意数量的values@Christos-不…6
应该在5
和7
中间,这取决于它。什么构成“更好/简化”?你当然可以用更少的代码行来完成它(如下所示)但是,如果结果更难理解/调试/维护,这真的更好吗?这是其中LINQ真的很酷和所有的时间…但男孩是很难理解。这是一个时间,LINQ真的很酷和所有…但男孩是很难理解。我喜欢这个实现,是它只是PAR。ses它所需要的。如果我们添加空检查(不提供输入等),这也会带来简单性的提升。如果将If(Convert.ToInt32(numaray[i])-i!=firstValue)
替换为If(numaray[i]=(firstValue+i).ToString(),速度可能会更快
因为字符串->整数转换可能比整数->字符串转换慢。我喜欢这个实现,因为它只解析它需要的内容。如果我们添加空检查(没有给出输入等),这也会带来简单性的提升。如果用If(Convert.ToInt32(numarray[I])-I!=firstValue)替换的话,速度可能会更快(numaray[i]==(firstValue+i).ToString())
因为字符串->整数转换可能比整数->字符串转换慢。