2015年3月26日 星期四

[2015][Homework]Team01 - Hw03

[2015][Homework]Team04 - Hw04

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

namespace JugPuzzle
{
    class Program
    {
        static void Main(string[] args)
        {
            int capacityA;
            int capacityB;
            int gcd;
            int target;

            Console.WriteLine("Jug Puzzle!");
            Console.WriteLine("Please input cylinderA capacity");      //key in
            capacityA = int.Parse(Console.ReadLine());
            Console.WriteLine("Please input cylinderB capacity");
            capacityB = int.Parse(Console.ReadLine());
            Console.WriteLine("Please input target capacity");
            target = int.Parse(Console.ReadLine());
            Console.WriteLine("*******************************************************");

            gcd = Program.gcd(capacityA, capacityB);      //find gcd

            if (target % gcd == 0)      //Is solvable?
            {
                if (capacityA + capacityB >= target)
                {
                    Program.jugPuzzle(capacityA, capacityB, target);
                }
                else
                {
                    Console.WriteLine("Not solvable!");
                }
            }
            else
            {
                Console.WriteLine("Not solvable!");
            }
            Console.Read();
        }

        static int gcd(int m, int n)      //最大公因數
        {
            int temp = 0;
            while (n != 0)
            {
                temp = m % n;
                m = n;
                n = temp;
            }
            return m;
        }

        static void jugPuzzle(int capacityA, int capacityB, int target)
        {
            int tem;
            int waterInA = 0;
            int waterInB = 0;

            if (capacityA > capacityB)      //if capacityA>B, exchange each other
            {
                tem = capacityA;
                capacityA = capacityB;
                capacityB = tem;
                Console.WriteLine("Capavity of B is smaller then capacity of A");
                Console.WriteLine("Exchange: capavity of A is {0}(smaller), and capacity of B is {1}(larger)", capacityA, capacityB);
            }
            else if (capacityA < capacityB)
            {
                Console.WriteLine("Capavity of A is smaller than capacity of B");
            }
            else
            {
                Console.WriteLine("Capavity of A and capacity of B are same");
            }
            Console.WriteLine("*******************************************************");

            while (true)
            {
                if (capacityA + capacityB == target)      //if capacity A + capacity B = target
                {
                    waterInA = capacityA;
                    waterInB = capacityB;
                    Console.WriteLine("Fill in cylinderA and cylinderB");
                    Console.WriteLine("cylinderA:({0}) cylinderB:({1})", waterInA, waterInB);
                    if (waterInA == target || waterInB == target || (waterInA + waterInB) == target)
                    {
                        break;
                    }
                }
                if (waterInA == 0)      //if A is empty
                {
                    waterInA = capacityA;
                    Console.WriteLine("Fill in cylinderA");
                    Console.WriteLine("cylinderA:({0}) cylinderB:({1})", waterInA, waterInB);
                    if (waterInA == target || waterInB == target || (waterInA + waterInB) == target)
                    {
                        break;
                    }
                }
                if (capacityB - waterInB > capacityA)      //if A pour into B
                {
                    waterInB = waterInB + waterInA;
                    waterInA = 0;
                    Console.WriteLine("CylinderA pour into cylinderB");
                    Console.WriteLine("cylinderA:({0}) cylinderB:({1})", waterInA, waterInB);
                    if (waterInA == target || waterInB == target || (waterInA + waterInB) == target)
                    {
                        break;
                    }
                }
                else
                {
                    waterInA = waterInA - (capacityB - waterInB);
                    waterInB = capacityB;
                    Console.WriteLine("CylinderA pour into cylinderB");
                    Console.WriteLine("cylinderA:({0}) cylinderB:({1})", waterInA, waterInB);
                    if (waterInA == target || waterInB == target || (waterInA + waterInB) == target)
                    {
                        break;
                    }
                }
                if (waterInB == capacityB)      //if B is full, pour out
                {
                    waterInB = 0;
                    waterInB = waterInA;
                    waterInA = 0;
                    Console.WriteLine("CylinderB is full, pour out");
                    Console.WriteLine("And cylinderA pour into cylinderB");
                    Console.WriteLine("cylinderA:({0}) cylinderB:({1})", waterInA, waterInB);
                    if (waterInA == target || waterInB == target || (waterInA + waterInB) == target)
                    {
                        break;
                    }
                }
            }
            Console.WriteLine("Done!");
        }
    }
}

