namespace practice
{
class homework2
{
static double findGreatestCommonDivisor(double CoefficientA, double CoefficientB)
{
if (CoefficientB > CoefficientA)//確保
{
double temp;
temp = CoefficientB;
CoefficientB = CoefficientA;
CoefficientA = temp;
}//(為確保較大的數字在前面,故需要交換位置)
double mod = CoefficientA % CoefficientB;//第一次輾轉相除
while (mod > 0)//若最大公因數==較小的數,直接回傳較小的數
{
CoefficientA = mod;//將第一次輾轉相除的餘數變成較小的係數
CoefficientB %= CoefficientA;/*準備第二次輾轉相除,讓第一次的餘數,對原本較小的係數相除。
處理過後做完重新回到開頭的情況 */
mod = CoefficientA % CoefficientB;//第二次輾轉相除
if (mod == 0)//判斷若mod==0則直接回傳第二次輾轉相除後較小的細數
return CoefficientB;
else
continue;//若不等於0則繼續做
}
return CoefficientB;
}//找最大公因數
static double [,]particularSolution(double CoefficientA, double CoefficientB, double CoefficientC, double gcd)
{
if (CoefficientC % gcd != 0)//若 c 不為最大公因數的倍數, 則無解。
{
Console.WriteLine("{0}x + {1}y = {2} : 此題無解", CoefficientA, CoefficientB, CoefficientC);
return null;
}
else if (CoefficientC % gcd == 0)//若 c 為最大公因數的倍數, 則同除最大公因數。
{
CoefficientA /= gcd;
CoefficientB /= gcd;
CoefficientC /= gcd;
}
double[,] particularSolution = new double[(int)CoefficientA + 1, (int)CoefficientA + 1];
//創一個放特解的假二維陣列
for (int i = 0; i <= CoefficientA; i++)
{
particularSolution[0, i] = (CoefficientC - (CoefficientB * i)) / (CoefficientA);
//在0<=y<=a的範圍內, 尋找 x 的整數解(x=(c'-b'y)/a必須為整數)
particularSolution[1, i] = i;
}
return particularSolution;
}//求特解
static double[, ,] generalSolution(double CoefficientA, double CoefficientB, double CoefficientC, double gcd,double[,] particularSolutionArr)
{
CoefficientA /= gcd;
CoefficientB /= gcd;
CoefficientC /= gcd;
double[, ,] generalSolution = new double[(int)CoefficientA + 1, (int)CoefficientA + 1, 21];
//創一個放通解的假二維陣列,一組特解配一組通解,故維度是一樣的
for (int i = 0; i <= CoefficientA; i++)
{
for (int t = 0; t <= 3; t++)
{
generalSolution[0, i,t] = particularSolutionArr[0, i] + CoefficientB * (t-10);
//x=x0+b't
generalSolution[1, i,t] = particularSolutionArr[1, i] - CoefficientA * (t-10);
} //y=y0-a't
}
return generalSolution;
}//求通解
static void jogPuzzle()
{
int bottleA = 0;
int bottleB = 0;
int delta = 0;//A跟B的差值
int clear = 0;
do
{
if (bottleA == 0)
{
bottleA += 3;
Console.WriteLine("A杯子裝滿3公升");
Console.WriteLine("目前狀態:({0},{1})",bottleA,bottleB);
}
else if (bottleB == 5)
{
bottleB = 0;
}
else if (bottleA == 3)
{
bottleA -= 3;
bottleB += 3;
if (bottleB > 5)
{
delta = bottleB - 5;
bottleB = 5;
bottleA = delta;
}
Console.WriteLine("A杯子往B杯倒水");
Console.WriteLine("目前狀態:({0},{1})", bottleA, bottleB);
}
else
{
bottleB += bottleA;
if (bottleB > 5)
{
delta = bottleB - 5;
bottleB = 5;
bottleA = delta;
}
else
{
bottleA = 0;
}
Console.WriteLine("A杯子往B杯倒水");
Console.WriteLine("目前狀態:({0},{1})", bottleA, bottleB);
}
} while (bottleB != 4);
Console.Read();
}
static void Main(string[] args)
{
string temp;
double CoefficientA;
double CoefficientB;
double CoefficientC;
double gcd;
double[,] particularSolutionArr;//特解的假二維陣列
double[, ,] generalSolutionArr;//通解的假三維陣列
do
{
Console.WriteLine("一、若 a,b 為整數, 求 ax + by = c 的整數解。");
Console.WriteLine("輸入參數a");
temp = Console.ReadLine();
CoefficientA = double.Parse(temp);
Console.WriteLine("輸入參數b");
temp = Console.ReadLine();
CoefficientB = double.Parse(temp);
Console.WriteLine("輸入參數c");
temp = Console.ReadLine();
CoefficientC = double.Parse(temp);
gcd = findGreatestCommonDivisor(CoefficientA, CoefficientB);//求最大公因數
particularSolutionArr = particularSolution(CoefficientA, CoefficientB, CoefficientC, gcd);//解特解之meothd
} while (particularSolutionArr == null);
generalSolutionArr = generalSolution(CoefficientA, CoefficientB, CoefficientC,gcd, particularSolutionArr);//解通解meothd
Console.WriteLine("{0}x + {1}y = {2}", CoefficientA, CoefficientB, CoefficientC);
for (int i = 0; i <= (CoefficientA/gcd); i++)//印出特解
{
Console.WriteLine("第{0}組特解(particularSolution):",i+1);
Console.WriteLine("x:{0},y:{1}\n",particularSolutionArr[0, i], particularSolutionArr[1, i]);//印特解
Console.WriteLine("第{0}組通解(particularSolution):",i+1);
for (int j = 0; j <= (CoefficientA / gcd); j++)//印出通解
{
for (int t = 0; t <= 3; t++)
{
Console.WriteLine("x:{0},y:{1}\n", generalSolutionArr[0, j, t], generalSolutionArr[1, j, t]);//印通解
}
}
}
Console.WriteLine("===============我是分隔線=================");
jogPuzzle();
Console.ReadKey();
}
}
}
2015年3月26日 星期四
[2015][Homework][Team03] - Hw02
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言