c# 判断连续数字。
1,2,3,42,4,5,6
如果是连续的一串数字,返回true
--------------------编程问答--------------------
2,4,5,6 算么?
1,3,5,7 算么? --------------------编程问答-------------------- 必须是连续的,递增或递减1 --------------------编程问答--------------------
string str ="987654321";--------------------编程问答-------------------- 3楼的 97421 你那返回的也是 TRUE 吧? --------------------编程问答--------------------
Regex r = new Regex(@"\d+");
Match m= r.Match(str);
if(m.Success)
{
bool success=true;
string value = m.Value;
char current = value[0];
for (int i =1; i < value.Length; i++)
{
if (current <= value[i])
{
success = false;
break;
}
current = value[i];
}
if(success)
MessageBox.Show(value);
}
先匹配数字, 然后计算前一个是不是比前前一个小.这个是逆序,改为 if (current>= value[i])是升序了
--------------------编程问答-------------------- 你这个数字式一个数字字符串还是一个数字的数组罗.把它用个嵌套循环一个一个比较就OK了. --------------------编程问答-------------------- Split(',') 然后再 decimal.Parse() 后判断。 --------------------编程问答--------------------
class CheckIncreaseOrDegression
{
//1 递增, -1 递减, 0 无序;
public static int checkArray(int[] nums)
{
if (nums[0] < nums[nums.Length - 1])
{
if (nums[nums.Length - 1] - nums[0] != nums.Length - 1)
return 0;
for (int i = 0; i < nums.Length - 2; i++ )
{
if (nums[i + 1] - nums[i] != 1)
return 0;
}
return 1;
}
else
{
if (nums[0] - nums[nums.Length - 1] != nums.Length - 1)
return 0;
for (int i = 0; i < nums.Length - 2; i++)
{
if (nums[i] - nums[i + 1] != 1)
return 0;
}
return -1;
}
}
}
--------------------编程问答-------------------- 顶了 ,学习了 --------------------编程问答-------------------- 判断头尾元素就行了。
private bool checknum(string v)
{
Regex r = new Regex(@"\d+");
Match m = r.Match(v);
if (m.Success)
{
string tem = m.Value;
int len = tem.Length;
if (len < 2) return true;
int step = 1;
bool asc = tem[0] < tem[1];//是否正序
for (int i = 0; i < len; i++)
{
if (i < len-1)
{
if (asc && Convert.ToInt32(tem[i]) + step != Convert.ToInt32(tem[i + 1]))return false;
else if (!asc && Convert.ToInt32(tem[i]) - step != Convert.ToInt32(tem[i + 1]))return false;
continue;
}
}
return true;
}
return false;
}
MessageBox.Show(checknum("654320") ? "正确" : "错误"); //错误
MessageBox.Show(checknum("123456") ? "正确" : "错误"); //正确
--------------------编程问答-------------------- 唉...这是小学数学题啊...
public static void Main(string[] args)
{
int [] strData = { 1, 2, 3, 4, 5, 7 };
if (strData[strData.Length-1]==strData[0]+strData.Length-1 || strData[0]==strData[strData.Length-1]-1)
{
Console.Write("True ");
}
else
{
Console.Write("False ");
}
Console.ReadKey();
}
string numbers = "5,2,3,4";--------------------编程问答-------------------- public static Boolean CheckNums(String s)
string[] nums = numbers.Split(',');
Array.Sort(nums);
return (int.Parse(nums[nums.Length - 1]) - int.Parse(nums[0])) == nums.Length - 1;
{
String[] strs = s.Split(',');
Int32[] nums = new Int32[strs.Length];
for (Int32 i = 0; i < strs.Length; i++)
{
nums[i] = Convert.ToInt32(strs[i]);
}
if (nums.Length == 1)
return true;
if (nums[0] > nums[1])
{
for (Int32 i = 0; i < nums.Length - 1; i++)
{
if (nums[i] != nums[i + 1] + 1)
return false;
}
return true;
}
else
{
for (Int32 i = 0; i < nums.Length - 1; i++)
{
if (nums[i] != nums[i + 1] - 1)
return false;
}
return true;
}
}
static void Main(string[] args)
{
String numStr = "7,6,5,4";
Console.WriteLine(CheckNums(numStr));
} --------------------编程问答-------------------- 1234321也算连续的吧 --------------------编程问答--------------------
--------------------编程问答--------------------
public static Boolean CheckNums(String s)
{
String[] strs = s.Split(',');
Int32[] nums = new Int32[strs.Length];
for (Int32 i = 0; i < strs.Length; i++)
{
nums[i] = Convert.ToInt32(strs[i]);
}
if (nums.Length == 1)
return true;
for (Int32 i = 0; i < nums.Length - 1; i++)
{
if ((nums[i] != nums[i + 1] + 1)||(nums[i] != nums[i + 1] - 1))
return false;
}
return true;
}
}
判断头尾是不行的,只能快速判断是不是可能是连续的数字...
比如 "1,2,5,4".... 你怎么判断? --------------------编程问答-------------------- 实现不难,谁能用linq实现…… --------------------编程问答-------------------- 判断头尾是个好思路! --------------------编程问答-------------------- [TestFixture]
public class TestContinueNumber
{
[Test]
public void Test()
{
Assert.IsTrue(CheckNumber("1,2,3,4,5"));
Assert.IsFalse(CheckNumber("1,2,3,4,6"));
}
public bool CheckNumber(string str)
{
var numbers = (from c in str.Split(',')
select Convert.ToInt32(c)).ToList();
var distinct = numbers.Distinct();
return numbers.Count > 0 && numbers.Count == distinct.Count() && numbers.Last() - numbers.First() == numbers.Count - 1;
}
}
--------------------编程问答-------------------- ++或-- --------------------编程问答--------------------
补充:.NET技术 , C#