/// <summary>
/// 按照升序排列的字符串,根据是否数字大小相连,重新显示字符串
/// </summary>
/// <param name="str">当前字符串</param>
/// <returns>返回新排列的字符串</returns>
public string GetStringNewList(string str)
{
string ret = "";
// string str = "1,2,3,4,5,8,9,11,12";
string[] arr = str.Split(',');
ret = arr[0];
for (int i = 1; i < arr.Length; i++)
{
int pre = int.Parse(arr[i - 1]);
int current = int.Parse(arr[i]);
int next = 0;
if (i < arr.Length - 1)
{
next = int.Parse(arr[i + 1]);
if (current - 1 == pre)
{
if (current + 1 == next)
{
}
else
{
ret += "-" + arr[i];
}
}
else
{
if (current + 1 == next)
{
ret += "," + arr[i];
}
else
{
ret += "," + arr[i];
}
}
}
else
{
if (current - 1 == pre)
{
ret += "-" + arr[i];
}
else
{
ret += "," + arr[i];
}
}
}
return ret;
}
有预先排序好么
static void Main(string[] args)
{
string strA = "1,3,4,5,8,9,11";
string[] strArr = strA.Split(',');
string strB = "";
int numFrom = int.Parse(strArr[0]);
int numTo = numFrom;
for (int i = 0; i < strArr.Length - 1; i++)
{
int intA = int.Parse(strArr[i]);
int intB = int.Parse(strArr[i + 1]);
if (intB - intA == 1)
{
numTo = intB;
}
else
{
if (numFrom == numTo)
{
strB += numFrom + ",";
}
else
{
strB += numFrom + "-" + numTo + ",";
}
numFrom = intB;
}
if (i == strArr.Length - 2)
{
numTo = int.Parse(strArr[strArr.Length - 1]);
if (numFrom == numTo)
{
strB += numFrom + ",";
}
else
{
strB += numFrom + "-" + numTo + ",";
}
}
}
Console.WriteLine(strA);
Console.Write(strB);
Console.ReadKey();
}
记得去掉最后面的那个逗号
numFrom = intB;
numTo = intB;
不好意思 不小心删掉了这句
static void Main(string[] args) { string strA = "1,3,4,5,8,9,11"; string[] strArr = strA.Split(','); string strB = ""; int numFrom = int.Parse(strArr[0]); int numTo = numFrom; for (int i = 0; i < strArr.Length - 1; i++) { int intA = int.Parse(strArr[i]); int intB = int.Parse(strArr[i + 1]); if (intB - intA == 1) { numTo = intB; } else { if (numFrom == numTo) { strB += numFrom + ","; } else { strB += numFrom + "-" + numTo + ","; } numFrom = intB; numTo = intB; } if (i == strArr.Length - 2) { numTo = int.Parse(strArr[strArr.Length - 1]); if (numFrom == numTo) { strB += numFrom + ","; } else { strB += numFrom + "-" + numTo + ","; } } } Console.WriteLine(strA); Console.Write(strB); Console.ReadKey(); }
public class KeyItems { List<int> keys = new List<int>(); Dictionary<int, List<int>> result = new Dictionary<int, List<int>>(); public KeyItems(string keyStr) { keys = keyStr.Split(',').Select(n => int.Parse(n)).ToList(); Compare(); } public override string ToString() { return string.Join(",", result .Select(n => n.Value.Count == 1 ? n.Value[0].ToString() : string.Format("{0}-{1}", n.Value[0].ToString(), n.Value[n.Value.Count - 1].ToString())).ToArray()); } void Compare() { int key = -1; for (int i = 0; i < keys.Count; i++) { if (Compare(keys[i], i==keys.Count-1?Convert.DBNull as int?:keys[i + 1])) { if (key == -1) key = i; if (result.Keys.Contains(key)) { result[key].Add(keys[i]); } else { result.Add(i, new List<int> { keys[i] }); } } else { if (key != -1) { result[key].Add(keys[i]); key = -1; } else { result.Add(i, new List<int> { keys[i] }); } } } } bool Compare(int? a, int? b) { if (b.HasValue) { if (b - a == 1) return true; } return false; } string text = "1,3,4,5,6,8,9,11,13,15,16,17,18,20"; MessageBox.Show(new KeyItems(text).ToString());
这是传说中的狼不大表达式么 看不懂额
标准的递归题目,此题用递归甚是合适,代码应该比楼上各位的少得多。
请问怎么递归?
var str = "1,3,4,5,8,9,13,14,15,16"; var array = str.Split(',').Select(t => Convert.ToInt32(t)).ToArray(); IDictionary<int, List<int>> keys = new Dictionary<int, List<int>>(); int key = Int32.MinValue; for (var i = 0; i < array.Count(); i++) { if (i == 0 || (array[i] - array[i - 1] > 1)) { key = array[i]; keys.Add(array[i], new List<int> { array[i] }); } else { keys[key].Add(array[i]); } } return string.Join(",", keys.Select(t => t.Value.Count == 1 ? t.Key.ToString() : t.Value.First() + "-" + t.Value.Last())).ToArray());