using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SeqListSort { /// <summary> /// <ather> /// lihonlin /// </ather> /// <content> /// 搬桌子问题 /// 某教学大楼一层有n个教室,从左到右依次编号为1、2、…、n。现在要把一些课桌从某些教室搬到 /// 另外一些教室,每张桌子都是从编号较小的教室搬到编号较大的教室,每一趟,都是从左到右走, /// 搬完一张课桌后,可以继续从当前位置或往右走搬另一张桌子。输入数据:先输入n、m,然后紧接 /// 着m行输入这m张要搬课桌的起始教室和目标教室。输出数据:最少需要跑几趟。 /// Sample Input /// 10 5 /// 1 3 /// 3 9 /// 4 6 /// 6 10 /// 7 8 /// Sample Output /// 3 /// </content> /// </summary> public struct Room { public int start; public int end; } class Greedy_Desk { const int size = 20; static int m; static int n; static Room[] room = new Room[size]; static bool[] isMove = new bool[size]; static int min = 0; static void InitData() { Console.WriteLine("请输入n个教室"); n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入m行课桌"); m = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("输入这m张要搬课桌的起始教室和目标教室"); for (int i = 0; i < m; ++i) { room[i].start = Convert.ToInt32(Console.ReadLine()); room[i].end = Convert.ToInt32(Console.ReadLine()); } } static void Sort() { for (int i = 0; i < m; ++i) { for (int j = 0; j < m - i - 1; ++j ) { if (room[j].start > room[j + 1].start) { int temp = room[j].start; room[j].start = room[j + 1].start; room[j + 1].start = temp; } } } } public static void MoveDesk() { InitData(); Sort(); int num = 0; while ( num < m ) { int temp = 0; for (int i = 0; i < m; ++i ) { if (temp <= room[i].start && !isMove[i]) { temp = room[i].end; isMove[i] = true; num++; } } min++; } Console.WriteLine("最少需要跑{0}趟", min); } } }
时间: 2024-10-18 17:54:39