[2015][Homework]Team03 - Hw01 (Revised)

namespace HW1
{
    class Program
    {
        static void printArmstrongNumber()
        {
            int first, second, third,num;
            for (int i = 100; i < 1000; i++)
            {
                first = i / 100;
                second = i / 10 % 10;
                third = i % 10;
                num = first * first * first + second * second * second + third * third * third;
                if (i == num)
                    Console.WriteLine(i);
            }
        }
        static bool determineLeapYear(int year)//閏年 回傳True
        {
            if (((year % 400) == 0) || ((year % 4 == 0) && (year % 100 != 0))) 
                return true;
            else return false;
        }
        static int countDayOfMonth(int year, int month)
        { 
            if (month != 2)
            {
                int big = 1;
                if (month <= 7)
                {
                    if (month % 2 == 0) big = 0;
                }
                else
                    if (month % 2 == 1) big = 0;
                if (big == 1) return 31;
                else return 30;
            }
            else                    //2月
                if (Program.determineLeapYear(year)) return 29;
                else return 28;
        }//回傳天數
        static void findWeek(int year, int month, int day)
        {
            int Difference;
            Difference = countDayBetweenOtherDay(1, 1, 1, year, month, day);
           // Difference++;
            if (Difference == -1) Console.WriteLine("星期1");
            Difference = Difference % 7;
            if (Difference == 0)
                Console.WriteLine("星期2");
            if (Difference == 1)
                Console.WriteLine("星期3");
            if (Difference == 2)
                Console.WriteLine("星期4");
            if (Difference == 3)
                Console.WriteLine("星期5");
            if (Difference == 4)
                Console.WriteLine("星期6");
            if (Difference == 5)
                Console.WriteLine("星期天");
            if (Difference == 6)
                Console.WriteLine("星期1");
        }
        static bool determineDay(int year, int month, int day)//有效日期判斷
        {
            if ((month <= 12) && (month >= 1) && ((day <= countDayOfMonth(year, month)) && (day > 0))) return true;
            else return false;
        }
        static int countDayBetweenOtherDay(int year, int month, int day, int year2, int month2, int day2)//回傳 兩日相差天數
        {
            int dayOffset = 0;//offset
            for (int i = year2; i > year; i--)//年換算
            {
                if (determineLeapYear(i - 1))
                    dayOffset += 366;
                else dayOffset += 365;
            }
            for (int i = month2; i > 1; i--)//月換算(轉成1/1)
                    dayOffset += countDayOfMonth(year2, i - 1);
                for (int i = month; i > 1; i--)//月換算(轉成1/1)
                    dayOffset -= countDayOfMonth(year, i - 1);
                    return day2 + dayOffset -day - 1;
        }
 
