# 一个比较有意思的题目，请求算法思路。该怎么解决

www.MyException.Cn  网友分享于：2013-01-29  浏览：12次

2011834310
2011834311
2011834312
2011834313

2011834310-3

2011834310
2011834311
2011834312
2011834313
2011834318
2011834319
2011834320

2011834310-3   2011834318-20

------解决方案--------------------
1 数组排序
2 循环比较, 记录跨越的次数生成存储结果的数组.
3 循环生成 结果数组.

------解决方案--------------------
long[] input = new long[]{2011834310,
2011834311,
2011834312,
2011834313,
2011834318,
2011834319,
2011834320};
int mark = 0;
int current = 0;
while (current < input.Length)
{
mark = current;
current++;
while (current < input.Length && input[current] - input[current - 1] == 1)
{
current++;
}
Console.Write(input[mark] + "- ");
string str1 = input[mark].ToString();
string str2 = input[current - 1].ToString();
int index = 0;
while (str1[index] == str2[index])
{
index++;
}
Console.WriteLine(str2.Substring(index));
}
------解决方案--------------------

------解决方案--------------------
\\封装一下，并考虑位数差，做成这样
public class bas
{
private long startvalue, lastvalue;

public long Lastvalue
{
get { return lastvalue; }
}

public long Startvalue
{
get { return startvalue; }
}

public bas(long i)
{
startvalue = lastvalue = i;
}

private string calString()
{
StringBuilder sn1=new StringBuilder(startvalue.ToString()), sn2=new StringBuilder(lastvalue.ToString()),tmps=new StringBuilder();
if(sn2.Length> sn1.Length)
{
sn1.Insert(0, "0 ", sn2.Length - sn1.Length);
}

for (int index = 0; index < sn2.Length;index++ )
{
if (sn2[index] != sn1[index])
{
if (index > 0) tmps.Append(sn2.ToString(0, index ));
tmps.Append(sn1.ToString(index, sn1.Length - index));
tmps.Append( "- ");
tmps.Append(sn2.ToString(index, sn2.Length - index));
break;
}
}
if(tmps.Length!=0)return tmps.ToString();
else return sn1.ToString();
}

{
if (i == lastvalue + 1) {
lastvalue = i;
return null; }
else
{
string ret = calString();
startvalue = lastvalue = i;
return ret;
}