天津网站建设揭秘,哪个网站做网络推好,windows优化大师会员,中国设计素材网文章目录 一、题目二、C# 题解 一、题目 珠玑妙算游戏#xff08;the game of master mind#xff09;的玩法如下。 计算机有4个槽#xff0c;每个槽放一个球#xff0c;颜色可能是红色#xff08;R#xff09;、黄色#xff08;Y#xff09;、绿色#xff08;G#… 文章目录 一、题目二、C# 题解 一、题目 珠玑妙算游戏the game of master mind的玩法如下。 计算机有4个槽每个槽放一个球颜色可能是红色R、黄色Y、绿色G或蓝色B。例如计算机可能有RGGB 4种槽1为红色槽2、3为绿色槽4为蓝色。作为用户你试图猜出颜色组合。打个比方你可能会猜YRGB。要是猜对某个槽的颜色则算一次“猜中”要是只猜对颜色但槽位猜错了则算一次“伪猜中”。注意“猜中”不能算入“伪猜中”。 给定一种颜色组合 solution 和一个猜测 guess编写一个方法返回猜中和伪猜中的次数 answer其中 answer[0] 为猜中的次数answer[1] 为伪猜中的次数。
示例 输入 solution“RGBY”,guess“GGRR” 输出 [1,1] 解释 猜中1次伪猜中1次。 提示
len(solution) len(guess) 4solution 和 guess 仅包含 R, G, B, Y 这4种字符 点击此处跳转题目。
二、C# 题解 数据量很小直接统计就好了。
public class Solution {public int[] MasterMind(string solution, string guess) {int[] ans new[] { 0, 0 };Dictionarychar, int dic new Dictionarychar, int() {{ R, 0 },{ G, 0 },{ B, 0 },{ Y, 0 }};foreach (char c in solution) { dic[c]; }for (var i 0; i solution.Length; i) {if (solution[i] guess[i]) ans[0];if (dic[guess[i]] 0) {dic[guess[i]]--;ans[1];}}ans[1] - ans[0];return ans;}
}时间116 ms击败 83.33% 使用 C# 的用户内存40.66 MB击败 16.67% 使用 C# 的用户