我想在从0到31的偶然数字的有序int [8]中找到所有连续数字.连续数字必须是最小长度3和最多5个数字.
在示例中,最后一个给我一个非常现实的问题.
例如:
int[] = new int[] { 3,7,14,16,23, 28, 29 ,30 } // no result (28,29 is length of 2 numbers)
int[] = new int[] { 4,5,6,7,18, 19, 20 ,21 } // 4,5,6,7 (yes! length of 4!!)
int[] = new int[] { 2.3.4.5.6.7.8.9 } // two results : 2,3,4 and 5,6,7,8,9
Run Code Online (Sandbox Code Playgroud)
我不想要解决方案,只是一个如何处理问题的例子,因为我正在尝试使用将军而且我真的被困住了!
非常感谢您的帮助!
基督教
- 这是我开始的代码(不是我厨房的汤)
public partial class Test2 : Form
{
public Test2()
{
InitializeComponent();
}
private void Test2_Load(object sender, EventArgs e)
{
int[] numbers = new[] { 21, 4, 5, 22, 17, 6, 20, 23 };
// int[] numbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
foreach (Campo r in FindRanges(numbers, 3))
{
listBox1.Items.Add(string.Join(", ", r.Select(x => x.ToString()).ToArray()));
}
}
struct Campo : IEnumerable<int>
{
readonly int _start;
readonly int _count;
public Campo(int start, int count)
{
_start = start;
_count = count;
}
public int Start
{
get { return _start; }
}
public int Count
{
get { return _count; }
}
public int End
{
get { return _start + _count - 1; }
}
public IEnumerator<int> GetEnumerator()
{
for (int i = 0; i < _count; ++i)
{
yield return _start + i;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public static Campo operator +(Campo x, int y)
{
return new Campo(x.Start, x.Count + y);
}
public Campo removefirst()
{
return new Campo(this.Start + 3, this.Count);
}
public Campo removelast()
{
return new Campo(this.Start, this.Count - 1);
}
}
static IEnumerable<Campo> FindRanges(IEnumerable<int> source, int minCount)
{
var ordered = source.OrderBy(x => x);
Campo r = default(Campo);
foreach (int value in ordered)
{
if (r.Count == 0)
{
r = new Campo(value, 1);
continue;
}
if (r.Count == 5)
{
r = r.removefirst();
continue;
}
if (value == r.End)
{
continue;
}
if ((value == 0 || value == 8 || value == 16 || value == 24) && (r.Count > minCount))
{
continue;
}
if ((value == 7 || value == 15 || value == 23 || value == 31) && (r.Count == 1))
{
continue;
}
else if (value == r.End + 1)
{
r += 1;
}
else
{
if (r.Count >= minCount)
{
yield return r;
}
r = new Campo(value, 1);
}
}
if (r.Count >= minCount)
{
yield return r;
}
}
Run Code Online (Sandbox Code Playgroud)
}
我建议你举一些例子,然后把它们写在纸上.当您尝试手动解决它们并将其转换为代码时,请仔细研究您在直觉上所做的事情.
你可能想要计算你已经在序列中找到的值的数量,以及之前的值是多少......
抱歉更新晚了,但时间实在是太压抑了......
这是我的最终解决方案,具有所有必要的限制(满足我的需要)。谢谢你们
static IEnumerable<IEnumerable<int>> Sequences(IEnumerable<int> input, bool ascen = false, int min = 3)
{
int ord = ascen == false ? -1 : 1;
input = ord == -1 ? input.OrderByDescending(x => x) : input.OrderBy(x => x);
var seq = new List<List<int>>();
foreach (var i in input)
{
var existing = seq.FirstOrDefault(lst => lst.Last() + ord == i);
if ((existing == null) || (seq.Last().Count == 5) || i == 7 || i == 15 || i == 23)
seq.Add(new List<int> { i });
else
existing.Add(i);
}
return min <= 1 ? seq : seq.Where(lst => lst.Count >= min);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6687 次 |
最近记录: |