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

获取数组三拇指定重出现次数的元素集(实例)

2012-12-20 
获取数组中指定重出现次数的元素集(实例)如A1中有如下数值6 12 16 21 24 36 48 3 9 13 18 21 33 45 48 1 6

获取数组中指定重出现次数的元素集(实例)
如A1中有如下数值6 12 16 21 24 36 48 3 9 13 18 21 33 45 48 1 6 10 16 22 26 31 34 46 12 24 27 32 36 42 48 1 13 16 21 25 31 37 41 46 49 3 8 12 18 24 28 33 36 48 1 5 11 17 21 26 29 41 

可按如下方式列出统计
【共00次】:02,04,07,14,15,19,20,23,30,35,38,39,40,43,44,47,
【共01次】:05,08,09,10,11,17,22,25,27,28,29,32,34,37,42,45,49,
【共02次】:03,06,13,18,26,31,33,41,46,
【共03次】:01,12,16,24,36,
【共04次】:21,48,
版块中有这样VB.NET贴,我粘贴到EXCEL中出错,烦请版主做一实例。在此感谢!!!
[最优解释]

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "6 12 16 21 24 36 48 3 9 13 18 21 33 45 48 1 6 10 16 22 26 31 34 46 12 24 27 32 36 42 48 1 13 16 21 25 31 37 41 46 49 3 8 12 18 24 28 33 36 48 1 5 11 17 21 26 29 41";
            var query = new string[] { string.Format("【共00次】:{0},", string.Join(",", Enumerable.Range(1, 49).Except(s.Split(' ').Select(x => int.Parse(x))).OrderBy(x => x).Select(x => x.ToString().PadLeft(2, '0')))) }.Union(s.Split(' ').Select(x => int.Parse(x))
                         .GroupBy(x => x)
                         .Select(x => new { x.Key, Count = x.Count() })
                         .GroupBy(x => x.Count)
                         .OrderBy(x => x.Key)
                         .Select(x => string.Format("【共{0}次】:{1},", x.Key.ToString().PadLeft(2, '0'), string.Join(",", x.Select(y => y.Key.ToString().PadLeft(2, '0')).OrderBy(y => y)))));
            foreach (var item in query)
            {
                Console.WriteLine(item);
            }
        }
    }
}


【共00次】:02,04,07,14,15,19,20,23,30,35,38,39,40,43,44,47,


【共01次】:05,08,09,10,11,17,22,25,27,28,29,32,34,37,42,45,49,
【共02次】:03,06,13,18,26,31,33,41,46,
【共03次】:01,12,16,24,36,
【共04次】:21,48,
Press any key to continue . . .
[其他解释]

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "6 12 16 21 24 36 48 3 9 13 18 21 33 45 48 1 6 10 16 22 26 31 34 46 12 24 27 32 36 42 48 1 13 16 21 25 31 37 41 46 49 3 8 12 18 24 28 33 36 48 1 5 11 17 21 26 29 41";
            var query = new string[] { string.Format("【共00次】:{0},", string.Join(",", Enumerable.Range(0, 50).Except(s.Split(' ').Select(x => int.Parse(x))).OrderBy(x => x).Select(x => x.ToString().PadLeft(2, '0')))) }.Union(s.Split(' ').Select(x => int.Parse(x))
                         .GroupBy(x => x)
                         .Select(x => new { x.Key, Count = x.Count() })
                         .GroupBy(x => x.Count)
                         .OrderBy(x => x.Key)
                         .Select(x => string.Format("【共{0}次】:{1},", x.Key.ToString().PadLeft(2, '0'), string.Join(",", x.Select(y => y.Key.ToString().PadLeft(2, '0')).OrderBy(y => y)))));
            foreach (var item in query)
            {
                Console.WriteLine(item);
            }
        }
    }
}


【共00次】:00,02,04,07,14,15,19,20,23,30,35,38,39,40,43,44,47,
【共01次】:05,08,09,10,11,17,22,25,27,28,29,32,34,37,42,45,49,
【共02次】:03,06,13,18,26,31,33,41,46,
【共03次】:01,12,16,24,36,
【共04次】:21,48,
Press any key to continue . . .
[其他解释]
vb.net 的写法 类似于楼上的C#
[其他解释]
可发送到我的邮箱中,tang200614@163.com,再次感谢!
------其他解决方案--------------------


版主,谢谢你的回复,本人刚接触这方面知识,实在是有点愚钝,能否做个EXCEL表格让我参考学习呢?
烦请发到我的tang200614@163.com邮箱中,或是共享在论坛下载页面里,也可供大家学习。在此不胜感激,得到了你及时的回复,谢谢!!!
[其他解释]
还有问题要问,A1,A2,A3,A4依此内推都有不同数值,又该怎么做?谢谢!
[其他解释]
Excel没有用过,你是说用VBA编写呢,还是用VB.NET做成Addin程序呢?但是原理应该大同小异。如果是VBA,可以去VB-VBA板块问问,或者去Excel方面的论坛问问。

热点排行