技術頻道導航
HTML/CSS
.NET技術
IIS技術
PHP技術
Js/JQuery
Photoshop
Fireworks
服務器技術
操作系統(tǒng)
網(wǎng)站運營

贊助商

分類目錄

贊助商

最新文章

搜索

C#實現(xiàn)24點快速計算【源碼/實例下載】

作者:admin    時間:2023-4-13 9:54:41    瀏覽:

在抖音常??吹接腥送?4點快速計算,這是一個腦力鍛煉智力開發(fā)的游戲,就是從撲克牌隨機拿出4張,然后快速計算出結果等于24,注意是每張牌只能算一次。

本文提供C#實現(xiàn)24點快速計算的源碼。

C#實現(xiàn)24點快速計算 

實例介紹

在輸入框里輸入1到10的4個數(shù),然后點擊“計算24點”按鈕,瞬間結果就出現(xiàn)在下方的“結果”方框里。

當無解時,“結果”方框里提示“沒有找到合適的方法”。

C#源碼

#region   24點算法
/*
 * Count24(3,3,7,7);
 * 窮舉法
 *
 */

private string[] countMethod = new string[] { "+", "-", "*", "/" };
private int[] countNum;
private int[] countNumBak;

public string Count24(int a, int b, int c, int d)
{
    countNumBak = new int[4] { a, b, c, d };
    countNum = new int[4];
    string result = "沒有找到合適的方法";
    bool isTrue;

    //把   abcd   四個數(shù)字隨機付給數(shù)組   countNum
    for (int i = 0; i < 4; i++)
    {
        countNum[0] = countNumBak[i];
        for (int j = 0; j < 4; j++)
        {
            if (j == i)
                continue;
            countNum[1] = countNumBak[j];
            for (int k = 0; k < 4; k++)
            {
                if (k == j || k == i)
                    continue;
                countNum[2] = countNumBak[k];
                countNum[3] = countNumBak[1 + 2 + 3 - i - j - k];

                result = countMain24(countNum, out isTrue);
                if (!isTrue)
                {
                    result = countMain(countNum, out isTrue);
                }

                if (isTrue)
                    return result;
                else
                    result = "沒有找到合適的方法";
            }
        }
    }

    return result;
}

/// <summary>
/// 組合計算,(第一個數(shù)字(方法)第二個數(shù)字)  (方法)  (第三個數(shù)字)(方法)(第四個數(shù)字)
/// </summary>
/// <param name="countNum"></param>
/// <param name="isTrue"></param>
/// <returns></returns>
private string countMain(int[] countNum, out bool isTrue)
{
    float a, b, c;
    string result = string.Empty;
    isTrue = false;

    foreach (string method in countMethod)
    {
        a = eval(method, (float)countNum[0], (float)countNum[1]);
        foreach (string m in countMethod)
        {
            b = eval(method, (float)countNum[2], (float)countNum[3]);

            foreach (string n in countMethod)
            {
                c = eval(n, a, b);

                if (Math.Round(c, 4) == 24)
                {
                    result = "(" + countNum[0].ToString() + method + countNum[1].ToString() + ")";
                    result += n + "(" + countNum[2].ToString() + m + countNum[3].ToString() + ")";
                    isTrue = true;
                    goto TODO;
                }
            }

        }
    }
TODO:
    return result;
}

