首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请教,这个Link方法查询,如何写

2013-02-20 
请问,这个Link方法查询,怎么写?Listint list new Listint(){6,1,3,0,14,5,7,9,2,1,8,3,2,4}将以上

请问,这个Link方法查询,怎么写?
List<int> list = new List<int>(){6,1,3,0,14,5,7,9,2,1,8,3,2,4};

将以上集合中元素存在连续奇数的,提取出来

提取结果为:{1,3}、{5,7,9}
[解决办法]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 6, 1, 3, 0, 14, 5, 7, 9, 2, 1, 8, 3, 2, 4 };
            List<List<int>> result = new List<List<int>>();
            if (list.Count() < 2) return;
            List<int> cur = list[0] % 2 == 1 ? new List<int>() { list[0] } : new List<int>();
            list.Skip(1).Aggregate(list[0], (pre, current) =>
                {
                    if (current == pre + 2 && pre % 2 == 1)
                        cur.Add(current);
                    if (current != pre + 2 && current % 2 == 1)
                    {
                        if (cur.Count > 1) result.Add(cur.ToList());
                        cur = new List<int>() { current };
                    }
                    return current;
                });
            foreach (List<int> item in result)
                Console.WriteLine("{{ {0} }}", string.Join(", ", item));
        }
    }
}

[解决办法]
List<int[]> resultsList = new List<int[]>();
List<int> list = new List<int>() { 6, 1, 3, 0, 14, 5, 7, 9, 2, 1, 8, 3, 2, 4 };


int flag = 0;
list.Where((x, y) =>
{
if (y < list.Count - 1 && (x % 2 == 1 && x + 2 == list[y + 1] 
[解决办法]
 x % 2 == 1 && x - 2 == list[y - 1]))
{
flag++;
return true;
}
if (flag > 1)
{
int[] results = list.Where((xx, yy) => yy >= y - flag && yy < y).ToArray();
resultsList.Add(results);
flag = 0;
}
return false;
}).ToList().ForEach(x => Console.Write(x + " "));
Console.Write("<br/>" + "得到的各个序列为:<br/>");
foreach (int[] results in resultsList)
{
foreach (int result in results)
Console.Write(result + " ");
Console.WriteLine();
}


[解决办法]
更正一下,修正最后2个元素是连续奇数的问题:
List<int[]> resultsList = new List<int[]>();
List<int> list = new List<int>() { 6, 1,3, 0, 14, 5, 7, 9, 2, 1, 8, 3, 2, 8, 11, 13};
int flag = 0;
list.Where((x, y) =>
{
if (y < list.Count - 1 && (x % 2 == 1 && x + 2 == list[y + 1] 
[解决办法]
 x % 2 == 1 && x - 2 == list[y - 1]))
{
flag++;
return true;
}
if (y == list.Count - 1 && flag > 0 && x % 2 == 1 && x - 2 == list[y - 1])
{
flag++;
int[] results = list.Where((xx, yy) => yy > y - flag && yy <= y).ToArray();
resultsList.Add(results);
flag = 0;
return true;
}
if (flag > 1)
{
int[] results = list.Where((xx, yy) => yy >= y - flag && yy < y).ToArray();
resultsList.Add(results);
flag = 0;
}
return false;
}).ToList().ForEach(x => Console.Write(x + " "));
Console.Write("\r\n" + "得到的各个序列为:\r\n");
foreach (int[] results in resultsList)
{
foreach (int result in results)
Console.Write(result + " ");
Console.WriteLine();
}

输出:
1 3 5 7 9 11 13 
得到的各个序列为:
1 3 
5 7 9 
11 13 

[解决办法]
最后,我给个写法简单的:
List<int[]> resultsList = new List<int[]>();
List<int> list = new List<int>() { 6, 1, 3, 0, 14, 5, 7, 9, 2, 1, 8, 3, 2, 8, 11, 13 };


int flag = 1, index = 1;
list.Concat(new int[] { 0 }).Aggregate((pre, current) =>
{
if (current % 2 == 1 && current == pre + 2)
flag++;
else
{
if (flag > 1)
{
int[] results = list.Where((x, y) => y >= index - flag && y < index).ToArray();
resultsList.Add(results);
flag = 1;
}
}
index++;
return current;
});
foreach (int[] results in resultsList)
{
string s = "{" + string.Join(",", results.Select(x => x.ToString()).ToArray()) + "}";
Console.WriteLine(s);
}


输出:
{1,3}
{5,7,9}
{11,13}

[解决办法]
static void Fun(List<int> list, int n, List<int> ary)
        {
            if (n > 0 && n < list.Count && list[n] % 2 == 1 && list[n - 1] + 2 == list[n])
            {
                if (ary.Count == 0)
                    ary.Add(list[n - 1]);
                ary.Add(list[n]);
                Fun(list, n + 1, ary);
            }
            else
            {
                if (ary.Count > 0)
                    Console.WriteLine("{{ {0} }}", string.Join(",", ary));
                if (n < list.Count)
                    Fun(list, n + 1, new List<int>());
            }
        }

热点排行