/***********************爲進程創建的類Process.cs*************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 我們的實驗 { public class Process { /********定義每個進程所擁有的屬性********/ public string name; //進程名 public int arrive_time; //到達時間 public int need_time; //估計所需執行時間 public int wait_time; //等待時間 public int need_memery; //所需內存大小 public int need_printer; //所需打印機數量 public int more_time; //還需要執行的時間 public int state; //表示執行爲0、就緒爲1、被阻塞等待爲2、完成爲3 public int rank ; //處於哪個就緒隊列1,2,3 public Process() { } public Process(string namePro,int arrive_timePro,int need_timePro, int need_memeryPro,int need_printerPro,int more_timePro) { this.name = namePro; this.arrive_time = arrive_timePro; this.need_time = need_timePro; this.need_memery = need_memeryPro; this.need_printer = need_printerPro; this.more_time = more_timePro; } } } /***********************類Form1.cs*************************/ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 我們的實驗 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button4_Click(object sender, EventArgs e) //退出 { this.Close(); } static int totalMem =0; static int totalPrinter=0; //剩餘的共有的資源數量 private void initial_set_Click(object sender, EventArgs e) //初始化設置 { Form2 form2 = new Form2(this.text1, this.text2); try { form2.ShowDialog(this); } finally { form2.Dispose();} totalMem = int.Parse(this.text1.Text); totalPrinter = int.Parse(this.text2.Text); } public void newPro(ListView L) //新建進程進入就緒隊列1中 { ListViewItem List = new ListViewItem(pro.name); List.SubItems.Add(pro.arrive_time.ToString()); List.SubItems.Add(pro.need_time.ToString()); List.SubItems.Add(pro.need_memery.ToString()); List.SubItems.Add(pro.need_printer.ToString()); List.SubItems.Add(pro.more_time.ToString()); L.Items.Add(List); new_pro_name.Text = ""; in_time.Text = ""; need_t.Text = ""; req_mem.Text = ""; req_printer.Text = ""; } public void Transfer(Process p) //時間片到了但沒有執行完,進入就緒隊列2 { ListViewItem item = new ListViewItem(p.name); item.SubItems.Add(p.arrive_time.ToString()); item.SubItems.Add(p.need_time.ToString()); item.SubItems.Add(p.need_memery.ToString()); item.SubItems.Add(p.need_printer.ToString()); item.SubItems.Add(p.more_time.ToString()); listView5.Items.Add(item); } public void finishPro(Process f) //進程執行完後進入完成隊列 { ListViewItem item = new ListViewItem(f.name); item.SubItems.Add(f.arrive_time.ToString()); item.SubItems.Add(f.need_time.ToString()); item.SubItems.Add(f.need_memery.ToString()); item.SubItems.Add(f.need_printer.ToString()); item.SubItems.Add(f.more_time.ToString()); listView3.Items.Add(item); } public void occupyCPU() //正在佔用CPU的進程 { if (listView7.Items.Count != 0) listView7.Items.Remove(listView7.Items[0]); ListViewItem item = new ListViewItem(process.name); item.SubItems.Add(process.arrive_time.ToString()); item.SubItems.Add(process.need_time.ToString()); item.SubItems.Add(process.need_memery.ToString()); item.SubItems.Add(process.need_printer.ToString()); item.SubItems.Add(process.more_time.ToString()); listView7.Items.Add(item); } public void wakePro(Process w) //被喚醒的進程進入就緒隊列3中 { ListViewItem item = new ListViewItem(w.name); item.SubItems.Add(w.arrive_time.ToString()); item.SubItems.Add(w.need_time.ToString()); item.SubItems.Add(w.need_memery.ToString()); item.SubItems.Add(w.need_printer.ToString()); item.SubItems.Add(w.more_time.ToString()); listView6.Items.Add(item); } public void blockPro(Process b) //被阻塞的進程進入等待隊列 { ListViewItem item = new ListViewItem(b.name); item.SubItems.Add(b.arrive_time.ToString()); item.SubItems.Add(b.need_time.ToString()); item.SubItems.Add(b.need_memery.ToString()); item.SubItems.Add(b.need_printer.ToString()); item.SubItems.Add(b.more_time.ToString()); listView2.Items.Add(item); } Process pro; private void new_job_Click(object sender, EventArgs e) //新建進程 { try { if (new_pro_name.Text != null && in_time.Text != null) { pro = new Process(new_pro_name.Text, int.Parse(in_time.Text), int.Parse(need_t.Text), int.Parse(req_mem.Text), int.Parse(req_printer.Text), int.Parse(need_t.Text)); if ((int.Parse(req_printer.Text) > totalPrinter) || (int.Parse(req_mem.Text) > totalMem)) { MessageBox.Show("內存和打印機資源不夠", "創建進程失敗", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { newPro(listView1); newPro(listView4); totalMem = totalMem - pro.need_memery; totalPrinter = totalPrinter - pro.need_printer; return; } } else { MessageBox.Show("輸入進程名", "創建進程失敗", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } catch { MessageBox.Show(""); } } private void button3_Click(object sender, EventArgs e) //exit { this.Close(); } Process process = new Process(null, 0, 0, 0, 0, 0); public void runPro() //多級反饋輪轉調度算法 { int ready =0; if (listView1.Items.Count != 0) { ready = 1; process.rank = 1; process.state = 1; process.name = listView1.Items[0].SubItems[0].Text; process.arrive_time = int.Parse(listView1.Items[0].SubItems[1].Text); process.need_time = int.Parse(listView1.Items[0].SubItems[2].Text); process.need_memery = int.Parse(listView1.Items[0].SubItems[3].Text); process.need_printer = int.Parse(listView1.Items[0].SubItems[4].Text); process.more_time = int.Parse(listView1.Items[0].SubItems[5].Text); listView1.Items.Remove(listView1.Items[0]); } else if (listView5.Items.Count != 0) { ready = 2; process.rank = 2; process.state = 1; process.name = listView5.Items[0].SubItems[0].Text; process.arrive_time = int.Parse(listView5.Items[0].SubItems[1].Text); process.need_time = int.Parse(listView5.Items[0].SubItems[2].Text); process.need_memery = int.Parse(listView5.Items[0].SubItems[3].Text); process.need_printer = int.Parse(listView5.Items[0].SubItems[4].Text); process.more_time = int.Parse(listView5.Items[0].SubItems[5].Text); listView5.Items.Remove(listView5.Items[0]); } else if (listView6.Items.Count != 0) { ready = 3; process.rank = 3; process.state = 1; process.name = listView6.Items[0].SubItems[0].Text; process.arrive_time = int.Parse(listView6.Items[0].SubItems[1].Text); process.need_time = int.Parse(listView6.Items[0].SubItems[2].Text); process.need_memery = int.Parse(listView6.Items[0].SubItems[3].Text); process.need_printer = int.Parse(listView6.Items[0].SubItems[4].Text); process.more_time = int.Parse(listView6.Items[0].SubItems[5].Text); listView6.Items.Remove(listView6.Items[0]); } if (ready == 0) { process.name = ""; process.arrive_time = 0; process.need_time = 0; process.need_memery = 0; process.need_printer = 0; process.more_time = 0; } else process.state = 0; if (listView7.Items.Count != 0) /*開始執行,隊列1中的進程進入執行隊列*/ listView7.Items.Remove(listView7.Items[0]); ListViewItem item = new ListViewItem(process.name); item.SubItems.Add(process.arrive_time.ToString()); item.SubItems.Add(process.need_time.ToString()); item.SubItems.Add(process.need_memery.ToString()); item.SubItems.Add(process.need_printer.ToString()); listView7.Items.Add(item); int c = ready; /******************爲三個就緒隊列分別分配各自執行的時間片****************/ if (ready == 1) //就緒隊列1爲新建的進程 timeSlice = 2; else if (ready == 2) //就緒隊列2爲時間片到了但沒有執行完的進程 timeSlice = 3; else if (ready == 3) //就緒隊列3是從等待隊列中被喚醒的進程 timeSlice = 4; else { timeSlice = 0; timer1.Enabled = false; MessageBox.Show("所有就緒隊列中進程執行結束", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); } } public void runFCFS() //先進先服務調度算法 { if (listView1.Items.Count != 0) { process.name = listView1.Items[0].SubItems[0].Text; process.arrive_time = int.Parse(listView1.Items[0].SubItems[1].Text); process.need_time = int.Parse(listView1.Items[0].SubItems[2].Text); process.need_memery = int.Parse(listView1.Items[0].SubItems[3].Text); process.need_printer = int.Parse(listView1.Items[0].SubItems[4].Text); process.more_time = int.Parse(listView1.Items[0].SubItems[5].Text); listView1.Items.Remove(listView1.Items[0]); } else { MessageBox.Show("沒有就緒狀態的進程", "消息", MessageBoxButtons.OK,MessageBoxIcon.Information); } } int timeSlice=0; bool flag=true ; int count = 0; private void timer1_Tick(object sender, EventArgs e) //時間片的控制 { if (flag) { process.more_time--; count++; } if (process.more_time == 0) { flag = false; process.state = 3; finishPro(process); } else if (count ==timeSlice) { flag = false; Transfer(process); } occupyCPU(); if (flag == false && process.name!= null) { runPro(); count = 0; flag = true; } if (process.name== null) MessageBox.Show("全部就緒隊列已經執行完畢!!!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void start_Click_1(object sender, EventArgs e) //開始模擬 { if (radioButton1.Checked) { runPro(); occupyCPU(); timer1.Enabled = true; } else if (radioButton2.Checked) { runFCFS(); occupyCPU(); timer1.Enabled = true; } else { MessageBox.Show("請選擇調度算法", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void button1_Click(object sender, EventArgs e) //阻塞進程 { timer1.Enabled = false; process.state = 2; blockPro(process); runPro(); if (timeSlice!=0) timer1.Enabled = true; else MessageBox.Show("沒有就緒狀態的進程", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); } int index = 0; private void button2_Click(object sender, EventArgs e) //喚醒等待隊列中的進程 { if (listView2.Items.Count != 0) { Process p1=new Process(listView2.Items[index].SubItems[0].Text, int.Parse(listView2.Items[0].SubItems[1].Text), int.Parse(listView2.Items[0].SubItems[2].Text), int.Parse(listView2.Items[0].SubItems[3].Text), int.Parse(listView2.Items[0].SubItems[4].Text), int.Parse(listView2.Items[0].SubItems[5].Text)); wakePro(p1); //喚醒進程 listView2.Items.Remove(listView2.Items[index]); if (int.Parse(listView7.Items[0].SubItems[1].Text) == 0) runPro(); timer1.Enabled = true; } else MessageBox.Show("沒有進程阻塞", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void reset_Click(object sender, EventArgs e) //重置 { listView1.Items.Clear(); listView2.Items.Clear(); listView3.Items.Clear(); listView4.Items.Clear(); listView5.Items.Clear(); listView6.Items.Clear(); listView7.Items.Clear(); text1.Text = ""; text2.Text = ""; new_pro_name.Text = ""; in_time.Text = ""; need_t.Text = ""; req_mem.Text = ""; req_printer.Text = ""; radioButton1.Checked = false; radioButton2.Checked = false; } private void button5_Click(object sender, EventArgs e) //重置 { listView3.Items.Clear(); listView4.Items.Clear(); } private void button6_Click(object sender, EventArgs e) //暫停 { timer1.Enabled = false; } private void button7_Click(object sender, EventArgs e) //繼續 { timer1.Enabled = true; } } }
原題連接:http://programming2013.cstnet.cn/qualification/problem/2 題目描述: 時間限制: 1000ms 內存限制: 256MB 描述 在 N 條水平線與 M 條豎直線構
這一節給出了關於階乘的兩個問題,當然,並不是求階乘的值,而是求階乘的值的一些特性。 問題1:給定一個整數N,那麼N的階乘N! 末尾有多少個0? 如果將N! 的結果求出來,然後再計算末尾0的個數,那就要考慮是否會溢出,計算時間也要考慮。 這
問題是將一個小數表示成一個分數。當然,只有有限小數和無限循環小數才能轉換爲分數。 對於無限循環小數,給定的格式是將循環節用括號括起來,比如:0.3333(3333)。 書中先只考慮了0到1的純小數,然後分有限小數和無限循環小數討論,書上講
這節給的題目是從一串數字中尋找最大的K個數,而且考慮數據量比較大的情況。 解法一首先考慮了快速排序和堆排序,但是,它們對所有的數據都進行了排序,然後考慮使用部分排序算法,如選擇排序和交換排序,它們能夠從一串數字中選擇前K個,但是,效率依舊
本節給出的題目是: 給定一個十進制整數N,寫下從1開始,到N的所有整數,然後數一下其中出現的所有“1”的個數。 例如: N = 2,寫下1, 2。出現1個“1”。 N = 12,寫下1, 2, 3,4,5,6,7,8,9,10,11,12
題目來自《編程之美》一排石頭的遊戲 N塊石頭排成一列,每塊石頭都有自己的固定位置,也就是相當於有自己的編號一樣。兩個玩家依次取石頭,每個玩家每次可以取其中任意一塊石頭,或者相鄰的兩塊,最後將所有石頭取走的玩家贏。 這個遊戲有必勝策略嗎?
《編程之美》是基於windows系統的,而我使用的是ubuntu,所以在完成這道題的時候使用的是linux系統 1. top–>查詢當前cpu運行狀態 2. cat /proc/cpuinfo –>主機的cpu信息 /* *
這是從編程之美上看到的一道題,簡述題目內容如下: 給定正整數N,計算出從1到N的所有數字的十進制表示中出現1的次數,並找出能夠滿足f(N)==N的最大的N值。比如f(12) = 5, 因爲存在的數字有:1,10,11,12,總共五
題目:如果我們把二叉樹看成一個圖,父子節點之間的連線看成是雙向的,我們姑且定義”距離”爲兩節點之間邊的個數。寫一個程序,求一棵二叉樹中相距最遠的兩個節點之間的距離. 例如: 10 / \ 5 12 / \ 4 7
題目:輸入一棵二元樹的根結點,求該樹的深度。 從根結點到葉結點依次經過的結點(含根、葉結點)形成樹的一條路徑,最長路徑的長度爲樹的深度。 例如:輸入二元樹: 8 / / 6 10 // // 5 7 9
魯棒是英文Robust的音譯。 健壯性是指軟件對於規範要求以外的輸入情況的處理能力。 所謂健壯的系統是指對於規範要求以外的輸入能夠判斷出這個輸入不符合規範要求,並能有合理的處理方式。 另外健壯性有時也和容錯性,可移植性,正確性有交叉的地
現在要把這幾種常見的算法給理清弄明白了,要不然只能做個低級程序員了。 動態規劃DP是求解決策過程的最優化的數學方式。動態規劃一般分爲線性動規,區域動規,樹形動規,揹包動規。 動態規劃是一種方法,但不是一種算法,一般用於多決策中的最優化問
查找數組中最大值和最小值, 方法一:分治法,一分爲二,分別查找子數組中最大值和最小值,合併時取兩個子數組的較大者和較小者。f(n) = 2f(n-1)+2;f(n)=1.5n-2; 方法二:將數組中元素每兩個一組,先比較每組中兩個元素,
P161 分治法求解,複雜度爲f(N) = 1.5N-1; 代碼如下:比較簡單 #include <iostream> using namespace std; void FindMaxAndMin(int num[],int l
1.11 要求是N塊石頭排成一排,位置固定,A和B每次取任意一塊或者相鄰兩塊,最後取光者獲勝;使用對稱策略,先取者B先取中間的一個(奇數個)或者兩個(偶數個),然後取跟A對稱的位置相同的個數的石頭即可。先取者獲勝; 擴展問題1:條件不變