2021年4月16日 星期五

The Blocks Problem + Q11038: How many 0's?

The Blocks Problem


在早期人工智慧的領域中常常會用到機器人,在這個問題中有一支機器手臂接受指令來搬動積木,而你的任務就是輸出最後積木的情形。
一開始在一平坦的桌面上有n塊積木(編號從0n-10號積木放在0號位置上,1號積木放在1號位置上,依此類推。
機器手臂有以下幾種合法搬積木的方式(ab是積木的編號):
move a onto b
在將a搬到b上之前,先將ab上的積木放回原來的位置(例如:1就放回1的最開始位罝)
move a over b
在將a搬到b所在的那堆積木之上之前,先將a上的積木放回原來的位罝(b所在的那堆積木不動)
pile a onto b
a本身和其上的積木一起放到b上,在搬之前b上方的積木放回原位
pile a over b
a本身和其上的積木一起搬到到b所在的那堆積木之上
quit
動作結束
前四個動作中若a=b,或者a, b在同一堆積木中,那麼這樣的動作算是不合法的。所有不合法的動作應該被忽略,也就是對各積木均無改變。
Input
輸入含有多組測試資料,每組測試資料的第一列有一個正整數n0 < n < 25),代表積木的數目(編號從0n-1)。接下來為機器手臂的動作,每個動作一列。如果此動作為 quit ,代表此組測試資料輸入結束。你可以假設所有的動作都符合上述的樣式。請參考Sample Input
Output
每組測試資料輸出桌面上各位置積木的情形(每個位置一列,也就是共有n列),格式請參考Sample Output
Sample Input
10
move 9 onto 1
move 8 over 1
move 7 over 1
move 6 over 1
pile 8 over 6
pile 8 over 5
move 2 over 1
move 4 over 9
quit
4
pile 0 over 1
pile 2 over 3
move 1 onto 3
quit
Sample Output
0: 0
1: 1 9 2 4
2:
3: 3
4:
5: 5 8 7 6
6:
7:
8:
9:
0: 0
1:
2: 2
3: 3 1

範例
static void Main(string[] args)
        {
            string[] s = new string[] { "ldqwdl""wekfekw""jfiwej""kwefok" };
            List<string> ls = new List<string>(s);           
            Console.WriteLine(ls.Count);
            Console.WriteLine(ls.Contains("wf"));
            ls.Add("wfeef");
            ls.AddRange(s);
            ls.Insert(2, "wqdq");
            ls.InsertRange(4, s);
            ls.Remove("wqd");
            ls.RemoveRange(2,2);           
            string[] Sr = ls.GetRange(2, 4).ToArray();
            ls.Sort();
            ls.Find(i => i == "dd");
            ls.IndexOf("kjk");
            ls.Clear();
            ls.TrimExcess();
            Console.WriteLine(ls.Capacity);
        }

