2015年4月16日 星期四

[2015][Quiz]MidExam_60373006H

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 WinJogPuzzle
{
    public partial class Form1 : Form
    {
        JogPuzzle jp;
        public Form1()
        {
            InitializeComponent();
            jp = new JogPuzzle();
        }

        private void btn_Solve_Click(object sender, EventArgs e)
        {
            jp.BottleA = Convert.ToInt16(txt_BottleA.Text);
            jp.BottleB = Convert.ToInt16(txt_BottleB.Text);
            jp.Target = Convert.ToInt16(txt_TargetLevel.Text);
            if (jp.LimitCondition() == true)
                txt_Result.Text = jp.Print();
            else
                MessageBox.Show("Boom!!藍線、紅線都會爆");
            jp.Bottle[0] = 0;
            jp.Bottle[1] = 0;
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            txt_Result.Text = "";
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WinJogPuzzle
{
    class JogPuzzle
    {
        public int BottleA; //A容器最大水位
        public int BottleB; //B容器最大水位
        public int Target; //目標水位
        private int GCD; //最大因數(GreastCommonDivisor)
        public int[] Bottle = new int[2]; //實際操作容器

        private void FindGCD()
        {
            int bi = BottleA;//bigger
            int sm = BottleB;//smaller
            if (sm > bi)//for輾轉相除,確保大小的順序必須正確
            {
                int temp;//swap
                temp = bi;
                bi = sm;
                sm = temp;
            }
            int mod = bi % sm;//第一次輾轉相除
            while (mod != 0)//如果第一次即得到結果,立刻將較小的數(sm)塞到GCD    
            {
                bi = sm;//(預備第二次)第一次的較小數(sm)作為第二次的較大數(bi)
                sm = mod;//(預備第二次)第一次的餘數(mod)作為第二次的較小數(sm)
                mod = bi % sm;//第二次輾轉相除
                if (mod == 0)//得到結果,將較小的數(sm)塞到GCD 
                    GCD = sm;
                else
                    continue;//如沒得到結果,則繼續做。
            }
            GCD = sm;//第一次輾轉相除結果
        }
        public bool LimitCondition()
        {
            FindGCD();//最大公因數
            if ((Target % GCD) == 0)//符合條件則回傳TRUE
                return true;
            else
                return false;
        }
        public int[] Process()
        {   //倒水過程
            int delta; //A與B的差值
            //Bottle陣列[0]=A杯,[1]=B杯
            if ((Bottle[0] + Bottle[1]) == Target)//如果Target=容器A+B直接回傳
            {
                return Bottle;
            }
            else if (Bottle[0] == 0)//如A杯沒水,則A杯裝滿水
            {
                Bottle[0] = BottleA;
                return Bottle;
            }
            else if (Bottle[1] == BottleB)//如B杯滿水,則B杯到空
            {
                Bottle[1] = 0;
                return Bottle;
            }
            else if (Bottle[0] == BottleA)//如A杯滿水,則往B杯倒
            {
                Bottle[0] -= BottleA;
                Bottle[1] += BottleA;
                if (Bottle[0] > BottleA)//如水量超過B杯,則相減放回A杯
                {
                    delta = Bottle[0] - BottleA;
                    Bottle[0] = delta;
                    Bottle[0] = BottleA;
                }
                else if (Bottle[1] > BottleB)//如水量超過A杯,則相減放回B杯
                {
                    delta = Bottle[1] - BottleB;
                    Bottle[0] = delta;
                    Bottle[1] = BottleB;
                }
                return Bottle;
            }
            else//A杯未滿,則直接往B杯倒。
            {
                Bottle[1] += Bottle[0];
                Bottle[0] = 0;
                if (Bottle[0] > BottleA)//如水量超過B杯,則相減放回A杯
                {
                    delta = Bottle[0] - BottleA;
                    Bottle[0] = delta;
                    Bottle[0] = BottleA;
                }
                else if (Bottle[1] > BottleB)
                {
                    delta = Bottle[1] - BottleB;//如水量超過A杯,則相減放回B杯
                    Bottle[0] = delta;
                    Bottle[1] = BottleB;
                }
                return Bottle;
            }
        }
        public string Print()
        {
            string output="";
            while (Bottle[0] != Target &&
                  Bottle[1] != Target &&
                  (Bottle[0] + Bottle[1]) != Target)//判斷A,B,A+B是否到達目標水位
            {
                Process();
                output += "(A,B)=";
                output += "(" + Bottle[0] + "," + Bottle[1] + ")" + Environment.NewLine;
                output += "==================" + Environment.NewLine;
                if (Bottle[0] == Target)
                {
                    output += "將容器A放在磅秤上 ";
                }
                else if (Bottle[1] == Target)
                {
                    output += "將容器B放在磅秤上";
                }
                else if ((Bottle[0] + Bottle[1]) == Target)
                {
                    output += "將容器A和B放在磅秤上";
                    return output;
                }
            }
            return output;
        }
    }
}

沒有留言:

張貼留言