第一題
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.Threading.Tasks;
using System.Windows.Forms;
namespace Quiz
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void butSolve_Click(object sender, EventArgs e)
{
JugPuzzle Jug = new JugPuzzle();
Jug.message = txtDisplay;
Jug.bottleA = int.Parse(txtBottleA.Text);
Jug.bottleB = int.Parse(txtBottleB.Text);
Jug.target = int.Parse(txtTarget.Text);
Jug.solv();
}
private void butClear_Click(object sender, EventArgs e)
{
txtDisplay.Clear();
}
}
}
JugPuzzle.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Quiz
{
class JugPuzzle
{
public TextBox message;
public int bottleA;
public int bottleB;
public int target;
public void solv()
{
int gcd = GCD(bottleA, bottleB);
if ((bottleA + bottleB) < target)
{
message.AppendText("無法達到\n");
}
else
{
if (target % gcd == 0)
{
Jug(bottleA, bottleB, target);
}
else
{
message.AppendText("無解\n");
}
}
}
public static int GCD(int m,int n)
{
int temp = 0;
while(n!=0)
{
temp = m % n;
m = n;
n = temp;
}
return m;
}
public void Jug(int bottleA,int bottleB,int target)
{
int waterInA=0;
int waterInB=0;
int delta=0;
do
{
if (bottleA + bottleB == target)
{
waterInA += bottleA;
waterInB += bottleA;
message.AppendText("A桶裝滿,B桶裝滿\n");
message.AppendText("目前狀態:A桶有" + waterInA.ToString() + "公升,B桶有" + waterInB.ToString() + "公升\n");
message.AppendText("===================================\n");
}
else if (waterInA == 0)
{
waterInA += bottleA;
message.AppendText("A桶裝滿\n");
message.AppendText("目前狀態:A桶有" + waterInA.ToString() + "公升,B桶有" + waterInB.ToString() + "公升\n");
message.AppendText("===================================\n");
}
else if (waterInB == bottleB)
{
waterInB = 0;
message.AppendText("B桶倒光\n");
message.AppendText("目前狀態:A桶有" + waterInA.ToString() + "公升,B桶有" + waterInB.ToString() + "公升\n");
message.AppendText("===================================\n");
}
else
{
waterInB+=waterInA;
if(waterInB>bottleB)
{
delta=waterInB-bottleB;
waterInB=bottleB;
waterInA=delta;
}
else
{
waterInA=0;
}
message.AppendText("A桶往B桶倒水\n");
message.AppendText("目前狀態:A桶有" + waterInA.ToString() + "公升,B桶有" + waterInB.ToString() + "公升\n");
message.AppendText("===================================\n");
}
} while (waterInA != target && waterInB != target && waterInA + waterInB != target);
if(waterInA==target)
{
message.AppendText("放A桶解除炸彈");
}
else if (waterInB == target)
{
message.AppendText("放B桶解除炸彈");
}
else
{
message.AppendText("放A和B桶解除炸彈");
}
}
}
}
第二題
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.Threading.Tasks;
using System.Windows.Forms;
namespace _40173008H
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyDateTime DateTime = new MyDateTime();
DateTime.Year = int.Parse(txtYear.Text);
DateTime.Month = int.Parse(cboxMonth.Text);
DateTime.Day = int.Parse(cboxDay.Text);
if(rbtnIsLeap.Checked)
{
if(DateTime.Isleap()==true)
{
txtDisplay.AppendText(txtYear.Text+"是閏年\n");
}
else
{
txtDisplay.AppendText(txtYear.Text+"不是閏年\n");
}
}
else if (rbtnDaysOfMonth.Checked)
{
txtDisplay.AppendText(DateTime.Print());
if(DateTime.IsValid()==true)
txtDisplay.AppendText("是合法的\n");
else
txtDisplay.AppendText("是不合法的\n");
}
else if (rbtnWeekDay.Checked)
{
txtDisplay.AppendText(DateTime.Print());
int s = DateTime.WeekDay();
switch (s)
{
case 0:
txtDisplay.AppendText( "是星期日\n");
break;
case 1:
txtDisplay.AppendText( "是星期一\n");
break;
case 2:
txtDisplay.AppendText( "是星期二\n");
break;
case 3:
txtDisplay.AppendText("是星期三\n");
break;
case 4:
txtDisplay.AppendText( "是星期四\n");
break;
case 5:
txtDisplay.AppendText( "是星期五\n");
break;
case 6:
txtDisplay.AppendText("是星期六\n");
break;
}
}
else if (rbtnCalend.Checked)
{
txtDisplay.AppendText("\tSun\tMon\tTue\tWed\tThr\tFri\tSat\n");
DateTime.Day = 1;
int days = DateTime.DaysOfMonth();
int begin = DateTime.WeekDay();
for (int i = 0; i < begin; i++)
txtDisplay.AppendText("\t");
for (int i = 1; i <=days; i++)
{
txtDisplay.AppendText("\t" + i.ToString());
if((i+begin)%7==0)
txtDisplay.AppendText("\n");
}
}
else
{
txtDisplay.AppendText("請選擇一個項目");
}
}
private void button2_Click(object sender, EventArgs e)
{
txtDisplay.Clear();
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _40173008H
{
class MyDateTime
{
public int Year;
public int Month;
public int Day;
public bool Isleap()
{
if (Year % 4 == 0 && Year % 100 != 0)
return true;
else if (Year % 400 == 0)
return true;
else
return false;
}
public int DaysOfMonth()
{
int day;
if (Month == 2)
{
if (Isleap() == false)
day = 28;
else
day = 29;
}
else if (Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12)
day=31;
else
day=30;
return day;
}
public bool IsValid()
{
if (Month == 2)
{
if (Day > 29)
return false;
else if (Isleap() == false && Day == 29)
return false;
else
return true;
}
else if (Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12)
{
if (Day > 31)
return false;
else
return true;
}
else
{
if (Day > 30)
return false;
else
return true;
}
}
public int WeekDay()
{
if (Month == 1 || Month == 2)
Year -= 1;
int y = Year % 100;
int c = Year / 100;
int w=0;
int d = Day;
int m = ((Month + 9) % 12) + 1;
w =(d+ Convert.ToInt16( Math.Floor(2.6 * m - 0.2)) + y + (y / 4) + (c / 4) - 2 * c) % 7;
if (w < 0)
w += 7;
return w;
}
public string Print()
{
string str = "";
str += Year.ToString();
str += "/";
str += Month.ToString();
str += "/";
str += Day.ToString();
return str;
}
}
}
沒有留言:
張貼留言