哈哈1213 2020-02-14 12:40 采纳率: 0%
浏览 528
已采纳

(c#)一组数据每四个为一组,对组编号,奇偶数编号组分开保存在两个数组中

例如:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 想分成

1 2 3 4 9 10 11 12

5 6 7 8 13 14 15 16

这两个组。求大牛解答。。。

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-02-14 13:19
    关注
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Q1054358
    {
        class Program
        {
            static void Main(string[] args)
            {
                string s = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16";
                var query = s.Split(' ').Select((x, i) => new {v = int.Parse(x), i}).GroupBy(x => (x.i / 4) % 2).Select(x => x.Select(y => y.v).ToArray()).ToArray();
                Console.WriteLine(string.Join(" ", query[0].Select(x => x.ToString())));
                Console.WriteLine(string.Join(" ", query[1].Select(x => x.ToString())));
            }
        }
    }
    
    

    1 2 3 4 9 10 11 12
    5 6 7 8 13 14 15 16
    Press any key to continue . . .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?