-
C# 24点游戏求解算法(修订1)
经常跟儿子玩24点,有时候比较难算的,算一会儿,两人算不出来,就收了,当作没法算。
以我的数学能力,一般来说,算不出来的,大概率确实是算不出来的。
但是遇到比较变态的,当作算不出来是可能的,所以一直想找一个直接能解24点的程序。
可是网上找了一圈,一直没找到。
想着自己写一个,可是这里面的逻辑一直理不清楚,不知道这个算法应该怎么写。
下午坐在电脑前面想的时候,不知道为啥,像是突然开窍了。写着写着居然写出来了。
贴出来供有兴趣的朋友参考,有问题请指正。
修订1:增加(a,b),(c,d)的算法
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace Calc24Point { internal class Program { private static void Main(string[] args) { while (true) { var a = int.Parse(Console.ReadLine()); var b = int.Parse(Console.ReadLine()); var c = int.Parse(Console.ReadLine()); var d = int.Parse(Console.ReadLine()); Console.Clear(); Console.WriteLine($"{a}, {b}, {c}, {d}"); Console.WriteLine("------------------------"); var items = CalcHelper.CalcResult(a, b, c, d); if (items?.Any() == true) { foreach (var item in items) { Console.WriteLine(item); } } else { Console.WriteLine("无解"); } Console.WriteLine("------------------------"); } } } public class CalcHelper { public static List<string> CalcResult(double a, double b, double c, double d) { List<string> myList = new List<string>(); List<CalcInfo> items = MakeCalcs(a, b, c, d); foreach (var item in items) { var result = item.GetResult(); if (double.IsNaN(result) == false && IsEqual(result, 24)) { var text = item.GetString(); if (myList.Contains(text) == false) { myList.Add(text); } } } return myList; } private static bool IsEqual(double a, double b) { return Math.Abs(a - b) < 0.000001; } private static List<CalcInfo> MakeCalcs(double a, double b, double c, double d) { var items = new List<CalcInfo>(); items.AddRange(MakeCalcs(a, (b, c, d))); items.AddRange(MakeCalcs(b, (a, c, d))); items.AddRange(MakeCalcs(c, (b, a, d))); items.AddRange(MakeCalcs(d, (b, c, a))); items.AddRange(MakeCalcs((a, b), (c, d))); items.AddRange(MakeCalcs((a, c), (b, d))); items.AddRange(MakeCalcs((a, d), (c, b))); return items; } private static IEnumerable<CalcInfo> MakeCalcs((double a, double b) p1, (double c, double d) p2) { foreach (CalcInfo item1 in MakeCalcs(p1.a, p1.b)) { foreach (CalcInfo item2 in MakeCalcs(p2.c, p2.d)) { yield return new CalcInfo(item1, item2, EMode.加); yield return new CalcInfo(item1, item2, EMode.减); yield return new CalcInfo(item2, item1, EMode.减); yield return new CalcInfo(item1, item2, EMode.乘); yield return new CalcInfo(item1, item2, EMode.除); yield return new CalcInfo(item2, item1, EMode.除); } } } private static IEnumerable<CalcInfo> MakeCalcs(double item1, (double b, double c, double d) p) { foreach (CalcInfo item2 in MakeCalcs(p.b, (p.c, p.d))) { yield return new CalcInfo(item1, item2, EMode.加); yield return new CalcInfo(item1, item2, EMode.减); yield return new CalcInfo(item2, item1, EMode.减); yield return new CalcInfo(item1, item2, EMode.乘); yield return new CalcInfo(item1, item2, EMode.除); yield return new CalcInfo(item2, item1, EMode.除); } foreach (CalcInfo item2 in MakeCalcs(p.c, (p.b, p.d))) { yield return new CalcInfo(item1, item2, EMode.加); yield return new CalcInfo(item1, item2, EMode.减); yield return new CalcInfo(item2, item1, EMode.减); yield return new CalcInfo(item1, item2, EMode.乘); yield return new CalcInfo(item1, item2, EMode.除); yield return new CalcInfo(item2, item1, EMode.除); } foreach (CalcInfo item2 in MakeCalcs(p.d, (p.b, p.c))) { yield return new CalcInfo(item1, item2, EMode.加); yield return new CalcInfo(item1, item2, EMode.减); yield return new CalcInfo(item2, item1, EMode.减); yield return new CalcInfo(item1, item2, EMode.乘); yield return new CalcInfo(item1, item2, EMode.除); yield return new CalcInfo(item2, item1, EMode.除); } } private static IEnumerable<CalcInfo> MakeCalcs(double item1, (double c, double d) p) { foreach (CalcInfo item2 in MakeCalcs(p.c, p.d)) { yield return new CalcInfo(item1, item2, EMode.加); yield return new CalcInfo(item1, item2, EMode.减); yield return new CalcInfo(item2, item1, EMode.减); yield return new CalcInfo(item1, item2, EMode.乘); yield return new CalcInfo(item1, item2, EMode.除); yield return new CalcInfo(item2, item1, EMode.除); } } private static IEnumerable<CalcInfo> MakeCalcs(double item1, double item2) { yield return new CalcInfo(item1, item2, EMode.加); yield return new CalcInfo(item1, item2, EMode.减); yield return new CalcInfo(item2, item1, EMode.减); yield return new CalcInfo(item1, item2, EMode.乘); yield return new CalcInfo(item1, item2, EMode.除); yield return new CalcInfo(item2, item1, EMode.除); } } public class CalcInfo { public CalcInfo Items1 { get; set; } public CalcInfo Items2 { get; set; } public EMode? Mode { get; set; } public double Result { get; set; } public CalcInfo(double value) { Result = value; } public CalcInfo(double value1, double value2, EMode mode) : this(new CalcInfo(value1), new CalcInfo(value2), mode) { } public CalcInfo(double value1, CalcInfo value2, EMode mode) : this(new CalcInfo(value1), value2, mode) { } public CalcInfo(CalcInfo value1, double value2, EMode mode) : this(value1, new CalcInfo(value2), mode) { } public CalcInfo(CalcInfo value1, CalcInfo value2, EMode mode) { Items1 = value1; Items2 = value2; Mode = mode; } public double GetResult() { if (Mode == null) { return Result; } var item1 = Items1.GetResult(); var item2 = Items2.GetResult(); if (double.IsNaN(item1) || double.IsNaN(item1)) { return double.NaN; } switch (Mode.Value) { case EMode.加: return item1 + item2; case EMode.减: return item1 - item2; case EMode.乘: return item1 * item2; case EMode.除: if (item2 == 0) { return double.NaN; } return item1 / item2; default: Debug.Assert(false); break; } return double.NaN; } public string GetString() { if (Mode == null) { return Result.ToString(); } switch (Mode.Value) { case EMode.加: return $"({Items1.GetString()} + {Items2.GetString()})"; case EMode.减: return $"({Items1.GetString()} - {Items2.GetString()})"; case EMode.乘: return $"({Items1.GetString()} * {Items2.GetString()})"; case EMode.除: return $"({Items1.GetString()} / {Items2.GetString()})"; default: Debug.Assert(false); break; } return null; } } public enum EMode { 加, 减, 乘, 除, } }
源码地址:https://gitee.com/wzwyc/Calc24Point
出处:https://www.cnblogs.com/wzwyc/p/14899751.html
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比