/// <summary>
/// 順序計算,第一個數(shù)字(方法)第二個數(shù)字(方法)第三個數(shù)字(方法)第四個數(shù)字
/// </summary>
/// <param name="countNum"></param>
/// <param name="isTrue"></param>
/// <returns></returns>
private string countMain24(int[] countNum, out bool isTrue)
{
    string result = "";
    float countValue = 0;
    float countValueBak = 0;
    isTrue = false;
    float upValueA, upValueBakA;
    float upValueB, upValueBakB;

    //   a   (方法)   b   (方法)   c   (方法)   d
    for (int i = 0; i < 4; i++)
    {   //不必計算   b/a   的情況,數(shù)組重排列中會計算到此種情況
        if (countMethod[i] == "/" && countNum[1] == 0)
            countValue = (float)countNum[0];
        else
            countValue = eval(countMethod[i], (float)countNum[0], (float)countNum[1]);

        upValueA = countValue;
        upValueBakA = countValue;

        for (int j = 0; j < 4; j++)
        {
            //第一種情況   (a和b的結果)   (方法)   c    
            if (countMethod[j] == "/" && countNum[2] == 0)
            { }
            else
            {
                countValue = eval(countMethod[j], upValueA, (float)countNum[2]);
            }

            //第二種情況   c   (方法)   (a和b的結果)
            if (countMethod[j] == "/" && upValueBakA == 0)
            {
                countValueBak = upValueBakA;
            }
            else
            {
                countValueBak = eval(countMethod[j], (float)countNum[2], upValueBakA);
            }

            upValueB = countValue;
            upValueBakB = countValueBak;

            for (int k = 0; k < 4; k++)
            {
                //第一種情況   d   (方法)   (a,b,c的結果1)
                if (countMethod[k] == "/" && upValueB == 0)
                { }
                else
                {
                    countValue = eval(countMethod[k], (float)countNum[3], upValueB);
                    if (Math.Round(countValue, 4) == 24)
                    {//如果已經(jīng)得到24點,則結束本程序
                        result = countNum[3].ToString() + countMethod[k] + "((" + countNum[0].ToString() + countMethod[i] + countNum[1].ToString() + ")";
                        result += countMethod[j] + countNum[2].ToString() + ")";
                        result += "   =   24";
                        isTrue = true;
                        return result;
                    }
                }

                //第二種情況   (a,b,c的結果1)   (方法)   d
                if (countMethod[k] == "/" && countNum[3] == 0)
                { }
                else
                {
                    countValue = eval(countMethod[k], upValueB, (float)countNum[3]);
                    if (Math.Round(countValue, 4) == 24)
                    {//如果已經(jīng)得到24點,則結束本程序
                        result = "((" + countNum[0].ToString() + countMethod[i] + countNum[1].ToString() + ")";
                        result += countMethod[j] + countNum[2].ToString() + ")";
                        result += countMethod[k] + countNum[3].ToString() + "   =   24";
                        isTrue = true;
                        return result;
                    }
                }

                //第三種情況   d   (方法)   (a,b,c的結果2)
                if (countMethod[k] == "/" && upValueBakB == 0)
                { }
                else
                {
                    countValueBak = eval(countMethod[k], (float)countNum[3], upValueBakB);
                    if (Math.Round(countValueBak, 4) == 24)
                    {//如果已經(jīng)得到24點,則結束本程序
                        result = countNum[3].ToString() + countMethod[k] + "(" + countNum[2].ToString() + countMethod[j] + "(" + countNum[0].ToString() + countMethod[i] + countNum[1].ToString() + "))";
                        result += "   =   24";
                        isTrue = true;
                        return result;
                    }
                }

                //第四種情況   (a,b,c的結果2)   (方法)   d
                if (countMethod[k] == "/" && countNum[3] == 0)
                { }
                else
                {
                    countValueBak = eval(countMethod[k], upValueBakB, (float)countNum[3]);
                    if (Math.Round(countValueBak, 4) == 24)
                    {//如果已經(jīng)得到24點,則結束本程序
                        result = "(" + countNum[2].ToString() + countMethod[j] + "(" + countNum[0].ToString() + countMethod[i] + countNum[1].ToString() + "))";
                        result += countMethod[k] + countNum[3].ToString();
                        result += "   =   24";
                        isTrue = true;
                        return result;
                    }
                }
            }
        }
    }

    return "";
}

private float eval(string method, float a, float b)
{
    switch (method)
    {
        case "+":
            return a + b;
        case "-":
            return a - b;
        case "*":
            return a * b;
        case "/":
            if (b == 0)
            {
                return a;
            }
            else
            {
                return a / b;
            }
        default:
            return 0;
    }
}
#endregion

實例下載

本實例使用.NET4.0創(chuàng)建,運行本實例你需要在本機安裝.NET4.0框架。

24點計算-C#.rar

標簽: CSharp  24點  
x
  • 站長推薦
/* 左側顯示文章內(nèi)容目錄 */