题干:
编程](二选一)连续数字
分值:300
程序执行时限: 600 ms
你的弟弟在纸上写了一些连续的整数,这些整数是乱序排列的,且每个整数只出现一次。淘气的弟弟把其中的一个整数擦掉了,你现在需要写程序找出他擦掉的是哪个数。
输入:
只有一行,用逗号分隔的若干个整数
输入约束:
至少1个,至多50个整数
每个整数位于闭区间[1,1000000000]
注意:弟弟一开始写的数字也位于这个区间。
输出:
只有一行,如果结果有多种可能,则将这多种可能的数字按由小到大的顺序输出,之间用逗号分隔,如果没有结果,则什么也不输出
举例1:
输入:
10,7,12,8,11
输出:
9
举例2:
输入
5,6,7,8
输出
4,9
举例3:
输入:
3,6
输出:
解释:
有时可能你的弟弟粗心写错了,这时你的程序不要输出任何数字。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
// while (true)
// {
// Console.WriteLine("输入:");
string line = Console.ReadLine();
string[] str = line.Split(',');
int[] resultArray = new int[str.Length];
for (int i = 0; i < resultArray.Length; i++)
{
resultArray[i] = Int32.Parse(str[i]);
}
Array.Sort(resultArray);
// Console.WriteLine("\n输出:");
Dialog(resultArray);
Console.ReadKey();
// Console.WriteLine("输入:");
// }
}
static void Dialog(int[] resultArray)
{
// if (IsLine(resultArray) && IsError(resultArray))
if (IsError(resultArray) || IsSame(resultArray) || IsMaxOutofArray(resultArray))
{
// Console.Write(" 题目不正确");
Console.Write("");
return;
}
if (RR(resultArray) != -1)
{
// Console.Write("不连续 " + RR(resultArray));
Console.Write( RR(resultArray));
return;
}
if (IsLine(resultArray))
{
if (resultArray[0] == 1)
{
// Console.Write("连续且为1 " + (resultArray[resultArray.Length - 1] + 1));
Console.Write( (resultArray[resultArray.Length - 1] + 1));
}
else
{
// Console.Write("连续不为1 " + (resultArray[0] - 1) + "," + (resultArray[resultArray.Length - 1] + 1));
Console.Write((resultArray[0] - 1) + "," + (resultArray[resultArray.Length - 1] + 1));
}
return;
}
}
/// <summary>
/// error =true = 题目有错误
/// </summary>
/// <param name="resultArray"></param>
/// <returns></returns>
static bool IsError(int[] resultArray)
{
for (int i = 0; i < resultArray.Length - 1; i++)
{
if (resultArray[i + 1] - resultArray[i] > 2)
{
return true;
}
}
return false;
}
/// <summary>
/// true=是连续的 只取两头数字输出
/// </summary>
/// <param name="resultArray"></param>
/// <returns></returns>
static bool IsLine(int[] resultArray)
{
for (int i = 0; i < resultArray.Length - 1; i++)
{
if (resultArray[i] + 1 != resultArray[i + 1])
{
return false;
}
}
return true;
}
static int RR(int[] resultArray)
{
List<int> temp = new List<int>();
for (int i = 0; i < resultArray.Length - 1; i++)
{
if (resultArray[i] + 1 != resultArray[i + 1])
{
// return resultArray[i] + 1;
temp.Add(resultArray[i] + 1);
}
}
if (temp.Count == 1)
{
return temp[0];
}
else
{
return -1;
}
}
/// <summary>
/// true ==有错误
/// </summary>
/// <param name="resultArray"></param>
/// <returns></returns>
static bool IsSame(int[] resultArray)
{
for (int i = 0; i < resultArray.Length - 1; i++)
{
if (resultArray[i] == resultArray[i + 1])
{
return true;
}
}
return false;
}
/// <summary>
/// true= 超出索引 有错误
/// </summary>
/// <param name="resultArray"></param>
/// <returns></returns>
static bool IsMaxOutofArray(int[] resultArray)
{
if (resultArray[resultArray.Length - 1] > 1000000000 || resultArray.Length <= 0 || resultArray.Length > 50)
{
return true;
}
return false;
}
}
}