        static void Main(string[] args)
        {
            string inputTemp;//input的暫存器
            int choose;
            int year;
            int month;
            int day;
            int year2;
            int month2;
            int day2;

            while(true)
            {
            #region input
            Console.WriteLine("請輸入要測哪一題:");
            inputTemp=Console.ReadLine();
            choose = Convert.ToInt32(inputTemp);
            #endregion
            #region test1
            if (choose == 1)
            {
                Console.WriteLine("請寫出可以列印出所有三位數(100~999)之『阿姆斯壯數』");
                printArmstrongNumber();
            }
            #endregion
            #region test2
            else if (choose == 2)
            {
                Console.WriteLine("輸入西元年份,讓我幫你算算是不是閏年:");
                inputTemp = Console.ReadLine();
                year = Convert.ToInt32(inputTemp);
                if (determineLeapYear(year)) Console.WriteLine("是閏年!");
                else Console.WriteLine("是平年!");
            }
            #endregion
            #region test3
            else if (choose == 3)
            {
                Console.WriteLine("輸入西元年份及月份,讓我幫你算算該月有幾天:");
                Console.WriteLine("請先輸入西元年份:");
                inputTemp = Console.ReadLine();
                year = Convert.ToInt32(inputTemp);
                Console.WriteLine("請輸入月份:");
                inputTemp = Console.ReadLine();
                month = Convert.ToInt32(inputTemp);
                Console.WriteLine(countDayOfMonth(year, month));
            }
            #endregion
            #region test4
            else if (choose == 4)
            {
                Console.WriteLine("輸入西元年份及月份和日期,讓我幫你算算是星期幾:");
                Console.WriteLine("請先輸入西元年份:");
                inputTemp = Console.ReadLine();
               year = Convert.ToInt32(inputTemp);
                Console.WriteLine("請輸入月份:");
                inputTemp = Console.ReadLine();
                month = Convert.ToInt32(inputTemp);
                Console.WriteLine("請輸入日期:");
                inputTemp = Console.ReadLine();
               day = Convert.ToInt32(inputTemp);
               findWeek(year, month, day);
            }
            #endregion
            #region test5
            else if (choose == 5)
            {
                Console.WriteLine("輸入西元年份及月份和日期,讓我幫你算算是不是合法日期:");
                Console.WriteLine("請先輸入西元年份:");
                inputTemp = Console.ReadLine();
               year = Convert.ToInt32(inputTemp);
                Console.WriteLine("請輸入月份:");
                inputTemp = Console.ReadLine();
                month = Convert.ToInt32(inputTemp);
                Console.WriteLine("請輸入日期:");
                inputTemp = Console.ReadLine();
               day = Convert.ToInt32(inputTemp);
                if (determineDay(year, month,day)) Console.WriteLine("合法!");
                else
                    Console.WriteLine("不合法!");
            }
            #endregion
            #region test6
            else if (choose == 6)
            {
                Console.WriteLine("輸入兩個西元年份及月份和日期,讓我幫你算算總共差幾天:");
                Console.WriteLine("請先輸入(起始)西元年份:");
                inputTemp = Console.ReadLine();
               year = Convert.ToInt32(inputTemp);
                Console.WriteLine("請輸入(起始)月份:");
                inputTemp = Console.ReadLine();
                month = Convert.ToInt32(inputTemp);
                Console.WriteLine("請輸入(起始)日期:");
                inputTemp = Console.ReadLine();
               day = Convert.ToInt32(inputTemp);
                Console.WriteLine("請先輸入(結束)西元年份:");
                inputTemp = Console.ReadLine();
                year2 = Convert.ToInt32(inputTemp);
                Console.WriteLine("請輸入(結束)月份:");
                inputTemp = Console.ReadLine();
                month2 = Convert.ToInt32(inputTemp);
                Console.WriteLine("請輸入(結束)日期:");
                inputTemp = Console.ReadLine();
                day2 = Convert.ToInt32(inputTemp);
                Console.WriteLine(countDayBetweenOtherDay(year, month, day, year2, month2, day2));
            }
            #endregion
            else Console.WriteLine("不合法的輸入= =");
 
        }
        }
    }
}

[2015][Homework]Team02- Hw03

