MidExam_第一題 jug 視窗主程式
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 JugPuzzle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSolve_Click(object sender, EventArgs e)
{
JugPuzzle jug = new JugPuzzle();
jug.Message = txtMessage;
jug.CapacityOfJugA = Convert.ToInt16(txtCapacityOfJugA.Text);
jug.CapacityOfJugB = Convert.ToInt16(txtCapacityOfJugB.Text);
jug.Target = Convert.ToInt16(txtTarget.Text);
jug.solve();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtMessage.Clear();
}
}
}
JugPuzzle 分水遊戲類別
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JugPuzzle
{
class JugPuzzle
{
public int CapacityOfJugA;
public int CapacityOfJugB;
public int Target;
public TextBox Message;
public int WaterInA;
public int WaterInB;
public int temp;
public void solve()
{
if (Target % gcd(CapacityOfJugA, CapacityOfJugB) != 0)
{
Message.AppendText("無解");
}
else if ((CapacityOfJugA + CapacityOfJugB) < Target)
{
Message.AppendText("沒可能的事,作夢吧!\n");
}
else
while (true)
{
//條件:A 是空的
//動作:把 A 加滿
if (WaterInA == 0)
{
WaterInA = CapacityOfJugA;
Action(WaterInA, WaterInB);
if (done() == true)
break;
}
//條件:A 是滿的
//動作:把 A 倒入 B
else if (WaterInA == CapacityOfJugA)
{
//若 A 比 B 剩下的容量多
if (WaterInA > CapacityOfJugB - WaterInB)
{
temp = (CapacityOfJugB - WaterInB);
WaterInB += (CapacityOfJugB - WaterInB);
WaterInA -= (temp);
Action(WaterInA, WaterInB);
if (done() == true)
break;
}
//若 A 比 B 剩下的容量少
else if (WaterInA < (CapacityOfJugB - WaterInB))
{
temp = (CapacityOfJugB - WaterInB);
WaterInB += WaterInA;
WaterInA = 0;
Action(WaterInA, WaterInB);
if (done() == true)
break;
}
}
//條件:B 是滿的
//動作:把 B 倒空
else if (WaterInB == CapacityOfJugB)
{
WaterInB = 0;
Action(WaterInA, WaterInB);
if (done() == true)
break;
}
//條件:A 未滿,B 是空的
//動作:將 A 倒入 B
else if (WaterInA > 0 && WaterInA < CapacityOfJugA)
{
//若 A 比 B 剩下的容量多
if (WaterInA > CapacityOfJugB - WaterInB)
{
temp = (CapacityOfJugB - WaterInB);
WaterInB += (CapacityOfJugB - WaterInB);
WaterInA -= (temp);
Action(WaterInA, WaterInB);
if (done() == true)
break;
}
//若 A 比 B 剩下的容量少
else if (WaterInA < (CapacityOfJugB - WaterInB))
{
WaterInB += WaterInA;
WaterInA = 0;
Action(WaterInA, WaterInB);
if (done() == true)
break;
}
}
}
Message.AppendText("Done!!");
}
void Action(int waterInA,int waterInB)
{
Message.AppendText("(A,B)=("+waterInA.ToString()+','+waterInB.ToString()+")\n");
}
bool done()
{
if (WaterInA == Target || WaterInB == Target || (WaterInA + WaterInB) == Target)
return true;
else
return false;
}
bool isSolvable()
{
if (Target % (gcd(CapacityOfJugA, CapacityOfJugB))!=0)
return true;
else
return false;
}
public int gcd(int num1, int num2)
{
int temp;
while (true)
if (num2 != 0)
{
temp = num1 % num2;
num1 = num2;
num2 = temp;
}
else break;
return num1;
}
}
}
MidExam_第二題
視窗主程式
//
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 MidExam_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_run_Click(object sender, EventArgs e)
{
MyDateTime date = new MyDateTime();
date.year = int.Parse(txt_year.Text);
date.month = int.Parse(cbox_month.Text);
date.day = int.Parse(cbox_day.Text);
if (rbtn_isLeap.Checked)
{
if (date.IsLeap() == true)
txt_message.AppendText(date.year.ToString() + "年是閏年\n");
else
txt_message.AppendText(date.year.ToString() + "年是平年\n");
}
if (rbtn_isLegal.Checked)
{
if (date.IsValid())
txt_message.AppendText(date.year.ToString() + '/' + date.month.ToString() + '/' + date.day.ToString() + "合法\n");
else
txt_message.AppendText(date.year.ToString() + '/' + date.month.ToString() + '/' + date.day.ToString() + "不合法\n");
}
if (rbtn_weekDay.Checked)
{
string str;
switch (date.Weekday())
{
case 1:
str = "星期一";
break;
case 2:
str = "星期二";
break;
case 3:
str = "星期三";
break;
case 4:
str = "星期四";
break;
case 5:
str = "星期五";
break;
case 6:
str = "星期六";
break;
case 0:
str = "星期日";
break;
default:
str = "0";
break;
}
txt_message.AppendText(date.year.ToString() + '/' + date.month.ToString() + '/' + date.day.ToString()+'是'+str.ToString()+'\n');
}
if (rbtn_calender.Checked)
{
date.day = 1;
int begin;
begin = date.Weekday();
txt_message.AppendText("\tSun\tMon\tTue\tWed\tThr\tFri\tSat\n");
for (int i = 0; i < begin; i++)
txt_message.AppendText("\t");
for (int i = 1; i < date.DayOfMonth(); i++)
{
txt_message.AppendText("\t"+i.ToString());
if ((i + begin) % 7 == 0)
txt_message.AppendText("\n");
}
}
}
private void btn_clear_Click(object sender, EventArgs e)
{
txt_message.Clear();
}
}
}
MyDateTime類別
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MidExam_2
{
class MyDateTime
{
public int year;
public int month;
public int day;
public bool IsLeap()
{
if (this.year % 4 == 0 && this.year % 100 != 0 || this.year % 400 == 0)
return true;
else
return false;
}
public int DayOfMonth()
{
switch (this.month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (IsLeap())
return 29;
else
return 28;
default:
return 0;
}
}
public bool IsValid()
{
if (month > 12 || month < 0)
return false;
if (day < 0 || day > DayOfMonth())
return false;
else
return true;
}
public int Weekday()
{
int tempYear;
int tempMonth;
int y;
int c;
int w;
tempYear = year;
if (month == 1 || month == 2)
{
tempYear -= 1;
}
tempMonth = ((month + 9) % 12) + 1;
y = tempYear % 100;
c = tempYear / 100;
w = (int)((day + Math.Floor(2.6 * tempMonth - 0.2) + y + (y / 4) + (c / 4) - (2 * c)) % 7);
if (w < 0)
w += 7;
return w;
}
}
}
沒有留言:
張貼留言