2015年4月16日 星期四

[2015][Quiz]MidExam - 40073049H

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JugPuzzle
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSolve_Click(object sender, EventArgs e)
        {
            JugPuzzle jug = new JugPuzzle();
            jug.JugA = int.Parse(txtJugA.Text);
            jug.JugB = int.Parse(txtJugB.Text);
            jug.Target = int.Parse(txtTarget.Text);
            jug.Solve();
            txtDisplay.AppendText(jug.Display);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtDisplay.Clear();
        }
    }
}

jug.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace JugPuzzle
{
    class JugPuzzle
    {
        public int JugA;
        public int JugB;
        public int Target;
        public string Display = "";
        int gcd(int A, int B)
        {
            if (A % B == 0)
            {
                return B;
            }
            return gcd(B, A % B);
        }
        bool IsSovlable()
        {
            if ((Target % gcd(JugA, JugB) == 0 && JugA + JugB > Target) || JugA + JugB == Target)
            {
                return true;
            }
            return false;
        }
        int CapacityA = 0;
        int CapacityB = 0;
        public void Solve()
        {
            int step=0;

            if (IsSovlable())
            {
                Display += "開始進行分水步驟!";
                Display += Environment.NewLine;
                if (JugA == Target)
                {
                    Display += "直接裝滿容器A即可!";
                }
                else if (JugB == Target)
                {
                    Display += "直接裝滿容器B即可!";
                }
                else if (JugA + JugB == Target)
                {
                    Display += "直接裝滿容器A及容器B即可!";
                }
                else
                {
                    do
                    {
                        step++;
                        if (CapacityA == 0)
                        {
                            PrintDivider();
                            Display += "將容器A裝滿";
                            Display += Environment.NewLine;
                            CapacityA = JugA;
                            PrintCurrentWater();
                        }
                        else if (CapacityB == JugB)
                        {
                            PrintDivider();
                            Display += "將容器B的水倒光";
                            Display += Environment.NewLine;
                            CapacityB = 0;
                            PrintCurrentWater();
                            PrintDivider();
                            Display += "將容器A的水倒進容器B";
                            Display += Environment.NewLine;
                            CapacityB = CapacityA;
                            CapacityA = 0;
                            PrintCurrentWater();
                        }
                        else if (CapacityA + CapacityB >= JugB && CapacityA == JugA)
                        {
                            PrintDivider();
                            Display += "將容器A的水倒進容器B,直到B滿為止";
                            Display += Environment.NewLine;
                            CapacityA -= (JugB - CapacityB);
                            CapacityB = JugB;
                            PrintCurrentWater();
                        }
                        else if (CapacityA + CapacityB < JugB && CapacityA == JugA)
                        {
                            PrintDivider();
                            Display += "將容器A的水倒進容器B";
                            Display += Environment.NewLine;
                            CapacityB += CapacityA;
                            CapacityA = 0;
                            PrintCurrentWater();
                        }
                        else
                        {
                            PrintDivider();
                            Display += "ERROR";
                            Display += Environment.NewLine;
                        }
                    }
                    while (!(CapacityA == Target || CapacityB == Target || CapacityA + CapacityB == Target));
                    Display += "*******************************************";
                    Display += Environment.NewLine;
                    Display += "*******************************************";
                    Display += Environment.NewLine;
                    if (Target == CapacityA)
                    {
                        Display += "將容器A放在磅秤上,即為所求";
                        Display += Environment.NewLine;
                        Safe();
                    }
                    else if (Target == CapacityB)
                    {
                        Display += "將容器B放在磅秤上,即為所求";
                        Display += Environment.NewLine;
                        Safe();
                    }
                    else
                    {
                        Display += "將容器A與B一起放在磅秤上,即為所求";
                        Display += Environment.NewLine;
                        Safe();
                    }
                }
                return;
            }
            else
            {
                Display += "無解,你被唬了,boom!";
                return;
            }
        }
        void PrintDivider()
        {
            Display += "=======================================";
            Display += Environment.NewLine;
            return;
        }
        void Safe()
        {
            Display += "炸彈解除!";
            Display += Environment.NewLine;
            return;
        }
        void PrintCurrentWater()
        {
            
            Display += "(A,B)=(" + CapacityA.ToString() + "," + CapacityB.ToString() + ")";
            Display += Environment.NewLine;
            return;
        }
    }
}

1 則留言:

  1. JugPuzzle類別裡面的欄位的字首記得改成小寫會比較好,然後30,31這兩行宣告的變數最好都統一寫在最上方
    gcd代表的是方法,名稱最好用大寫的GCD代替.
    部分JugPuzzle內的方法最好加上存取權限修飾詞(public,protected,private...等)會比較好,雖然C#在編譯時會自動將類別內沒有存取權限修飾詞的方法/欄位都視為private.

    回覆刪除