2015年3月23日 星期一

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

第一題
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1_1
{
    class Program
    {
        static void Main(string[] args)
        {
            //問題1
            int ArmstrongNumber;
            double sum;
            Console.WriteLine("1.阿姆斯壯數(100~999):");
            for (ArmstrongNumber = 100; ArmstrongNumber <= 999; ArmstrongNumber++)
            {
                sum = Math.Pow(ArmstrongNumber / 100, 3) + Math.Pow(ArmstrongNumber / 10 % 10, 3) + Math.Pow(ArmstrongNumber % 10, 3);
                if (ArmstrongNumber == sum)
                {
                    Console.WriteLine("{0}", ArmstrongNumber);
                }
            }

            Console.Read();
        }
    }
}
第二題
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1_2
{
    class Program
    {
        static void Main(string[] args)
        {
            //問題2
            int year;
            Console.WriteLine("2.\n輸入西元年:");
            year = int.Parse(Console.ReadLine());
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
            {
                Console.WriteLine("西元{0}年是閏年", year);
            }
            else
            {
                Console.WriteLine("西元{0}年是平年", year);
            }
            Console.Read();
        }
    }
}

第三題
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Homework1_3
{
    class Program
    {
        static void Main(string[] args)
        {
            int year,month;
            Console.WriteLine("請輸入年份");
            year = int.Parse(Console.ReadLine());
            Console.WriteLine("請輸入月份");
            month = int.Parse(Console.ReadLine());
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
            {
                switch (month)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        Console.WriteLine("{0}年{1}月有31天", year, month);
                        break;
                    case 2:
                         Console.WriteLine("{0}年{1}月有29天", year, month);
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        Console.WriteLine("{0}年{1}月有30天", year, month);
                        break;
                    default:
                        Console.WriteLine("輸入錯月份");
                        break;
               }       
            }
            else
            {
                switch (month)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        Console.WriteLine("{0}年{1}月有31天", year, month);
                        break;
                    case 2:
                        Console.WriteLine("{0}年{1}月有29天", year, month);
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        Console.WriteLine("{0}年{1}月有30天", year, month);
                        break;
                    default:
                        Console.WriteLine("輸入錯月份");
                        break;
                }
            
            }
            Console.Read();
        }
    }
}

