• C#:使用Hashtable实现输出那些用户发表主题最多的信息


    构思:先计算各自的数量,那些数量最多,输出详细信息

    具体算法如下:

        public class Count
        {
            #region 计算各实体数量
            public static Hashtable EntityCount(DataTable dt, string clmnUid)
            {
                //List<string> lst = new List<string>();
                Hashtable uidsCount = new Hashtable();
    
                foreach( DataRow row in dt.Rows)
                {
                    string uid = row[clmnUid].ToString();
                    if (uidsCount.Contains(uid))
                    {
                        uidsCount[uid] = (int)uidsCount[uid] + 1;
                    }
                    else
                    {
                        uidsCount.Add(uid,1);
                    }
                }
                return uidsCount;
            }
            #endregion
    
            #region 获取数量最多的实体
    
            public static IList<string> MaxCountUid(Hashtable uidsCount,bool listResult)
            {
                int maxCount = 0;
                IList<string> uids = new List<string>();
                int value = 0;
                foreach (DictionaryEntry kv in uidsCount)
                {
                    value = (int)kv.Value;
                    if (value > maxCount)
                    {
                        maxCount = value;
                        uids.Clear();
                        uids.Add(kv.Key.ToString());
                    }
                    else if(value == maxCount)
                    {
                        uids.Add(kv.Key.ToString());
                    }
                }
                return uids;
            }
    
            public static Hashtable MaxCountUid(Hashtable uidsCount)
            {
                int maxCount = 0;
                Hashtable uids = new Hashtable();
                int value = 0;
                foreach (DictionaryEntry kv in uidsCount)
                {
                    value = (int)kv.Value;
                    if (value > maxCount)
                    {
                        maxCount = value;
                        uids.Clear();
                        uids.Add(kv.Key,kv.Value);
                    }
                    else if (value == maxCount)
                    {
                        uids.Add(kv.Key, kv.Value);
                    }
                }
                return uids;
            }
    
            #endregion
    
            #region 输出用户的ID、姓名、数量
    
            public static void PrintMaxCountName(IList<string> uids,DataTable users, string clmnUid, string clmnName)
            { 
                foreach(DataRow dr in users.Rows)
                {
                    string strUid = dr[clmnUid].ToString();
                    foreach(string uid in uids)
                    {
                        if (uid == strUid)
                        {
                            string strName = dr[clmnName].ToString();
                            Console.WriteLine(strUid + ":" + strName);
                        }
                    }
                }
            }
    
            public static void PrintMaxCountName(Hashtable uids, DataTable users, string clmnUid, string clmnName)
            {
                foreach (DataRow dr in users.Rows)
                {
                    string strUid = dr[clmnUid].ToString();
                    foreach (DictionaryEntry kv in uids)
                    {
                        if (kv.Key.ToString() == strUid)
                        {
                            string strName = dr[clmnName].ToString();
                            Console.WriteLine(strUid + ":" + strName +"(" + kv.Value.ToString() + ")");
                        }
                    }
                }
            }
    
            #endregion
        }
    View Code

    构造测试数据:

        public class Data
        {
            private string[] usersColumns = { "uid", "name" };
            private string[] subjectsColumns = { "title", "uid", "time" };
    
            public DataTable initUsers()
            {
                DataTable users = new DataTable();
                foreach (string columnName in usersColumns)
                {
                    users.Columns.Add(columnName);
                }
    
                for (int i = 0; i < 10;i++ )
                {
                    users.Rows.Add("uid"+i,"name"+i);
                }
    
                return users;
            }
    
            public DataTable initSujects()
            {
                DataTable subjects = new DataTable();
                foreach (string columnName in subjectsColumns)
                {
                    subjects.Columns.Add(columnName);
                }
    
                for (int i = 0; i < 123; i++)
                {
                    subjects.Rows.Add("title" + i, "uid" + i % 10,"time" + 1);
                }
    
                return subjects;
            }
        }
    View Code

    测试代码:

        class Program
        {
            
            static void Main(string[] args)
            {
                const string clmnUid = "uid";
                const string clmnName = "name";
                Data data = new Data();
                DataTable users = data.initUsers();
                DataTable subjects = data.initSujects();
    
                Hashtable uidsCount = Count.EntityCount(subjects, clmnUid);
                Hashtable maxCountUids = Count.MaxCountUid(uidsCount);
                Count.PrintMaxCountName(maxCountUids,users,clmnUid,clmnName);
    
                Console.Read();
            }
        }
    View Code

    输出结果:
    uid0:name0(13)

    uid1:name1(13)

    uid2:name2(13)

  • 相关阅读:
    A Node Influence Based Label Propagation Algorithm for Community detection in networks 文章算法实现的疑问
    Fast Newman-FN算法以及模块度定义介绍
    Label Propagation Algorithm LPA 标签传播算法解析及matlab代码实现
    设计一个smartnic
    Intel GEN11 GPU
    Intel GEN9 GPU
    Shared Virtual Memory (SVM) Functions
    connect via ssh to virtualbox guest vm without knowing ip address
    smartnic
    技术精品翻译
  • 原文地址:https://www.cnblogs.com/shenchao/p/4743935.html
Copyright © 2020-2023  润新知