Skip to main content

C# 24点游戏求解算法

C# 24点游戏求解算法 - Hi friends, I hope you are all in good healthzonetechnoupdate, In the article you are reading this time with the title C# 24点游戏求解算法 , We have prepared this article well for you to read and take information in it. hopefully the contents of the post what we write you can understand. ok, happy reading.

Title : C# 24点游戏求解算法
link : C# 24点游戏求解算法

read also


C# 24点游戏求解算法

经常跟儿子玩24点,有时候比较难算的,算一会儿,两人算不出来,就收了,当作没法算。

以我的数学能力,一般来说,算不出来的,大概率确实是算不出来的。

但是遇到比较变态的,当作算不出来是可能的,所以一直想找一个直接能解24点的程序。

可是网上找了一圈,一直没找到。

想着自己写一个,可是这里面的逻辑一直理不清楚,不知道这个算法应该怎么写。

下午坐在电脑前面想的时候,不知道为啥,像是突然开窍了。写着写着居然写出来了。

贴出来供有兴趣的朋友参考,有问题请指正。

using System;using System.Collections.Generic;using System.Diagnostics;namespace Calc24Point{ internal class Program {  private static void Main(string[] args)  {   var items = CalcHelper.CalcResult(3, 3, 8, 8);   foreach (var item in items)   {    Console.WriteLine(item);   }   Console.ReadKey();  } } 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)));   return items;  }  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 {  加,  减,  乘,  除, }}

 









原文转载:http://www.shaoqun.com/a/812639.html

跨境电商:https://www.ikjzd.com/

我接的黑人客人好痛苦 黑人锁住高潮也不拔出来:http://lady.shaoqun.com/a/247110.html

刘小东:https://www.ikjzd.com/w/1853

晚上和闺蜜换男友 喜欢让人㖭我下面自述:http://www.30bags.com/m/a/249737.html

naver:https://www.ikjzd.com/w/1727


经常跟儿子玩24点,有时候比较难算的,算一会儿,两人算不出来,就收了,当作没法算。以我的数学能力,一般来说,算不出来的,大概率确实是算不出来的。但是遇到比较变态的,当作算不出来是可能的,所以一直想找一个直接能解24点的程序。可是网上找了一圈,一直没找到。想着自己写一个,可是这里面的逻辑一直理不清楚,不知道这个算法应该怎么写。下午坐在电脑前面想的时候,不知道为啥,像是突然开窍了。写着写着居然写出来了
adore:https://www.ikjzd.com/w/2202
圣诞节送男友两种礼物可以促进感情升温:http://lady.shaoqun.com/m/a/329661.html
强行扒开女班长大腿 班长让我脱了她的内衣:http://lady.shaoqun.com/m/a/247580.html
北京到哈尔滨高铁什么时候开通?:http://www.30bags.com/a/400932.html
建德卧龙峡漂流门票多少钱?团购票价优惠吗:http://www.30bags.com/a/370308.html
建德卧龙峡漂流营业时间?开放时间:http://www.30bags.com/a/370307.html
建德下渚湖 秋意浓满江南湖畔:http://www.30bags.com/a/415510.html
【三清山门票价格】三清山门票多少钱,三清山门票优惠政策:http://www.30bags.com/a/426460.html
宝宝我们换个姿势楼梯间 每走走一步重重地撞一下:http://lady.shaoqun.com/m/a/248161.html
暑假内蒙古旅游哪里好玩 2021暑期内蒙古玩转大草原攻略 :http://www.30bags.com/a/440880.html
暑假辽宁自驾游去哪里好 2021暑假辽宁自驾游好去处推荐 :http://www.30bags.com/a/440881.html
欧盟增值税发生重大变化!将影响ebay卖家:https://www.ikjzd.com/articles/145849


That's the article C# 24点游戏求解算法

That's it for the article C# 24点游戏求解算法 this time, hopefully can be useful for all of you. okay, see you in another article post.

You are now reading the article C# 24点游戏求解算法 with link address https://zonetechno-update.blogspot.com/2021/06/c-24.html
Comment Policy: Please write your comments that match the topic of this page post. Comments containing links will not be displayed until they are approved.
Open Comments
Close Comment