2015年3月25日 星期三

[2015][Homework]Team01 - Hw02

2-1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework2_1
{
    class Program
    {
        static void Main(string[] args)
        {
            ///題目:若a、b為整數,求 ax + by = c 的整數解

            int a;
            int b;
            int c;
            int k;
            int x;
            int y;
            char selection;

            while (true)
            {
                Console.Write("請輸入a、b、c 值 \na=");
                a = Convert.ToInt32(Console.ReadLine());

                Console.Write("b=");
                b = Convert.ToInt32(Console.ReadLine());

                Console.Write("c=");
                c = Convert.ToInt32(Console.ReadLine());

                k = gcd(a, b); //Greatest Common Divisor
                x = 0;

                if (c % k != 0)
                {
                    Console.WriteLine("({0},{1}) = {2} 不是 {3} 的因數,無整數解", a, b, k, c);
                }
                else
                {
                    for (y = 0; y <= a; y++)
                    {
                        x = (c - b * y) % a;
                        if (x == 0)
                        {
                            x = (c - b * y) / a;
                            break;
                        }
                    }
                    Console.WriteLine("{0}x + {1}y = {2} 的通解:\nx = {3} + {4}*t \ny = {5} - {6}*t", a, b, c, x, b / k, y, a / k);
                }

                Console.WriteLine("\n是否繼續執行?(Y/N)");
                selection = char.Parse(Console.ReadLine());

                if (selection == 'Y' || selection == 'y')
                {
                    Console.Clear();
                }
                else
                {
                    break;
                }
            }
        }
        public static int gcd(int a, int b) //輾轉相除法求GCD
        {
            if (a % b == 0)
            {
                return b;
            }
            else
            {
                return gcd(b, a % b);
            }
        }
    }
}

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

namespace Homework2_2
{
    class Program
    {
        static void Main(string[] args)
        {
            ///分水遊戲

            int step;
            int capacity = 0;

            Console.WriteLine("分水的步驟如下:");

            for (step = 1; ; step++)
            {
                capacity += 3;
                if(capacity >= 5)
                {
                    capacity -= 5;
                    Console.WriteLine("步驟{0}:將量筒A(容量3L)裝滿水,倒入量筒B(容量5L)中,直到量筒B(容量5L)達滿水位", step);
                    step += 1;
                    Console.WriteLine("步驟{0}:把量筒B(容量5L)的水全部倒掉,並將量筒A(容量3L)中剩餘的水倒入量筒B(容量5L)",step);
                    Console.WriteLine("\t此時,量筒B(容量5L)中有{0}公升的水", capacity);
                }
                else
                {
                    Console.WriteLine("步驟{0}:將量筒A(容量3L)裝滿水,倒入量筒B(容量5L)中", step);
                    Console.WriteLine("\t此時,量筒B(容量5L)中有{0}公升的水", capacity);
                }
                if (capacity == 4)
                {
                    break;
                }
            }
            Console.WriteLine("經過以上{0}個步驟,目標達成",step);
            
            Console.Read();
        }
    }
}

5 則留言: