2015年3月19日 星期四

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



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

namespace Homework1_1
{
    class Homework1_1
    {
        static void Main(string[] args)
        {
            int temp;
            int sum;
            for (int i = 100; i < 1000; i++)
            {
                sum = 0;
                temp = i;
                while (temp != 0)
                {
                    sum += (temp % 10) * (temp % 10) * (temp % 10);
                    temp = temp / 10;
                }
                if (sum == i)
                    Console.WriteLine("{0}",i);
            }
            Console.ReadLine();
        }
    }
}
Homework1_2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1_2
{
    class Homework1_2
    {
        static void Main(string[] args)
        {
            while(true)
            {
                Console.WriteLine("欲離開請輸入0");
                Console.WriteLine("請輸入年份");
                int year = int.Parse(Console.ReadLine());
                if (year == 0)
                {
                    break;
                }
                else if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                {
                    Console.WriteLine("閏年");
                }
                else
                {
                    Console.WriteLine("平年");
                }
                Console.ReadKey();
                Console.Clear();
            }
        }
    }
}
Homework1_3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1_3
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1_3
{
    class Homework1_3
    {
        static void Main(string[] args)
        {
            Console.WriteLine("欲離開請輸入0");
            while (true)
            {
                Console.WriteLine("請輸入年");
                int year = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入月");
                int month = int.Parse(Console.ReadLine());
                if (year <= 0 || month <= 0 || month > 12)
                {
                    break;
                }
                switch (month)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        Console.WriteLine("31天");
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        Console.WriteLine("30天");
                        break;
                    case 2:
                        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                        {
                            Console.WriteLine("29天");
                        }
                        else
                        {
                            Console.WriteLine("28天");
                        }
                        break;
                    default:
                        break;
                }
                Console.ReadKey();
                Console.Clear();
            }
        }
    }
}

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

namespace Homework1_4
{
    class Homework1_4
    {
        static void Main(string[] args)
        {
            Console.WriteLine("欲離開請輸入0");
            while (true)
            {
                Console.WriteLine("請輸入年");
                int year = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入月");
                int month = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入日");
                int date = int.Parse(Console.ReadLine());
                if (year <= 0 || month <= 0 || date <= 0 || month > 12)
                {
                    break;
                }
                if (month == 1 || month == 2)                         //利用基姆拉爾森公式,1.2月當作去年的13.14月
                {
                    year -= 1;
                    month += 12;
                }
                int day = (year + year / 4 + year / 400 - year / 100 + 2*month + 3*(month+1)/5 + date + 1) % 7;
                switch (day)
                {
                    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;
                }
                Console.ReadKey();
                Console.Clear();
            }
        }
    }
}
Homework1_5
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1_5
{
    class Homework1_5
    {
        static void Main(string[] args)
        {
            Console.WriteLine("欲離開請輸入0");
            while (true)
            {
                Console.WriteLine("請輸入年");
                int year = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入月");
                int month = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入日");
                int date = int.Parse(Console.ReadLine());
                if (year <= 0 || month <= 0 || date <= 0 || month > 12)
                {
                    Console.WriteLine("false");
                    break;
                }
                switch (month)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        if (date > 0 && date <= 31)
                        {
                            Console.WriteLine("true");
                        }
                        else
                        {
                            Console.WriteLine("false");
                        }
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        if (date > 0 && date <= 30)
                        {
                            Console.WriteLine("true");
                        }
                        else
                        {
                            Console.WriteLine("false");
                        }
                        break;
                    case 2:
                        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                        {
                            if (date > 0 && date <= 29)
                            {
                                Console.WriteLine("true");
                            }
                            else
                            {
                                Console.WriteLine("false");
                            }
                        }
                        else
                        {
                            if (date > 0 && date <= 28)
                            {
                                Console.WriteLine("true");
                            }
                            else
                            {
                                Console.WriteLine("false");
                            }
                        }
                        break;
                    default:
                        break;
                }
                Console.ReadKey();
                Console.Clear();
            }
        }
    }
}
Homework1_6
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1_6
{
    class Homework1_6
    {
        static void Main(string[] args)
        {
            int day;
            int dayEnd;
            while(true)
            {
                Console.WriteLine("請輸入起始年");
                int year = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入起始月");
                int month = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入起始日");
                int date = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入結束年");
                int yearEnd = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入結束月");
                int monthEnd = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入結束日");
                int dateEnd = int.Parse(Console.ReadLine());
                if (year <= 0 || month <= 0 || date <= 0 || yearEnd <= 0 || monthEnd <= 0 || dateEnd <= 0)
                {
                    break;
                }
                day = 0;           
                dayEnd=0;         
                for (int i = 1; i <= month; i++)         //計算日期為起始年第幾天
                {
                    if (i == month)
                    {
                        day += date;
                        break;
                    }
                    if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
                        day += 31;
                    else if (i == 4 || i == 6 || i == 9 || i == 11)
                        day += 30;
                    else
                    {
                        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                            day += 29;
                        else
                            day += 28;
                    }
                }

                for (int i = 1; i <= monthEnd; i++)    //計算日期為結束年第幾天
                {
                    if (i == monthEnd)
                    {
                        dayEnd += dateEnd;
                        break;
                    }
                    if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
                        dayEnd += 31;
                    else if (i == 4 || i == 6 || i == 9 || i == 11)
                        dayEnd += 30;
                    else
                    {
                        if (yearEnd % 400 == 0 || (yearEnd % 4 == 0 && yearEnd % 100 != 0))
                            dayEnd += 29;
                        else
                            dayEnd += 28;
                    }
                }
                if(year==yearEnd)                      //若年份相同 則天數相減
                {
                    Console.WriteLine("之間有{0}天",dayEnd-day-1);
                }
                if(year!=yearEnd)                      //若年份不同 加上之間的年天數
                {
                    for (int i = year; i < yearEnd; i++)
                    {
                        if (i % 400 == 0 || (i % 4 == 0 && i % 100 != 0))
                            dayEnd += 366;
                        else
                            dayEnd += 365;
                    }
                    Console.WriteLine("之間有{0}天", dayEnd - day - 1);
                }
                Console.ReadKey();
                Console.Clear();
            }
        }
    }
}

3 則留言:

  1. Homework1_2
    string input = Console.ReadLine();
    int year = int.Parse(input);
    改成
    int year = int.Parse(Console.ReadLine());

    回覆刪除

  2. 結束年: year_end ==> yearEnd (其實 endYear 比較好)
    開始年 year ==> startYear

    其他變數也應比照這種精神修改

    回覆刪除