類別程式碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HW2
{
    class MyDateTime
    {
        //field
        public int Year;
        public int Month;
        public int Day;
        //Method
        public bool IsLeap()
        {
            if (Year % 4 == 0 && Year % 100 != 0)
                return true;
            else if (Year % 400 == 0)
                return true;
            else
                return false;
        }
        public void Print()
        {
            Console.WriteLine("{0}/{1}/{2}", Year, Month, Day);
        }
        public int DaysOfYear()
        {
            if(IsLeap () == true)
                return 366;
            else
                return 365;
        }
        public int DaysOfMonth()
        {
            if (Month == 2)
            {
                if (IsLeap() == true)
                {
                    return 29;
                }
                else
                    return 28;
            }
            else if (Month % 2 == 0 && Month >= 8)
                return 31;
            else if (Month % 2 == 1 && Month <= 7)
                return 31;
            else
                return 30;
        }
        public bool isLegal()
        {
            if (Day > 31 || Month > 12)
                return false;
            else if (Day == 31 && Month % 2 == 0)
            {
                if ((Month == 8) || (Month == 10) || (Month == 12))
                    return false;
                else
                    return false;
            }
            else if (Day == 31 && Month % 2 == 1)
            {
                if ((Month == 9) || (Month == 11))
                    return false;
                else
                    return true;
            }
            else if (Day >= 30 && Month == 2)
                 return false;
            else if (Day == 29 && Month == 2)
            {
                if (Year % 4 == 0)
                {
                    if (Year % 100 == 0 && Year % 400 != 0)
                        return false;
                    else
                        return true;
                }
                else
                    return false;
            }
            else
                return true;
                        
        }
        public string WeekDay()
        {
            if (Month == 1 || this.Month == 2)
            {
                Month += 12;
                Year--;
            }
            int week = (Day + 2 * Month + 3 * (Month + 1) / 5 + Year + Year / 4 - Year / 100 + Year / 400 + 1) % 7;
            switch (week)
            {
                case 1:
                   return "Monday";
                case 2:
                    return "Tuesday";
                 case 3:
                    return "Wednesday";
                case 4:
                    return "Thursday";
                case 5:
                    return "Friday";
                case 6:
                    return "Saturday";
                case 0:
                    return "Sunday";
                default:
                    return "false";
            }
        }
    }
}

主程式碼
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)
        {
            MyDateTime date = new MyDateTime();
            while (true)
            {
                Console.WriteLine("Input Year");
                date.Year = int.Parse(Console.ReadLine());
                Console.WriteLine("Input Month");
                date.Month = int.Parse(Console.ReadLine());
                Console.WriteLine("Input Day");
                date.Day = int.Parse(Console.ReadLine());
                if (date.isLegal() == false)
                {
                    Console.WriteLine("{0},{1},{2} is not a legal date!", date.Year, date.Month, date.Day);
                    Console.WriteLine("Input Again!");
                    Console.ReadLine();
                    Console.Clear();
                    continue;
                }
                Console.WriteLine("************************************************************");
                switch (date.IsLeap())
                {
                    case true:
                        Console.WriteLine("Year {0} is a leap year!", date.Year);
                        break;
                    case false:
                        Console.WriteLine("Year {0} is not a leap year!", date.Year);
                        break;
                }
                Console.WriteLine("Days of year {0} = {1}", date.Year, date.DaysOfYear());
                Console.WriteLine("Days of {0}/{1} = {2}", date.Year, date.Month, date.DaysOfMonth());
                Console.WriteLine("{0}/{1}/{2} is {3}", date.Year, date.Month, date.Day, date.WeekDay());
                Console.WriteLine("************************************************************");
                Console.WriteLine("Again? Y/N");
                char select = Convert.ToChar(Console.ReadLine());
                if (select != ('y') && select != ('Y'))
                {
                    break;
                }
                Console.Clear();
            }
            Console.WriteLine("Program end!");
            Console.ReadLine();
        }
    }
}

[2015][Homework]Team05 - Hw03

