前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >常见程序题——算法

常见程序题——算法

作者头像
指尖改变世界
发布2018-08-31 16:23:01
4380
发布2018-08-31 16:23:01
举报
文章被收录于专栏:vuevue

1、求出一个字符在某一个字符串中出现的位置和次数。

代码语言:javascript
复制
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str">被查找的字符串</param>
        /// <param name="strKey">查找的字符</param>
        public static void LookUpStrKey(string str, string strKey) {
            int count = 1;
            int index = 0;
            int strkeyLength = strKey.Length;
            while (true)
            {
                index = str.IndexOf(strKey, index) ;
                if (index != -1)
                {
                    Console.WriteLine("第{0}次寻找,所在字符串中的位置是{1}", count, index);
                }
                else
                {
                    break;
                }
                index += strkeyLength;
                count++;
            }
            Console.ReadKey();
        }

调用:

            string str= "咳嗽我的世界你好!咳嗽我的四阶段就是"+                 "垃圾咳嗽哈哈哈哈哈哈哈哈咳嗽搜搜哦柯索咳嗽哈哈哈哈哈哈哈哈"+                 "咳嗽哈哈哈哈哈哈哈哈咳嗽搜搜哦柯索咳嗽搜搜哦柯索";             string strKey = "咳嗽";             LookUpStrKey(str, strKey);

输出结果:

第1次寻找,所在字符串中的位置是0 第2次寻找,所在字符串中的位置是9 第3次寻找,所在字符串中的位置是20 第4次寻找,所在字符串中的位置是30 第5次寻找,所在字符串中的位置是37 第6次寻找,所在字符串中的位置是47 第7次寻找,所在字符串中的位置是57 第8次寻找,所在字符串中的位置是64

代码语言:javascript
复制
2、将两个集合中的元素消除重复并组合成一个新的集合,并将这个集合进行排序
            List<int> list1 = new List<int>() {1, 3, 5, 6 };
            List<int> list2 = new List<int>() { 9, 6, 7, 1 };
            for (int i = 0; i < list2.Count(); i++) {
                if (!list1.Contains(list2[i])) {
                    list1.Add(list2[i]);
                }
            }
            //list1.OrderBy(l => l);
            list1.Sort();//也是排序
            foreach (int item in list1) {
                Console.WriteLine(item);
            }
            Console.ReadKey();

3、向一个集合里面添加10个1-100的随机数,要求不能出现重复且必须是偶数。

代码语言:javascript
复制
            Random r = new Random();
            List<int> list = new List<int>();
   //方法1
            for (int i = 0; i < 10; i++)
            {
                int number = r.Next(1, 101);
                if (!list.Contains(number) && number % 2 == 0)
                    list.Add(r.Next(1, 101));
                else
                    i--;
            }

            //方法2

代码语言:javascript
复制
            while (list.Count < 10) {
                int number = r.Next(1, 101);
                if (!list.Contains(number) && number % 2 == 0) { 
                    list.Add(r.Next(1, 101));
                }
            }
            foreach (int item in list)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
代码语言:javascript
复制
4、//计算出字符串"Wellcome, to china"中每个字母出现的次数
            string str = "Wellcome, to china";
            Dictionary<char, int> dic = new Dictionary<char, int>();
            //键:字符--------------值:出现的次数
            for (int i = 0; i < str.Length; i++)
            {
                //如果是空格则跳过,只计算字母出现的次数
                if (str[i] == ' ' || str[i] == ',')
                {
                    continue;
                }
                //第一次出现则添加到字典
                if (!dic.ContainsKey(str[i]))
                    dic.Add(str[i], 1);
                else
                    dic[str[i]]++;
            }
            foreach (KeyValuePair<char, int> kv in dic)
            {
                Console.WriteLine("字母{0}出现了{1}", kv.Key, kv.Value);
            }
            Console.ReadKey();
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-04-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档