A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 墨蹄 中级黑马   /  2014-4-5 09:55  /  1365 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 墨蹄 于 2014-4-9 12:04 编辑

1-10000 共10000个数 组成数组(顺序随机),从中随机删除5个数,怎么快速找出这5个数是什么?

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

3 个回复

倒序浏览
想了想最简单的还是用list.contains。问题想太复杂也没结果。方法步数小的通用性就差。下面是代码,为了简单我只生成了一个5位的数组,来模拟删除后的数组。
  1. List<int> iList = new List<int>();
  2.             Random r = new Random();
  3.             int i;
  4.             while (iList.Count < 5)//生成删除后的数组
  5.             {
  6.                 i = r.Next(1, 11);
  7.                 if (!iList.Contains(i))
  8.                 {
  9.                     iList.Add(i);
  10.                 }
  11.             }
  12.             //for (int j = 0; j < iList.Count; j++)
  13.             //{
  14.             //    Console.WriteLine(iList[j]);
  15.             //}
  16.             //Console.ReadKey();
  17.             List<int> listend = new List<int>();
  18.             for (int j = 0; j < 10; j++)//遍历循环,10000个就需要10000次contains
  19.             {
  20.                 if (!iList.Contains(j))
  21.                 {
  22.                     listend.Add(j);
  23.                 }
  24.             }
  25.             for (int j = 0; j < iList.Count; j++)
  26.             {
  27.                 Console.WriteLine(listend[j]);
  28.             }
  29.             Console.ReadKey();
复制代码



评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
直接使用数组还真不太好找,我也主要是用了list.IndexOf()方法,思路是占位置的方法,先初使一个数组,然后将原来被删过后的数组在新的数组里占个位置,最后找出没被占过的索引就是被删掉的数
  1.             List<int> iList = new List<int>();
  2.             Random r = new Random();
  3.             int i;
  4.             while (iList.Count < 10000)//生成删除前的数组
  5.             {
  6.                 i = r.Next(1, 10001);
  7.                 if (!iList.Contains(i))
  8.                 {
  9.                     iList.Add(i);
  10.                 }
  11.             }
  12.             int[] delarr = new int[5];// 生成要删除的数
  13.             int delid = 0;
  14.             while (true)
  15.             {
  16.                 int n = r.Next(1, 10001);
  17.                 if (delarr.Contains(n))
  18.                     continue;
  19.                 delarr[delid] = n;
  20.                 delid++;
  21.                 if (delid == 5)
  22.                     break;
  23.             }
  24.             foreach (int n in delarr) // 删除
  25.             {
  26.                 iList.Remove(n);
  27.             }
  28.             // 主要部份,寻找被删除数
  29.             List<int> allarr = new int[10000].ToList();
  30.             for (int j = 0; j < iList.Count; j++)
  31.             {
  32.                 int id = iList[j];
  33.                 allarr[id - 1] = id;
  34.             }
  35.             for (int j = 0; j < 5; j++)
  36.             {
  37.                 int id = allarr.IndexOf(0);
  38.                 Console.WriteLine(id + 1);
  39.                 allarr[id] = 99999;
  40.             }
  41.             //-----------------------
  42.             Console.Write("原被删除数:"); // 输出
  43.             foreach (int a in delarr)
  44.             {
  45.                 Console.Write(a + ",");
  46.             }
  47.             //-----------------------
复制代码

代码看着繁琐,但主要部份也就十二行...

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
看不懂。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马