[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 - Hw01 (Revised)

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

namespace Hw1
{
    class Program
    {
        static void Main(string[] args)
        {
            
            int year; 
            int month; 
            int day;
            int select;
            while (true)
            {
                Console.WriteLine("請輸入題號1~6來執行以下題目");
                Console.WriteLine("第1題 阿姆斯壯數");
                Console.WriteLine("第2題 判斷閏年");
                Console.WriteLine("第3題 判斷天數");
                Console.WriteLine("第4題 判斷星期幾");
                Console.WriteLine("第5題 判斷日期是否合法");
                Console.WriteLine("第6題 判斷日期相差天數");
                Console.WriteLine("請輸入非1~6的數字來結束程式");
                select = int.Parse(Console.ReadLine());
                switch (select)
                {
                    case 1:
                        //第1題 阿姆斯壯數
                        int a = 0;
                        int b = 0;
                        int c = 0;
                        double sum = 0;
                        Console.WriteLine("顯示100~999間的阿姆斯壯數");
                        for (int i = 100; i <= 999; i++)
                        {
                            a = i / 100;
                            b = i / 10 % 10;
                            c = i % 10;
                            sum = Math.Pow((double)a, 3) + Math.Pow((double)b, 3) + Math.Pow((double)c, 3);
                            if (sum == i)
                                Console.WriteLine("{0}", i);
                        }
                        
                        break;
                    case 2:
                        //第2題 判斷閏年
                        Console.WriteLine("請輸入年分");
                        year = int.Parse(Console.ReadLine());
                        if (year % 4 == 0 && year % 100 != 0)
                            Console.WriteLine("{0}年是閏年", year);
                        else if (year % 400 == 0)
                            Console.WriteLine("{0}年是閏年", year);
                        else
                            Console.WriteLine("{0}這年不是閏年", year);
                        
                        break;
                    case 3:
                        //第3題 判斷天數
                        Console.WriteLine("請輸入年分");
                        year = int.Parse(Console.ReadLine());
                        Console.WriteLine("請輸入月分");
                        month = int.Parse(Console.ReadLine());
                        if (month > 12)
                            Console.WriteLine("這不是月份,呆呆!");
                        else if (month == 2)//閏年2月有29天
                        {
                            if (year % 4 == 0 && year % 100 != 0)
                                Console.WriteLine("{0}年的{1}月有29天", year, month);
                            else if (year % 400 == 0)
                                Console.WriteLine("{0}年的{1}月有29天", year, month);
                            else
                                Console.WriteLine("{0}年的{1}月有28天", year, month);
                        }
                        else if (month % 2 == 0 && month >= 8)
                            Console.WriteLine("這個月有31天");
                        else if (month % 2 == 1 && month <= 7)//基數月有31天
                            Console.WriteLine("這個月有31天");
                        else
                            Console.WriteLine("這個月有30天");
                       
                        break;

                    case 4:
                        //第4題 判斷星期幾
                        Console.WriteLine("請輸入年分");
                        year = int.Parse(Console.ReadLine());
                        Console.WriteLine("請輸入月分");
                        month = int.Parse(Console.ReadLine());
                        Console.WriteLine("請輸入日期");
                        day = int.Parse(Console.ReadLine());
                        int week;
                        //基姆拉爾森公式,需將1月和2月看成是上一年的13月和14月
                        if (month == 1 || month == 2)
                        {
                            month += 12;
                            year--;
                        }
                        week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400 + 1) % 7;
                        switch (week)
                        {
                            case 1:
                                Console.WriteLine("這天星期一");
                                break;
                            case 2:
                                Console.WriteLine("這天星期二");
                                break;
                            case 3:
                                Console.WriteLine("這天星期三");
                                break;
                            case 4:
                                Console.WriteLine("這天星期四");
                                break;
                            case 5:
                                Console.WriteLine("這天星期五");
                                break;
                            case 6:
                                Console.WriteLine("這天星期六");
                                break;
                            case 0:
                                Console.WriteLine("這天星期天");
                                break;
                        }
                        
                        break;

                    case 5:
                        //第5題 判斷日期合法與否
                        Console.WriteLine("請輸入年分");
                        year = int.Parse(Console.ReadLine());
                        Console.WriteLine("請輸入月分");
                        month = int.Parse(Console.ReadLine());
                        Console.WriteLine("請輸入日期");
                        day = int.Parse(Console.ReadLine());
                        if (day > 31 || month > 12)
                            Console.WriteLine("此日期不合法");
                        else if (day == 31 && month % 2 == 0)
                        {
                            if ((month == 8) || (month == 10) || (month == 12))
                                Console.WriteLine("此日期合法");
                            else
                                Console.WriteLine("此日期不合法");
                        }
                        else if (day == 31 && month % 2 == 1)
                        {
                            if ((month == 9) || (month == 11))
                                Console.WriteLine("此日期不合法");
                            else
                                Console.WriteLine("此日期合法");
                        }
                        else if (day >= 30 && month == 2)
                            Console.WriteLine("此日期不合法");
                        else if (day == 29 && month == 2)
                        {
                            if (year % 4 == 0)
                            {
                                if (year % 100 == 0 && year % 400 != 0)
                                    Console.WriteLine("此日期不合法");
                                else
                                    Console.WriteLine("此日期合法");
                            }
                            else
                                Console.WriteLine("此日期不合法");
                        }
                        else
                            Console.WriteLine("此日期合法");
                       
                        break;

                    case 6:
                        //第6題 判斷日期相差天數
                        int sumYear = 0;
                        int sumDay1 = 0;
                        int sumDay2 = 0;
                        int day1;
                        int day2;
                        int monthDay;
                        int leapYear = 0;
                        Console.WriteLine("請輸入第一個日期:範例:20150210");
                        int inputDate1 = int.Parse(Console.ReadLine());
                        Console.WriteLine("請輸入第二個日期:範例:20150416");
                        int inputDate2 = int.Parse(Console.ReadLine());
                        int inputYear1 = inputDate1 / 10000;
                        int inputMonth1 = inputDate1 % 10000 / 100;
                        int inputDay1 = inputDate1 % 100;
                        int inputYear2 = inputDate2 / 10000;
                        int inputMonth2 = inputDate2 % 10000 / 100;
                        int inputDay2 = inputDate2 % 100;
                        for (day1 = 1; day1 < inputDay1; day1++) ;
                        for (day2 = 1; day2 < inputDay2; day2++) ;
                        //date1
                        if ((inputYear1 % 4 == 0 && inputYear1 % 100 != 0) || (inputYear1 % 400 == 0))//潤
                        {
                            for (month = 1; month < inputMonth1; month++)
                            {
                                if (month == 2)
                                    monthDay = 29;
                                else if (month <= 8 && month % 2 == 1)
                                    monthDay = 31;
                                else if (month <= 8 && month % 2 == 0)
                                    monthDay = 30;
                                else if (month > 8 && month % 2 == 0)
                                    monthDay = 30;
                                else
                                    monthDay = 31;
                                sumDay1 = sumDay1 + monthDay;
                            }

                        }
                        else
                        {
                            for (month = 1; month < inputMonth1; month++)
                            {
                                if (month == 2)
                                    monthDay = 28;
                                else if (month <= 8 && month % 2 == 1)
                                    monthDay = 31;
                                else if (month <= 8 && month % 2 == 0)
                                    monthDay = 30;
                                else if (month > 8 && month % 2 == 0)
                                    monthDay = 30;
                                else
                                    monthDay = 31;
                                sumDay1 = sumDay1 + monthDay;
                            }
                        }
                        sumDay1 = sumDay1 + day1;
                        //date2
                        if ((inputYear2 % 4 == 0 && inputYear2 % 100 != 0) || (inputYear2 % 400 == 0))//潤
                        {
                            for (month = 1; month < inputMonth2; month++)
                            {
                                if (month == 2)
                                    monthDay = 29;
                                else if (month <= 8 && month % 2 == 1)
                                    monthDay = 31;
                                else if (month <= 8 && month % 2 == 0)
                                    monthDay = 30;
                                else if (month > 8 && month % 2 == 0)
                                    monthDay = 30;
                                else
                                    monthDay = 31;
                                sumDay2 = sumDay2 + monthDay;
                            }

                        }
                        else
                        {
                            for (month = 1; month < inputMonth2; month++)
                            {
                                if (month == 2)
                                    monthDay = 28;
                                else if (month <= 8 && month % 2 == 1)
                                    monthDay = 31;
                                else if (month <= 8 && month % 2 == 0)
                                    monthDay = 30;
                                else if (month > 8 && month % 2 == 0)
                                    monthDay = 30;
                                else
                                    monthDay = 31;
                                sumDay2 = sumDay2 + monthDay;
                            }
                        }
                        sumDay2 = sumDay2 + day2;
                        //year
                        for (year = inputYear1; year < inputYear2; year++)
                        {

                            if (year % 4 == 0 && year % 100 != 0)//潤
                            {

                                leapYear++;

                            }
                            else if (year % 400 == 0)
                            {

                                leapYear++;
                            }
                            else
                                sumYear++;

                        }
                        sumYear = sumYear * 365 + leapYear * 366;
                        Console.WriteLine("{0}天", sumYear - sumDay1 + sumDay2 - 1);

                        break;
                    default:
                        return;
                }
                Console.ReadKey();
                Console.Clear();
            }

        }
    }
}

