電梯調度演算法

來源:互聯網
上載者:User

標籤:

對於電梯的要求:電梯在一樓出發,待使用者輸完想停的樓層後,停在其中的一層樓然後所有人下去徒步找各自的目標地,使最後所有人的爬樓梯數最短。

      設最後停在i層,i層下有n1個人,i層有n2個人,i+1層有n3個人,假設停i層為最短Y,若停在i-1層則Y-(n1-n2-n3)停在i+1層則Y-(n3-n1-n2)則n1>n2+n3停在i-1層,n3>n1+n2則電梯停在i+1層合算其餘停i。

 1 //電梯演算法 2 #include<iostream> 3 using namespace std; 4  5 int main() 6 { 7     int people[10]; 8     int i; 9     int n1 = 0;10     int n2;11     int n3 = 0;12     /*int sum;*/13     for (i = 0; i < 10; i++)14     {15         cin >> people[i];16     /*    sum += people[i];*/17     }18     n2 = people[0];19     for (i = 1; i <= 9; i++)20     {21         n3 += people[i];22     }23     for (i = 0; i < 10; i++)24     {25         if (n3 > n1 + n2)26         {27             n1 = n1 + n2;28             n2 = people[i + 1];29             n3 -= people[i + 1];30         }31         else32         {33             cout << "要停在";34             cout << i + 1;35             break;36         }37     }38 39 40     return 0;41 }

時間複雜度o(n)的演算法最佳化了,卻是我思維僵化了,本演算法在老師的協助下想出的。

電梯調度演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.