当前位置: 首页 > news >正文

C#集合练习题

1、考察List集合使用(使用了lamdba表达式) 难度:⭐

需求
创建一个学生成绩管理系统,使用List<double>存储成绩。

要求

  1. 添加5个学生的成绩(85, 92, 78, 90, 88)

  2. 计算并输出:平均分、最高分、最低分

  3. 统计及格人数(≥60分)

  4. 移除所有低于60分的成绩

  5. 按降序排序并输出

参考答案:

using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { List<double> scores = new List<double> { 85, 92, 78, 90, 88 }; // 计算统计信息 double average = scores.Average(); double max = scores.Max(); double min = scores.Min(); int passCount = scores.Count(s => s >= 60); Console.WriteLine($"平均分: {average:F2}"); Console.WriteLine($"最高分: {max}"); Console.WriteLine($"最低分: {min}"); Console.WriteLine($"及格人数: {passCount}"); // 移除低于60的 scores.RemoveAll(s => s < 60); // 降序排序 scores.Sort((a, b) => b.CompareTo(a)); Console.WriteLine("\n降序排序后: " + string.Join(", ", scores)); } }

2、考察HashSet集合 难度:⭐

需求
使用HashSet<T>实现好友推荐系统。

要求

定义两个集合如下:

HashSet<string> userFriends = new HashSet<string> { "Alice", "Bob", "Charlie", "David" }; HashSet<string> otherFriends = new HashSet<string> { "Bob", "David", "Eve", "Frank" };
  1. 找出共同好友

  2. 找出推荐好友(对方有但用户没有的)

  3. 合并所有好友(并集)

  4. 找出用户独有的好友

参考答案:

using System; using System.Collections.Generic; class Program { static void Main() { HashSet<string> userFriends = new HashSet<string> { "Alice", "Bob", "Charlie", "David" }; HashSet<string> otherFriends = new HashSet<string> { "Bob", "David", "Eve", "Frank" }; // 共同好友 var common = new HashSet<string>(userFriends); common.IntersectWith(otherFriends); Console.WriteLine($"共同好友: {string.Join(", ", common)}"); // 推荐好友(对方有但用户没有) var recommended = new HashSet<string>(otherFriends); recommended.ExceptWith(userFriends); Console.WriteLine($"推荐好友: {string.Join(", ", recommended)}"); // 并集 var allFriends = new HashSet<string>(userFriends); allFriends.UnionWith(otherFriends); Console.WriteLine($"所有好友: {string.Join(", ", allFriends)}"); // 用户独有的好友 var unique = new HashSet<string>(userFriends); unique.ExceptWith(otherFriends); Console.WriteLine($"独有好: {string.Join(", ", unique)}"); } }

3、考察Queue集合 难度:⭐

需求
使用Queue<string>模拟打印机任务队列。

要求

  1. 添加5个打印任务

  2. 显示当前队列中的任务数量

  3. 依次处理(出队)每个任务

  4. 处理完一个任务后显示剩余任务数

  5. 查看下一个要打印的任务(不删除)

参考答案:

using System; using System.Collections.Generic; class Program { static void Main() { Queue<string> printerQueue = new Queue<string>(); // 添加任务 printerQueue.Enqueue("文档1.pdf"); printerQueue.Enqueue("图片.jpg"); printerQueue.Enqueue("报告.docx"); printerQueue.Enqueue("表格.xlsx"); printerQueue.Enqueue("演示.pptx"); Console.WriteLine($"队列中有 {printerQueue.Count} 个任务"); Console.WriteLine($"下一个任务: {printerQueue.Peek()}\n"); // 处理任务 while (printerQueue.Count > 0) { string task = printerQueue.Dequeue(); Console.WriteLine($"正在打印: {task}"); Console.WriteLine($"剩余任务: {printerQueue.Count}"); } Console.WriteLine("\n所有任务处理完毕!"); } }

4、考察LinkedList集合 难度:⭐

需求
使用LinkedList<string>实现一个简单的LRU(最近最少使用)缓存,容量为3。

要求

  1. 添加元素:如果存在则移到链表头部,不存在且未满则添加到头部

  2. 如果已满,删除尾部元素再添加新元素

  3. 演示以下操作序列:

    • 添加 "A", "B", "C"

    • 访问 "B"(移到头部)

    • 添加 "D"(应移除 "A")

    • 输出最终链表顺序

参考答案:

using System; using System.Collections.Generic; class LRUCache { private LinkedList<string> _list = new LinkedList<string>(); private Dictionary<string, LinkedListNode<string>> _dict = new Dictionary<string, LinkedListNode<string>>(); private int _capacity; public LRUCache(int capacity) { _capacity = capacity; } public void Add(string item) { if (_dict.ContainsKey(item)) { // 存在则移到头部 _list.Remove(_dict[item]); _list.AddFirst(_dict[item]); } else { // 不存在 if (_dict.Count >= _capacity) { // 移除尾部 string last = _list.Last.Value; _dict.Remove(last); _list.RemoveLast(); } // 添加到头部 _list.AddFirst(item); _dict[item] = _list.First; } } public void Access(string item) { Add(item); // 访问等同于添加(移到头部) } public void Display() { Console.WriteLine($"缓存顺序(头部→尾部): {string.Join(" → ", _list)}"); } } class Program { static void Main() { LRUCache cache = new LRUCache(3); cache.Add("A"); cache.Add("B"); cache.Add("C"); cache.Display(); // A → B → C cache.Access("B"); cache.Display(); // B → A → C cache.Add("D"); cache.Display(); // D → B → A (C被移除) } }

5、考察Dictionary集合 难度:⭐

需求
使用Dictionary<string, Student>管理学生信息,Key为学生ID。

Student类

class Student { public string Name { get; set; } public int Age { get; set; } public string Major { get; set; } }

要求

  1. 添加3个学生

  2. 根据学号查询学生信息

  3. 修改学生专业

  4. 删除一个学生

  5. 遍历所有学生并输出

using System; using System.Collections.Generic; class Student { public string Name { get; set; } public int Age { get; set; } public string Major { get; set; } public override string ToString() { return $"姓名:{Name}, 年龄:{Age}, 专业:{Major}"; } } class Program { static void Main() { Dictionary<string, Student> students = new Dictionary<string, Student>(); // 添加学生 students.Add("S001", new Student { Name = "张三", Age = 20, Major = "计算机" }); students.Add("S002", new Student { Name = "李四", Age = 21, Major = "数学" }); students.Add("S003", new Student { Name = "王五", Age = 19, Major = "物理" }); // 查询 if (students.ContainsKey("S002")) { Console.WriteLine($"S002: {students["S002"]}"); } // 修改 students["S002"].Major = "应用数学"; Console.WriteLine($"修改后: {students["S002"]}"); // 删除 students.Remove("S003"); // 遍历 Console.WriteLine("\n所有学生:"); foreach (var kvp in students) { Console.WriteLine($"{kvp.Key}: {kvp.Value}"); } } }

6、考察Dictionary集合 难度:⭐⭐

需求
统计一段文字中每个单词出现的次数。

输入文本

"hello world hello csharp world hello programming"

要求

  1. 使用Dictionary<string, int>统计单词频率

  2. 忽略大小写(Hello和hello算同一个)

  3. 按出现次数降序输出

  4. 找出出现次数最多的单词

参考答案:

using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { string text = "hello world hello csharp world hello programming"; // 分割单词并转为小写 string[] words = text.ToLower().Split(' '); Dictionary<string, int> wordCount = new Dictionary<string, int>(); foreach (string word in words) { if (wordCount.ContainsKey(word)) wordCount[word]++; else wordCount[word] = 1; } // 按次数降序输出 var sorted = wordCount.OrderByDescending(x => x.Value); Console.WriteLine("单词频率统计:"); foreach (var item in sorted) { Console.WriteLine($" {item.Key}: {item.Value}次"); } // 出现最多的单词 var mostFrequent = sorted.First(); Console.WriteLine($"\n出现最多的单词: '{mostFrequent.Key}', 出现{mostFrequent.Value}次"); } }

思考:

各种集合有什么区别,适合什么使用场景

http://www.jsqmd.com/news/580423/

相关文章:

  • OpenDataLab MinerU智能文档理解:5分钟零基础部署,小白也能搭建OCR系统
  • Figma中文界面高效解决方案:5个维度打造无障碍设计工作流
  • SUNFLOWER MATCH LAB 效果对比:不同卷积神经网络架构下的识别精度
  • 如何让离线视频不再崩坏?智能合并工具的实战方案
  • seo广东话与内容营销的关系是什么
  • RTX 4090+Qwen2.5-VL-7B-Instruct:开源多模态视觉助手替代商业SaaS的ROI测算
  • KLayout:开源EDA工具如何解决半导体设计中的成本与效率难题
  • E-Hentai漫画批量下载器:3步轻松保存完整漫画集
  • 3步实现学术文献智能管理:Zotero Reference插件深度解析
  • Bili2text:让B站视频内容价值重获新生的智能转化工具
  • Topit:提升多任务处理效率的macOS窗口管理开源解决方案
  • BetterNCM安装器:网易云音乐插件生态的高效管理工具
  • DFRDisplayKm:让MacBook Touch Bar在Windows焕发新生的开源驱动方案
  • 【神器推荐】Elsevier投稿监控:告别手动刷新,实现智能追踪!
  • 线程池中execute和submit的区别?
  • Windows Defender深度移除技术解析:从问题诊断到系统优化的实战指南
  • ComfyUI-Manager实战手册:插件全生命周期管理与多系统适配全流程方案
  • OBS多平台推流插件终极指南:5分钟搞定多平台同步直播
  • 核心架构记录
  • Qwen3.5-9B惊艳效果:128K上下文下跨文档逻辑链推理演示
  • CLIP-GmP-ViT-L-14从零开始:Ubuntu/CUDA环境部署图文匹配系统
  • Qwen3-0.6B-FP8开源可部署:完全离线运行的轻量级大模型本地化方案
  • 东莞seo优化如何选择关键词
  • DOM Document
  • FDS-210 土壤电导率盐分传感器 二合一同时监测 密封好 耐腐蚀
  • 说说事务的传播级别?
  • JX3Toy:提升剑网3游戏效率的自动化工具
  • 算法题:数组中的第k个最大元素
  • 3个核心突破:Full Page Screen Capture的智能滚动截图解决方案
  • 千问3.5-9B操作系统概念解析与Linux内核学习指南