using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JugPuzzle
{
class Program
{
static void Main(string[] args)
{
start:
Console.WriteLine("請輸入水杯A");
int settingbottleA = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入水杯B");
int settingbottleB = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入要幾公升");
int settingtarget = Convert.ToInt32(Console.ReadLine());
int gcd = Program.gcd(settingbottleA, settingbottleB);
if ((settingtarget > settingbottleB) && (settingtarget > settingbottleA))
{
Console.WriteLine("要設定的公升比大水杯還大,永遠達不到,結束程式");
}
else
{
if (settingtarget % gcd == 0)
{
Program.jugPuzzle(settingbottleA, settingbottleB, settingtarget);
}
else
{
Console.WriteLine("此題無解");
}
}
Console.Read();
}
static int gcd(int m, int n) //最大公因數
{
int temp = 0;
while (n != 0)
{
temp = m % n;
m = n;
n = temp;
}
return m;
}
static void jugPuzzle(int settingbottleA, int settingbottleB, int settingbottletarget)
{
int bottleA = 0;
int bottleB = 0;
int delta;
do
{
if (bottleA == 0)
{
bottleA += settingbottleA;
Console.WriteLine("A杯子裝滿{0}公升", settingbottleA);
Console.WriteLine("目前狀態:(A杯子為{0}公升,B杯子為{1}公升)", bottleA, bottleB);
}
else if (bottleB == settingbottleB)
{
bottleB = 0;
Console.WriteLine("B杯子把水倒掉");
Console.WriteLine("目前狀態:(A杯子為{0}公升,B杯子為{1}公升)", bottleA, bottleB);
}
else if (bottleA == settingbottleA)
{
bottleA -= settingbottleA;
bottleB += settingbottleA;
if (bottleB > settingbottleB)
{
delta = bottleB - settingbottleB;
bottleB = settingbottleB;
bottleA = delta;
}
Console.WriteLine("A杯子往B杯倒水");
Console.WriteLine("目前狀態:(A杯子為{0}公升,B杯子為{1}公升)", bottleA, bottleB);
}
else
{
bottleB += bottleA;
if (bottleB > settingbottleB)
{
delta = bottleB - settingbottleB;
bottleB = settingbottleB;
bottleA = delta;
}
else
{
bottleA = 0;
}
Console.WriteLine("A杯子往B杯倒水");
Console.WriteLine("目前狀態:(A杯子為{0}公升,B杯子為{1}公升)", bottleA, bottleB);
}
} while ((bottleB != settingbottletarget)&&(bottleA != settingbottletarget));
}
}
}
2015年4月5日 星期日
[2015][Homework]Team03 - Hw04
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言