成品

    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.Write("請輸入需要幾塊積木: ");
                List<string> Data = new List<string>();
                int N = Convert.ToInt32(Console.ReadLine());
                for (int i = 0; i < N; i++)
                    Data.Add(i.ToString());
                while (true)
                {
                    string Order = Console.ReadLine();
                    if (Order == "quit")
                    {
                        Console.WriteLine("最終積木位置如下");
                        int n = 0;
                        foreach (string Stack in Data)
                        {
                            Console.Write("{0}: ",n);
                            foreach (char unit in Stack.ToCharArray())
                                Console.Write("{0} ", unit);
                            n++;
                            Console.WriteLine();
                        }
                        Data.Clear();
                        Data.TrimExcess();
                        break;
                    }
                    else
                    {
                        string[] value = Order.Split(' ');
                        string BeforeTemp = "", AfterTemp = "";
                        int BeforeX = 0, BeforeY = 0, AfterX = 0, AfterY = 0;
                        foreach (string Stack in Data)
                        {                           
                            List<char> unit = new List<char>(Stack.ToCharArray());
                            // 如果Data[n]找到value[1]跳出
                            if (unit.Contains(Convert.ToChar(value[1])))
                            {
                                BeforeY = unit.IndexOf(Convert.ToChar(value[1]));
                                BeforeTemp = Data[BeforeX];
                                break;
                            }
                            BeforeX++;
                        }
                        char[] BT_Ch = BeforeTemp.ToCharArray();
                        Data[BeforeX] = BeforeTemp = "";
                        for (int i = 0; i < BT_Ch.Length; i++)
                        {
                            if (i < BeforeY) Data[BeforeX] += BT_Ch[i];
                            else BeforeTemp += BT_Ch[i];
                        }
                        foreach (string Stack in Data)
                        {
                            List<char> unit = new List<char>(Stack.ToCharArray());
                            // 如果Data[n]找到value[3]跳出
                            if (unit.Contains(Convert.ToChar(value[3])))
                            {
                                AfterY = unit.IndexOf(Convert.ToChar(value[3]));
                                AfterTemp = Data[AfterX];
                                break;
                            }
                            AfterX++;
                        }
                         if (AfterX == Data.Count)
                        {
                            Console.WriteLine("操作錯誤");
                            Data[BeforeX] += BeforeTemp;
                        }               
                        else
                        {
                            char[] AT_Ch = AfterTemp.ToCharArray();
                            Data[AfterX] = AfterTemp = "";
                            for (int i = 0; i < AT_Ch.Length; i++)
                            {
                                if (i < AfterY) Data[AfterX] += AT_Ch[i];
                                else AfterTemp += AT_Ch[i];
                            }
                            switch (value[0])
                            {
                                case "move":
                                    char[] BT_ChMove = BeforeTemp.ToCharArray();
                                    for (int i = 1; i < BT_ChMove.Length; i++)
                                        Data[Convert.ToInt32(BT_ChMove[i].ToString())] += BT_ChMove[i];
                                    BeforeTemp = BT_ChMove[0].ToString();
                                    break;
                                case "pile":
                                    break;
                                default:
                                    Console.WriteLine("指令錯誤");
                                    break;
                            }
                            switch (value[2])
                            {
                                case "onto":
                                    char[] AT_ChOnto = AfterTemp.ToCharArray();
                                    for (int i = 1; i < AT_ChOnto.Length; i++)
                                        Data[Convert.ToInt32(AT_ChOnto[i].ToString())] += AT_ChOnto[i].ToString();
                                    AfterTemp = AT_ChOnto[0].ToString();
                                    Data[AfterX] += AfterTemp + BeforeTemp;
                                    break;
                                case "over":
                                    Data[AfterX] += AfterTemp + BeforeTemp;
                                    break;
                                default:
                                    Console.WriteLine("指令錯誤");
                                    break;
                            }
                        }                      
                    }
                }
                Console.WriteLine("\n\n");
            }
        }
    }

Q11038: How many 0's?

一位僧侶用十進位寫下了 m n 之間所有的整數,0 m n,他總共需要寫下多少個「0」?

Input

每組測試資料一列,每列有兩個無號32-bit整數 m n,且mn
一列含兩個負數代表輸入結束。

Output

對於每一組測試資料,輸出此代表僧侶需寫下多少個 0

Sample Input

10 11

100 200

0 500

1234567890 2345678901

0 4294967295

-1 -1

Sample Output

1

22

92

987654304

3825876150

            while (true)
            {
                string[] Input = Console.ReadLine().Split(' ');
                double Answer = 0, a = Convert.ToDouble(Input[0])
                    , b = Convert.ToDouble(Input[1]);
                if (a < 0 && b < 0) return;
                for (double i = a; i <= b; i++)
                {
                    foreach (var item in i.ToString().ToCharArray())
                        if (item == '0') Answer++;
                }
                Console.WriteLine(Answer);
            }


沒有留言:

張貼留言

WinFormTb02

https://drive.google.com/drive/u/0/folders/1UwS9FZ3ELCOK6SAwirHrkxq3z_RSbxJt https://www.youtube.com/watch?v=k7IkIeww_U0&list=PLumjEWemD...