顯示具有 [2015][Homework]Hw02 標籤的文章。 顯示所有文章
顯示具有 [2015][Homework]Hw02 標籤的文章。 顯示所有文章

2015年3月26日 星期四

[2015][Homework][Team03] - Hw02

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][Homework]Team02 - Hw02

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hw2
{
    class Program
    {
        static void Main(string[] args)
        {
            int sele;
            while (true)
            {
                Console.WriteLine("請輸入1或2執行第一題或第二題");
                Console.WriteLine("若要結束程式請輸入其他數字");
                sele = int.Parse(Console.ReadLine());
                switch (sele)
                {
                    case 1:
                        int kGcd;
                        Console.WriteLine("若 a, b 為整數, 求ax+by=c的整數解。");
                        Console.WriteLine("請輸入一個整數a");
                        int a = int.Parse(Console.ReadLine());
                        Console.WriteLine("請輸入一個整數b");
                        int b = int.Parse(Console.ReadLine());
                        Console.WriteLine("請輸入一個整數c");
                        int c = int.Parse(Console.ReadLine());

                        //演算步驟一     計算 a, b 的最大公因數 
                        int aCopy = a;
                        int bCopy = b;
                        int temp;
                        if (a > b)
                        {
                            while(aCopy % bCopy != 0)
                            {
                                temp = aCopy % bCopy;
                                aCopy = bCopy;
                                bCopy = temp;
                            }
                            kGcd = bCopy;

                        }
                        else
                        {
                            while(bCopy % aCopy != 0)
                            {
                                temp = bCopy % aCopy;
                                bCopy = aCopy;
                                aCopy = temp;
                            }
                            kGcd = aCopy;
                        }
                        
                        Console.WriteLine("最大公因數={0}",kGcd);
                        //演算步驟二     設(a, b) = k, 若 c 不為 k 的倍數, 則無解。 
                        if (c % kGcd != 0)
                        {
                            Console.WriteLine("無解");
                            Console.ReadLine();
                            Console.Clear();
                            break;
                        }
                        //演算步驟三     若 c = kc’
                        a /= kGcd;
                        b /= kGcd;
                        c /= kGcd;
                        //演算步驟四     求特殊解
                        int x = 0;
                        int y = 0;
                        for (y = 0; y <= a; y++)
                        {
                            if ((c - b * y) % a == 0)
                            {
                                x = (c - b * y) / a;
                                break;
                            }
                        }
                        //演算步驟五     通解
                        int t;
                        for (t = 0; t < 10; t++)
                        {
                            Console.WriteLine("x={0,3},y={1,3}", x, y);
                            x += b;
                            y -= a;
                        }
                        Console.ReadKey();
                        Console.Clear();
                        break;
                    case 2:
                        int aContainer = 3;
                        int bContainer = 5;
                        int aWater = 0;
                        int bWater = 0;
                        int step = 0;
                        Console.WriteLine("一開始,A桶有水{0}公升,B桶有水{1}公升\n", aWater, bWater);
                        while (bWater != 4)
                        {
                            step++;
                            if (bWater == 0)
                            {
                                bWater = bContainer;
                                Console.WriteLine("step{0}:把B桶裝滿水\n此時,A桶有水{1}公升,B桶有水{2}公升\n", step, aWater, bWater);

                            }
                            else if (aWater >= 3)
                            {
                                aWater = 0;
                                Console.WriteLine("step{0}:A桶水滿,把A桶的水全部倒掉\n此時,A桶有水{1}公升,B桶有水{2}公升\n", step, aWater, bWater);
                            }
                            else if (aWater + bWater >= aContainer)
                            {
                                bWater = aWater + bWater - aContainer;
                                aWater = aContainer;
                                Console.WriteLine("step{0}:將B桶的水倒滿A桶\n此時,A桶有水{1}公升,B桶有水{2}公升\n", step, aWater, bWater);
                            }
                            else if (aWater + bWater != 4)
                            {
                                aWater = aWater + bWater;
                                bWater = 0;
                                Console.WriteLine("step{0}:將B桶的水倒滿A桶\n此時,A桶有水{1}公升,B桶有水{2}公升\n", step, aWater, bWater);
                            }
                            else//bWater+aWater=4
                            {
                                bWater = aWater + bWater;
                                aWater = 0;
                                Console.WriteLine("step{0}:將A桶的水倒回B桶\n此時,A桶有水{1}公升,B桶有水{2}公升\n", step, aWater, bWater);
                            }
                        }
                        Console.WriteLine("最後,A桶有水{0}公升,B桶有水{1}公升\n", aWater, bWater);
                        Console.WriteLine("B桶有水{0}公升放在啟動開關上,解除炸彈", bWater);
                        Console.ReadLine();
                        Console.Clear();
                        break;
                    default:
                        return;
                }
            }
        }
    }
}