C#撰寫[ 門禁卡系統-任意輸入通行時段,最後輸出可通行的三個時段 ] 演算法

C#撰寫[ 門禁卡系統-任意輸入通行時段,最後輸出可通行的三個時段 ] 演算法

C#撰寫[ 門禁卡系統-任意輸入通行時段,最後輸出可通行的三個時段 ] 演算法

 

 

PS 寫出這一個程式,我確定自己打死不寫韌體,因為如果不用這一招浪費記憶體的方法,我寫不出來其他的解法

 

條件規定:

01.任意輸入通行時段,最後輸出可通行的三個時段

02.要能具有疊加重複和取最大的設定

03.計算出可通行的三個時段,時段1和時段2就直接取出,時段3就取出剩餘設定最大範圍

 

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace CS_Console_Passage_time

{

    //任意輸入通行時段,最後輸出可通行的三個時段

    //要能具有疊加重複和取最大的設定

    //計算出可通行的三個時段,時段1和時段2就直接取出,時段3就取出剩餘設定最大範圍

 

    /*測試驗用程式碼

    class Program

    {

        static void Pause()

        {

            Console.Write(“Press any key to continue . . . “);

            Console.ReadKey(true);

        }

        static void Main(string[] args)

        {

            Passage_Time.init();//所有變數清空

            Passage_Time.setValue((0* 60+ 0), (7 * 60 + 10));//00:00~07:10

            Passage_Time.setValue((8* 60+ 10), (14 * 60 + 55));//08:10~14:55

            //Passage_Time.setValue((14 * 60 + 00), (14 * 60 + 55));//14:00~14:55

            //Passage_Time.setValue((14 * 60 + 00), (15 * 60 + 55));//14:00~15:55

            Passage_Time.setValue((20 * 60 + 30), (21 * 60 + 00));//20:30~21:00

            Passage_Time.setValue((22 * 60 + 30), (23 * 60 + 59));//22:30~23:59

            Passage_Time.calculateValue();

 

            String StrOutput = “”;

 

            String StrStart1 = String.Format(“{0:00}:{1:00}”, Passage_Time.m_Start1 / 60, Passage_Time.m_Start1 % 60);

            String StrEnd1 = String.Format(“{0:00}:{1:00}”, Passage_Time.m_End1 / 60, Passage_Time.m_End1 % 60);

            StrOutput=String.Format(“第一組:{0}~{1}”,StrStart1,StrEnd1);

            Console.WriteLine(StrOutput);

 

            String StrStart2 = String.Format(“{0:00}:{1:00}”, Passage_Time.m_Start2 / 60, Passage_Time.m_Start2 % 60);

            String StrEnd2 = String.Format(“{0:00}:{1:00}”, Passage_Time.m_End2 / 60, Passage_Time.m_End2 % 60);

            StrOutput = String.Format(“第二組:{0}~{1}”, StrStart2, StrEnd2);

            Console.WriteLine(StrOutput);

 

            String StrStart3 = String.Format(“{0:00}:{1:00}”, Passage_Time.m_Start3 / 60, Passage_Time.m_Start3 % 60);

            String StrEnd3 = String.Format(“{0:00}:{1:00}”, Passage_Time.m_End3 / 60, Passage_Time.m_End3 % 60);

            StrOutput = String.Format(“第三組:{0}~{1}”, StrStart3, StrEnd3);

            Console.WriteLine(StrOutput);

            Pause();

        }

    }

    */

    class Passage_Time

    {

        public static bool[] m_Minute = new bool[60 * 24];//紀錄一天所有分鐘數的狀態記憶體

        public static int m_Start1, m_End1;//儲存第一組通行時間

        public static int m_Start2, m_End2;//儲存第二組通行時間

        public static int m_Start3, m_End3;//儲存第三組通行時間

        public static void init()//先把所有變數清空的函數

        {

            for (int i = 0; i < (60 * 24); i++)

            {

                m_Minute[i] = false;

            }

            m_Start1 = -1; m_End1 = -1;

            m_Start2 =- 1; m_End2 = -1;

            m_Start3 = -1; m_End3 = -1;

        }

        public static void setValue(int start, int end)//填入想要設定時段

        {

            if (end < (60 * 24))//防止白癡不知一天24小時,一小時是60分鐘

            {

                for (int i = start; i <= end; i++)

                {

                    m_Minute[i] = true;

                }

            }

        }

        public static void calculateValue()//計算出可通行的三個時段,時段1和時段2就直接取出,時段3就取出剩餘設定最大範圍

        {

            int state=0;

            for (int i = 0; i < (60 * 24); i++)

            {

                switch (state)

                {

                case 0:

                    if (m_Minute[i] == true)

                    {

                        m_Start1 = i;

                        state++;//1

                    }

                    break;

                case 1:

                    if (m_Minute[i] == false)

                    {

                        m_End1 = (i – 1);

                        state++;//2

                    }

                    break;

                case 2:

                    if (m_Minute[i] == true)

                    {

                        m_Start2 = i;

                        state++;//3

                    }

                    break;

                case 3:

                    if (m_Minute[i] == false)

                    {

                        m_End2 = (i – 1);

                        state++;

                    }

                    break;

                case 4:

                    if (m_Minute[i] == true)

                    {

                        m_Start3 = i;

                        state++;//4

                    }

                    break;

                case 5:

                    if (m_Minute[i] == true)

                    {

                        m_End3 = i;

                    }

                    break;

                }

 

                //—

                //防呆用

                if (m_Start1 < 0)

                {

                    m_Start1 = 0;

                }

                if (m_Start2 < 0)

                {

                    m_Start2 = 0;

                }

                if (m_Start3 < 0)

                {

                    m_Start3 = 0;

                }

                if (m_End1 < 0)

                {

                    m_End1 = 0;

                }

                if (m_End2 < 0)

                {

                    m_End2 = 0;

                }

                if (m_End3 < 0)

                {

                    m_End3 = 0;

                }

                //–

            }

        }

    }

}

 

 

 

 

 

 

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *