C++完美演繹 經典算法 如 /* 頭文件:my_Include.h */ #include <stdio.h> /* 展開C語言的內建函數指令 */ #define PI 3.1415926 /* 宏常量,在稍后章節再詳解 */ #define circle(radius) (PI*radius*radius) /* 宏函數,圓的面積 */ /* 將比較數值大小的函數寫在自編include文件內 */ int show_big_or_small (int a,int b,int c) { int tmp if (a>b) { tmp = a a = b b = tmp } if (b>c) { tmp = b b = c c = tmp } if (a>b) { tmp = a a = b b = tmp } printf("由小至大排序之后的結果:%d %d %d\n", a, b, c) } 程序執行結果: 由小至大排序之后的結果:1 2 3 可將內建函數的include文件展開在自編的include文件中 圓圈的面積是=201.0619264
算法ebook(10部算法經典著作的合集) 算法ebook> 10部算法經典著作的合集 chm格式 (1)Fundamentals of Data Structures by Ellis Horowitz and Sartaj Sahni (2)Data Structures, Algorithms and Program Style Using C by James F. Korsh and Leonard J. Garrett (3)Data Structures and Algorithm Analysis in C by Mark Allen Weiss (4)Data Structures: From Arrays to Priority Queues by Wayne Amsbury (5)Information Retrieval: Data Structures & Algorithms edited by William B. Frakes and Ricardo Baeza-Yates (6)Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson, and Ronald L. Rivest (7)Practical Data Structures in C++ by Bryan Flamig (8)Reliable Data Structures in C by Thomas Plum (9)Data Structures and Algorithms Alfred V. Aho, Bell Laboratories, Murray Hill, New Jersey John E. Hopcroft, Cornell University, Ithaca, New York Jeffrey D. Ullman, Stanford University, Stanford, California (10)DDJ Algorithms and Data Structures Articles
1. 汽車輪渡口,過江渡船每次能載10輛車過江,過江車輛分為客車類和貨車類,上渡船有如下規定:同類車先到先上船,客車先于貨車上渡船,且每上4輛客車,才允許上一輛貨車。若等待客車不足4輛,則從貨車代替,若無貨車等待允許客車上船。試寫一個算法模擬渡口管理。
算法設計:
1客車和貨車均建立一個鏈式隊列,初始均為空。以后來一輛車不是貨車就是客車,因此可以說整個程序的事件驅動event就是這兩個,客車表示1,貨車表示0.
2輪船還沒有到達時客車和貨車均按次序排在各自隊列中。
3輪船到達時,根據兩個隊列的情況,分別處理。處理如下:
a 客車數不滿4輛,則將排在前面的貨車上船,但總數不能超過10,若沒有貨車等待,客車直接上船。
b 客車數滿4,但不滿8輛,客車先上,排在前面的只有一輛貨車可以上船,若沒有貨車等待則貨車不上。
c 客車滿8輛但不滿10,客車上船,排在前面的貨車最多可以上2輛,但總數不能超過10。
d 客車滿10,則全上客車,但總數不能超過10。