主函式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsJugPuzzle
{
static class Program
{
///
/// 應用程式的主要進入點。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsJugPuzzle
{
public partial class Form1 : Form
{
JugPuzzle Solution = new JugPuzzle();
public Form1()
{
InitializeComponent();
}
private void btnSolve_Click(object sender, EventArgs e)
{
Solution.CapacityOfA = int.Parse(txtCapacityA.Text);
Solution.CapacityOfB = int.Parse(txtCapacityB.Text);
Solution.Target = int.Parse(txtTarget.Text);
Solution.SetStep();
txtOutcome.Text = Solution.StepOfSolution;
}
private void btnClear_Click(object sender, EventArgs e)
{
txtOutcome.Clear();
}
}
}
Class JugPuzzle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsJugPuzzle
{
class JugPuzzle
{
public int CapacityOfA;
public int CapacityOfB;
public int Target;
public string StepOfSolution;
int AmountOfA;
int AmountOfB;
int WhichCase;
public int gcd(int a, int b)
{
if(a % b ==0)
{
return b;
}
else
{
return gcd(b, a % b);
}
}
public bool IsSolvable()
{
if(CapacityOfA + CapacityOfB >= Target && Target % gcd(CapacityOfA,CapacityOfB) == 0 && CapacityOfA > 0 && CapacityOfB >0 && Target > 0)
{
return true;
}
else
{
return false;
}
}
public bool IsSpecialCase()
{
if(CapacityOfA == Target)
{
WhichCase = 1;
return true;
}
if (CapacityOfB == Target)
{
WhichCase = 2;
return true;
}
if (CapacityOfA + CapacityOfB == Target)
{
WhichCase = 3;
return true;
}
else
{
return false;
}
}
public string SetStep()
{
StepOfSolution = "開始進行分水步驟!" + Environment.NewLine;
if(IsSpecialCase())
{
switch(WhichCase)
{
case 1:
StepOfSolution += "====================" + Environment.NewLine;
StepOfSolution += "將容器A裝滿" + Environment.NewLine;
StepOfSolution += "(" + CapacityOfA.ToString() + ",0)";
break;
case 2:
StepOfSolution += "====================" + Environment.NewLine;
StepOfSolution += "將容器B裝滿" + Environment.NewLine;
StepOfSolution += "(0," + CapacityOfB.ToString() + ")";
break;
case 3:
StepOfSolution += "====================" + Environment.NewLine;
StepOfSolution += "將容器A裝滿" + Environment.NewLine;
StepOfSolution += "(" + CapacityOfA.ToString() + ",0)" + Environment.NewLine;
StepOfSolution += "====================" + Environment.NewLine;
StepOfSolution += "將容器B裝滿" + Environment.NewLine;
StepOfSolution += "(" + CapacityOfA.ToString() + "," + CapacityOfB.ToString() + ")";
break;
}
StepOfSolution += Environment.NewLine + "********************" + Environment.NewLine;
StepOfSolution += "********************" + Environment.NewLine;
StepOfSolution += "將容器A與B一起放在磅秤上,即為所求" + Environment.NewLine;
StepOfSolution += "炸彈解除";
return StepOfSolution;
}
if(!IsSolvable())
{
StepOfSolution = "無解,你被唬了,Boom!";
return StepOfSolution;
}
if(CapacityOfA > CapacityOfB)
{
for (; ; )
{
if (AmountOfA + AmountOfB == Target)
{
break;
}
if (AmountOfA == 0)
{
AmountOfA = CapacityOfA;
StepOfSolution += "====================" + Environment.NewLine;
StepOfSolution += "將容器A裝滿" + Environment.NewLine;
StepOfSolution += "(" + AmountOfA.ToString() + "," + AmountOfB + ")" + Environment.NewLine;
}
else
{
if (AmountOfB == CapacityOfB)
{
AmountOfB = 0;
StepOfSolution += "====================" + Environment.NewLine;
StepOfSolution += "將容器B的水倒光" + Environment.NewLine;
StepOfSolution += "(" + AmountOfA.ToString() + "," + AmountOfB + ")" + Environment.NewLine;
}
else
{
if (AmountOfB == 0 && AmountOfA >= CapacityOfB)
{
AmountOfB += CapacityOfB;
AmountOfA -= CapacityOfB;
StepOfSolution += "====================" + Environment.NewLine;
StepOfSolution += "將容器A的水倒入容器B中,直到B滿為止" + Environment.NewLine;
StepOfSolution += "(" + AmountOfA.ToString() + "," + AmountOfB + ")" + Environment.NewLine;
continue;
}
if (AmountOfB == 0 && AmountOfA < CapacityOfB)
{
AmountOfB += AmountOfA;
AmountOfA = 0;
StepOfSolution += "====================" + Environment.NewLine;
StepOfSolution += "將容器A的水倒入容器B" + Environment.NewLine;
StepOfSolution += "(" + AmountOfA.ToString() + "," + AmountOfB + ")" + Environment.NewLine;
continue;
}
else
{
AmountOfA += AmountOfB - CapacityOfB;
AmountOfB = CapacityOfB;
StepOfSolution += "====================" + Environment.NewLine;
StepOfSolution += "將容器A的水倒入容器B中,直到B滿為止" + Environment.NewLine;
StepOfSolution += "(" + AmountOfA.ToString() + "," + AmountOfB + ")" + Environment.NewLine;
continue;
}
}
}
}
}
else
{
for (; ; )
{
if(AmountOfA + AmountOfB ==Target)
{
break;
}
if (AmountOfB == 0)
{
AmountOfB = CapacityOfB;
StepOfSolution += "====================" + Environment.NewLine;
StepOfSolution += "將容器B裝滿" + Environment.NewLine;
StepOfSolution += "(" + AmountOfA.ToString() + "," + AmountOfB + ")" + Environment.NewLine;
}
else
{
if(AmountOfA == CapacityOfA)
{
AmountOfA = 0;
StepOfSolution += "====================" + Environment.NewLine;
StepOfSolution += "將容器A的水倒光" + Environment.NewLine;
StepOfSolution += "(" + AmountOfA.ToString() + "," + AmountOfB + ")" + Environment.NewLine;
}
else
{
if (AmountOfA == 0 && AmountOfB >= CapacityOfA)
{
AmountOfA += CapacityOfA;
AmountOfB -= CapacityOfA;
StepOfSolution += "====================" + Environment.NewLine;
StepOfSolution += "將容器B的水倒入容器A中,直到A滿為止" + Environment.NewLine;
StepOfSolution += "(" + AmountOfA.ToString() + "," + AmountOfB + ")" + Environment.NewLine;
continue;
}
if (AmountOfA == 0 && AmountOfB < CapacityOfA)
{
AmountOfA += AmountOfB;
AmountOfB = 0;
StepOfSolution += "====================" + Environment.NewLine;
StepOfSolution += "將容器B的水倒入容器A" + Environment.NewLine;
StepOfSolution += "(" + AmountOfA.ToString() + "," + AmountOfB + ")" + Environment.NewLine;
continue;
}
else
{
AmountOfB += AmountOfA - CapacityOfA;
AmountOfA = CapacityOfA;
StepOfSolution += "====================" + Environment.NewLine;
StepOfSolution += "將容器B的水倒入容器A中,直到A滿為止" + Environment.NewLine;
StepOfSolution += "(" + AmountOfA.ToString() + "," + AmountOfB + ")" + Environment.NewLine;
continue;
}
}
}
}
}
StepOfSolution += "********************" + Environment.NewLine;
StepOfSolution += "********************" + Environment.NewLine;
StepOfSolution += "將容器A與B一起放在磅秤上,即為所求" + Environment.NewLine;
StepOfSolution += "炸彈解除";
AmountOfA = 0;
AmountOfB = 0;
return StepOfSolution;
}
}
}
JugPuzzle內欄位的名稱字首記得要改小寫.
回覆刪除這邊的gcd是一個方法,建議用改寫成GCD, 配合一個動詞改成如GetGCD會更好喔