[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;
                }
            }
        }
    }
}

2015年3月25日 星期三

[2015][Homework]Team05 - Hw01 (Revised)

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

namespace _40173007H_hw3
{
    class Program
    {
        static void Main(string[] args)
        {
            string select;
            int y, m, d, sel;
            while (1 == 1)
            {
                Console.WriteLine("輸入1~6題號");
                Console.WriteLine("輸入其他數字以結束程式");
                select = Console.ReadLine();
                sel = int.Parse(select);
                switch (sel)
                {
                    case (1):
                        //第一題 阿姆斯壯數
                        int a = 0;
                        int b = 0;
                        int c = 0;
                        double sum = 0;
                        Console.WriteLine("顯示100~999間的阿姆斯壯數");
                        for (int i = 100; i <= 999; i++)
                        {
                            a = i % 10;
                            b = i / 10 % 10;
                            c = i / 100 % 10;
                            sum = Math.Pow((double)a, 3) + Math.Pow((double)b, 3) + Math.Pow((double)c, 3);
                            if (sum == i)
                                Console.WriteLine("{0}", i);
                        }
                        Console.ReadKey();
                        Console.Clear();
                        break;
                    case (2):
                        //第二題 判斷閏年
                        Console.WriteLine("輸入年分");
                        y = int.Parse(Console.ReadLine());
                        if (y % 4 == 0 && y % 100 != 0)
                            Console.WriteLine("這年是閏年");
                        else if (y % 400 == 0)
                            Console.WriteLine("這年是閏年");
                        else
                            Console.WriteLine("這年不是閏年");
                        Console.ReadKey();
                        Console.Clear();
                        break;
                    case (3):
                        //第三題 判斷天數
                        Console.WriteLine("輸入年分");
                        y = int.Parse(Console.ReadLine());
                        Console.WriteLine("輸入月分");
                        m = int.Parse(Console.ReadLine());
                        if (m > 12)
                            Console.WriteLine("這不是月份!");
                        else if (m == 2)
                            if (y % 4 == 0)
                                Console.WriteLine("這個月有29天");
                            else
                                Console.WriteLine("這個月有28天");
                        else if (m % 2 == 1 || m == 8)
                            Console.WriteLine("這個月有31天");
                        else
                            Console.WriteLine("這個月有30天");
                        Console.ReadKey();
                        Console.Clear();
                        break;

                    case (4):
                        //第四題 判斷星期幾
                        Console.WriteLine("輸入年分");
                        y = int.Parse(Console.ReadLine());
                        Console.WriteLine("輸入月分");
                        m = int.Parse(Console.ReadLine());
                        Console.WriteLine("輸入日期");
                        d = int.Parse(Console.ReadLine());
                        int W;
                        if (m == 1)
                        {
                            y = y - 1;
                            m = 13;
                        }
                        else if (m == 2)
                        {
                            y = y - 1;
                            m = 14;
                        }
                        W = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7;
                        switch (W)
                        {
                            case (1):
                                Console.WriteLine("今天星期一");
                                break;
                            case (2):
                                Console.WriteLine("今天星期二");
                                break;
                            case (3):
                                Console.WriteLine("今天星期三");
                                break;
                            case (4):
                                Console.WriteLine("今天星期四");
                                break;
                            case (5):
                                Console.WriteLine("今天星期五");
                                break;
                            case (6):
                                Console.WriteLine("今天星期六");
                                break;
                            case (0):
                                Console.WriteLine("今天星期天");
                                break;
                        }
                        Console.ReadKey();
                        Console.Clear();
                        break;

                    case (5):
                        //第五題 判斷日期合法與否
                        Console.WriteLine("輸入年分");
                        y = int.Parse(Console.ReadLine());
                        Console.WriteLine("輸入月分");
                        m = int.Parse(Console.ReadLine());
                        Console.WriteLine("輸入日期");
                        d = int.Parse(Console.ReadLine());
                        if (d > 31)
                            Console.WriteLine("此日期不合法");
                        else if (d == 31 && m % 2 == 0)
                        {
                            if (m == 8 || m == 10 || m == 12)
                                Console.WriteLine("此日期合法");
                            else
                                Console.WriteLine("此日期不合法");
                        }
                        else if (d == 30 && m == 2)
                            Console.WriteLine("此日期不合法");
                        else if (d == 29)
                        {
                            if (y % 4 == 0)
                            {
                                if (y % 100 == 0 && y % 400 != 0)
                                    Console.WriteLine("此日期不合法");
                                else
                                    Console.WriteLine("此日期合法");
                            }
                            else
                                Console.WriteLine("此日期不合法");
                        }
                        else
                            Console.WriteLine("此日期合法");
                        Console.ReadKey();
                        Console.Clear();
                        break;

                    case (6):
                        //第六題 判斷日期相差天數
                        int totalDay1 = 0, totalDay2 = 0, diff = 0, leaps, tempsum;
                        for (int times = 1; times < 3; times++)
                        {
                            Console.WriteLine("輸入第{0}次年分", times);
                            y = int.Parse(Console.ReadLine());
                            Console.WriteLine("輸入第{0}次月分", times);
                            m = int.Parse(Console.ReadLine());
                            Console.WriteLine("輸入第{0}次日期", times);
                            d = int.Parse(Console.ReadLine());
                            leaps = y / 4 - y / 100 + y / 400;
                            switch (m)
                            {
                                case (1):
                                    tempsum = (y * 365 + leaps) + d;
                                    totalDay1 = totalDay2;
                                    totalDay2 = tempsum;
                                    break;
                                case (2):
                                    tempsum = (y * 365 + leaps) + 31 + d;
                                    totalDay1 = totalDay2;
                                    totalDay2 = tempsum;
                                    break;
                                case (3):
                                    tempsum = (y * 365 + leaps) + 59 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalDay1 = totalDay2;
                                    totalDay2 = tempsum;
                                    break;
                                case (4):
                                    tempsum = (y * 365 + leaps) + 90 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalDay1 = totalDay2;
                                    totalDay2 = tempsum;
                                    break;
                                case (5):
                                    tempsum = (y * 365 + leaps) + 120 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalDay1 = totalDay2;
                                    totalDay2 = tempsum;
                                    break;
                                case (6):
                                    tempsum = (y * 365 + leaps) + 151 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalDay1 = totalDay2;
                                    totalDay2 = tempsum;
                                    break;
                                case (7):
                                    tempsum = (y * 365 + leaps) + 181 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalDay1 = totalDay2;
                                    totalDay2 = tempsum;
                                    break;
                                case (8):
                                    tempsum = (y * 365 + leaps) + 212 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalDay1 = totalDay2;
                                    totalDay2 = tempsum;
                                    break;
                                case (9):
                                    tempsum = (y * 365 + leaps) + 243 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalDay1 = totalDay2;
                                    totalDay2 = tempsum;
                                    break;
                                case (10):
                                    tempsum = (y * 365 + leaps) + 273 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalDay1 = totalDay2;
                                    totalDay2 = tempsum;
                                    break;
                                case (11):
                                    tempsum = (y * 365 + leaps) + 304 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalDay1 = totalDay2;
                                    totalDay2 = tempsum;
                                    break;
                                case (12):
                                    tempsum = (y * 365 + leaps) + 334 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalDay1 = totalDay2;
                                    totalDay2 = tempsum;
                                    break;
                            }
                        }
                        diff = totalDay1 - totalDay2;
                        if (diff < 0)
                            Console.WriteLine("此兩日相差{0}天", -(diff - 1));
                        else
                            Console.WriteLine("此兩日相差{0}天", diff - 1);
                        Console.ReadKey();
                        Console.Clear();
                        break;
                    default:
                        return;
                }
            }

        }
       
    }
}

[2015][Homework]Team01 - Hw02

[2015][Homework]Team05 - Hw02

[2015][Homework]Team06 - Hw03