第四題
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Homework1_4
{
    class part4
    {
        static void Main(string[] args)
        {
            int year, month, day,week;
            string input;

            Console.WriteLine("請輸入年份");
            input = Console.ReadLine();
            year = int.Parse(input);
            Console.WriteLine("請輸入月份");
            input = Console.ReadLine();
            month = int.Parse(input);
            Console.WriteLine("請輸入日期");
            input = Console.ReadLine();
            day = int.Parse(input);
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
            {
                if (((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day <= 31) || ((month == 4 || month == 6 || month == 9 || month == 11) && day <= 31) || (month == 2 && day <= 29))
                {
                    Console.WriteLine("{0}年{1}月{2}日正確", year, month, day);

                    if (month == 1 || month == 2) {
                        year--;
                        month += 12;
                    }
                    week = (day +1+ 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
                    switch (week) { 
                        case 0:
                            Console.WriteLine("為星期日");
                            break;
                        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;

                    
                    }
                
                }
            
                else
                {
                    Console.WriteLine("{0}年{1}月{2}日錯誤請重新輸入", year, month, day);
                }
            }
            
            else
            {
                if (((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day <= 31) || (month == 2 && day <= 28) || ((month == 4 || month == 6 || month == 9 || month == 11) && day <= 30) || (month == 2 && day <= 29))
                {
                 
                    Console.WriteLine("{0}年{1}月{2}日正確", year, month, day);
                if (month == 1 || month == 2) {
                        year--;
                        month += 12;
                    }
                    week = (day+1 + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
                    switch (week) { 
                        case 0:
                            Console.WriteLine("為星期日");
                            break;
                        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;
                    }
                
                }
                else
                {
                    Console.WriteLine("{0}年{1}月{2}日錯誤請重新輸入", year, month, day);
                }

            }
            Console.Read();

            }
        
        }
    }


第五題
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1_5
{
    class Program
    {
        static void Main(string[] args)
        {
            //問題5
            int year;
            int month;
            int day;
            Console.WriteLine("5.\n輸入西元年:");
            year = int.Parse(Console.ReadLine());
            Console.WriteLine("輸入月份:");
            month = int.Parse(Console.ReadLine());
            Console.WriteLine("輸入日期");
            day = int.Parse(Console.ReadLine());

            if (month > 12 || month < 1||day<1)
                Console.WriteLine("false");
            else
            {
                switch (month)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        if (day > 31 )
                        {
                            Console.WriteLine("false");
                        }
                        else
                        {
                            Console.WriteLine("true");
                        }
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        if (day > 30 || day < 1)
                        {
                            Console.WriteLine("false");
                        }
                        else
                        {
                            Console.WriteLine("true");
                        }
                        break;
                    case 2:
                        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                        {
                            if (day > 29 || day < 1)
                            {
                                Console.WriteLine("false");
                            }
                            else
                            {
                                Console.WriteLine("true");
                            }
                        }
                        else
                        {
                            if (day > 28 || day < 1)
                            {
                                Console.WriteLine("false");
                            }
                            else
                            {
                                Console.WriteLine("true");
                            }
                        }
                        break;
                }
            }

            Console.Read();
        }
    }
}

第六題
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1_6
{
    class Program
    {
        static void Main(string[] args)
        {
            //問題6
            int year2;
            int month2;
            int day2;

            int year1;
            int month1;
            int day1;

            int timeInterval1 = 0;
            int timeInterval2 = 0;

            ///
            /// 輸入結束的時間
            /// 

            Console.WriteLine("6.\n輸入結束時間:");
            Console.WriteLine("輸入年份");
            year2 = int.Parse(Console.ReadLine());

            Console.WriteLine("輸入月份");
            month2 = int.Parse(Console.ReadLine());

            Console.WriteLine("輸入日期");
            day2 = int.Parse(Console.ReadLine());
            Console.WriteLine();

            ///
            /// 輸入開始的時間
            /// 

            Console.WriteLine("輸入開始時間:");
            Console.WriteLine("輸入年份");
            year1 = int.Parse(Console.ReadLine());

            Console.WriteLine("輸入月份");
            month1 = int.Parse(Console.ReadLine());

            Console.WriteLine("輸入日期");
            day1 = int.Parse(Console.ReadLine());

            ///
            /// 計算結束的時間和西元前1年12月31日的時間差
            ///

            timeInterval2 = (year2 - 1) * 365 + (year2 - 1) / 4 - (year2 - 1) / 100 + (year2 - 1) / 400;
            switch (month2)
            {
                case 1:
                    timeInterval2 = timeInterval2 + day2;
                    break;
                case 2:
                    timeInterval2 = timeInterval2 + 31 + day2;
                    break;
                case 3:
                    timeInterval2 = timeInterval2 + 31 + 28 + day2;
                    break;
                case 4:
                    timeInterval2 = timeInterval2 + 31 * 2 + 28 + day2;
                    break;
                case 5:
                    timeInterval2 = timeInterval2 + 31 * 2 + 28 + 30 + day2;
                    break;
                case 6:
                    timeInterval2 = timeInterval2 + 31 * 3 + 28 + 30 + day2;
                    break;
                case 7:
                    timeInterval2 = timeInterval2 + 31 * 3 + 28 + 30 * 2 + day2;
                    break;
                case 8:
                    timeInterval2 = timeInterval2 + 31 * 4 + 28 + 30 * 2 + day2;
                    break;
                case 9:
                    timeInterval2 = timeInterval2 + 31 * 5 + 28 + 30 * 2 + day2;
                    break;
                case 10:
                    timeInterval2 = timeInterval2 + 31 * 5 + 28 + 30 * 3 + day2;
                    break;
                case 11:
                    timeInterval2 = timeInterval2 + 31 * 6 + 28 + 30 * 3 + day2;
                    break;
                case 12:
                    timeInterval2 = timeInterval2 + 31 * 6 + 28 + 30 * 4 + day2;
                    break;
            }
            if ((year2 % 4 == 0 && year2 % 100 != 0 || year2 % 400 == 0) && month2 > 2)
                timeInterval2 = timeInterval2 + 1;

            ///
            /// 計算開始的時間和西元前1年12月31日的時間差
            ///

            timeInterval1 = (year1 - 1) * 365 + (year1 - 1) / 4 - (year1 - 1) / 100 + (year1 - 1) / 400;
            switch (month1)
            {
                case 1:
                    timeInterval1 = timeInterval1 + day1;
                    break;
                case 2:
                    timeInterval1 = timeInterval1 + 31 + day1;
                    break;
                case 3:
                    timeInterval1 = timeInterval1 + 31 + 28 + day1;
                    break;
                case 4:
                    timeInterval1 = timeInterval1 + 31 * 2 + 28 + day1;
                    break;
                case 5:
                    timeInterval1 = timeInterval1 + 31 * 2 + 28 + 30 + day1;
                    break;
                case 6:
                    timeInterval1 = timeInterval1 + 31 * 3 + 28 + 30 + day1;
                    break;
                case 7:
                    timeInterval1 = timeInterval1 + 31 * 3 + 28 + 30 * 2 + day1;
                    break;
                case 8:
                    timeInterval1 = timeInterval1 + 31 * 4 + 28 + 30 * 2 + day1;
                    break;
                case 9:
                    timeInterval1 = timeInterval1 + 31 * 5 + 28 + 30 * 2 + day1;
                    break;
                case 10:
                    timeInterval1 = timeInterval1 + 31 * 5 + 28 + 30 * 3 + day1;
                    break;
                case 11:
                    timeInterval1 = timeInterval1 + 31 * 6 + 28 + 30 * 3 + day1;
                    break;
                case 12:
                    timeInterval1 = timeInterval1 + 31 * 6 + 28 + 30 * 4 + day1;
                    break;
            }
            if ((year1 % 4 == 0 && year1 % 100 != 0 || year1 % 400 == 0) && month1 > 2)
                timeInterval1 = timeInterval1 + 1;

            ///
            /// 印出所求的答案
            ///

            Console.WriteLine("開始日期和結束日期之間有{0}天", timeInterval2 - timeInterval1 - 1);

            Console.Read();
        }
    }
}

3 則留言:

  1. 第三題

    input = Console.ReadLine();
    year = int.Parse(input);
    改成
    year = int.Parse(Console.ReadLine());
    可以省去一個變數的宣告

    回覆刪除
  2. 第五題
    > 31 & < 1 return false
    可以拿到 外層來判斷 (switch case 外)

    回覆刪除
    回覆
    1. 修正:
      < 1 return false
      可以拿到 外層來判斷 (switch case 外)

      刪除