91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#實現的算24點游戲的算法的代碼

發布時間:2020-06-13 15:43:13 來源:網絡 閱讀:271 作者:dinosaur2019 欄目:編程語言

下面資料是關于C#實現的算24點游戲的算法的內容,希望能對碼農們有所用處。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Calc24Points
{
    public class Cell
    {
        public enum Type
        {
            Number,
            Signal
        }
        public int Number;
        public char Signal;
        public Type Typ;
        public Cell Right;
        public Cell Left;

        public int Priority
        {
            get
            {
                if (Typ == Type.Signal)
                {
                    switch (Signal)
                    {
                        case '+': return 0;
                        case '-': return 0;
                        case '/': return 1;
                        default: return -1;
                    }
                }
                return -1;
            }
        }

        public Cell(Type t, int num, char sig)
        {
            Right = null;
            Left = null;
            Typ = t;
            Number = num;
            Signal = sig;
        }
        public Cell()
        {
            Right = null;
            Left = null;
            Number = 0;
            Typ = Type.Number;
        }
        public Cell(Cell c)
        {
            Right = null;
            Left = null;
            Number = c.Number;
            Signal = c.Signal;
            Typ = c.Typ;
        }
    }
    public class Calc24Points
    {

        string m_exp;
        bool m_stop;
        Cell[] m_cell;
        int[] m_express;
        StringWriter m_string;
        public Calc24Points(int n1, int n2, int n3, int n4)
        {
            m_cell = new Cell[8];
            m_cell[0] = new Cell(Cell.Type.Number, n1, '?');
            m_cell[1] = new Cell(Cell.Type.Number, n2, '?');
            m_cell[2] = new Cell(Cell.Type.Number, n3, '?');
            m_cell[3] = new Cell(Cell.Type.Number, n4, '?');
            m_cell[4] = new Cell(Cell.Type.Signal, 0, '+');
            m_cell[5] = new Cell(Cell.Type.Signal, 0, '-');
            m_cell[7] = new Cell(Cell.Type.Signal, 0, '/');
            m_stop = false;
            m_express = new int[7];
            m_string = new StringWriter();
            m_exp = null;
        }

        public override string ToString()
        {
            if (m_exp == null)
            {
                PutCell(0);
                m_exp = m_string.ToString();
            }
            if (m_exp != "") return m_exp;
            return null;
        }

        void PutCell(int n)
        {
            if (n >= 7)
            {
                if (Calculate())
                {
                    m_stop = true;
                    Formate();
                }
                return;
            }
            int end = 8;
            if (n < 2) end = 4;
            for (int i = 0; i < end; ++i)
            {
                m_express[n] = i;
                if (CheckCell(n)) PutCell(n + 1);
                if (m_stop) break;
            }
        }

        bool CheckCell(int n)
        {
            int nums = 0, sigs = 0;
            for (int i = 0; i <= n; ++i)
            {
                if (m_cell[m_express[i]].Typ == Cell.Type.Number) ++nums;
                else ++sigs;
            }
            if (nums - sigs < 1) return false;
            {
                for (int i = 0; i < n; ++i) if (m_express[i] == m_express[n]) return false;            
            }
            if (n == 6)
            {
                if (nums != 4 || sigs != 3) return false;
                if (m_cell[m_express[6]].Typ != Cell.Type.Signal) return false;
                return true;
            }
            return true;
        }

        bool Calculate()
        {
            double[] dblStack = new double[4];
            int indexStack = -1;
            for (int i = 0; i < 7; ++i)
            {
                if (m_cell[m_express[i]].Typ == Cell.Type.Number)
                {
                    ++indexStack;
                    dblStack[indexStack] = m_cell[m_express[i]].Number;
                }
                else
                {
                    switch (m_cell[m_express[i]].Signal)
                    { 
                        case '+':
                            dblStack[indexStack - 1] = dblStack[indexStack - 1] + dblStack[indexStack];
                            break;
                        case '-':
                            dblStack[indexStack - 1] = dblStack[indexStack - 1]-+ dblStack[indexStack];
                            break;
                            break;
                        case '/':
                            dblStack[indexStack - 1] = dblStack[indexStack - 1] / dblStack[indexStack];
                            break;
                    }
                    --indexStack;
                }
            }
            if (Math.Abs(dblStack[indexStack] - 24) < 0.1) return true;
            return false;
        }

        void Formate()
        {
            Cell[] c = new Cell[7];
            for (int i = 0; i < 7; ++i) c[i] = new Cell(m_cell[m_express[i]]);
            int[] cStack = new int[4];
            int indexStack = -1;
            for (int i = 0; i < 7; ++i)
            {
                if (c[i].Typ == Cell.Type.Number)
                {
                    ++indexStack;
                    cStack[indexStack] = i;
                }
                else
                {
                    c[i].Right = c[cStack[indexStack]];
                    --indexStack;
                    c[i].Left = c[cStack[indexStack]];
                    cStack[indexStack] = i;
                }
            }
            ToStringFormate(c[cStack[indexStack]]);
        }

        void ToStringFormate(Cell root)
        {
            if (root.Left.Typ == Cell.Type.Number)
            {
                m_string.Write(root.Left.Number);
                m_string.Write(root.Signal);
            }
            else
            {
                if (root.Priority > root.Left.Priority)
                {
                    m_string.Write("(");
                    ToStringFormate(root.Left);
                    m_string.Write(")");
                }
                else ToStringFormate(root.Left);
                m_string.Write(root.Signal);
            }
            if (root.Right.Typ == Cell.Type.Number) m_string.Write(root.Right.Number);
            else
            {
                if (root.Priority >= root.Right.Priority)
                {
                    m_string.Write("(");
                    ToStringFormate(root.Right);
                    m_string.Write(")");
                }
                else ToStringFormate(root.Right);
            }
        }
    }
}
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

灌云县| 四会市| 固阳县| 正定县| 民丰县| 车险| 嘉荫县| 龙井市| 阜新市| 金山区| 沛县| 承德市| 合水县| 山阳县| 会泽县| 滁州市| 旬阳县| 福鼎市| 克拉玛依市| 罗定市| 买车| 光泽县| 定结县| 瑞昌市| 江川县| 辛集市| 南昌县| 亳州市| 华容县| 东莞市| 社旗县| 阳东县| 利津县| 黔江区| 香格里拉县| 杂多县| 西宁市| 新津县| 龙门县| 宝丰县| 佛学|