1、1第 1 章游 戏 之 乐游戏中碰到的题目1. 代码清单 1-1int main()for(; ; for(int i = 0; i level) System.Threading.Thread.Sleep(10); 代码清单 1-4/ C+ code to make task manager generate sine graph#include “Windows.h“#include “stdlib.h“#include “math.h“const double SPLIT = 0.01;const int COUNT = 200;const double PI = 3.14159265;
2、const int INTERVAL = 300;int _tmain(int argc, _TCHAR* argv)DWORD busySpanCOUNT; / array of busy times DWORD idleSpanCOUNT; / array of idle timesint half = INTERVAL / 2;double radian = 0.0;for(int i = 0; i HALF_BITS_LENGTH)/ 这个数字表示右 bits的mask,在二进制表示中,它表示00001111。#define RSET(b, n) (b = (LMASK for(LSE
3、T(b, 1); LGET(b) 0);m_nCakeCnt = nCakeCnt;/ 初始化烙 饼数组m_CakeArray = new intm_nCakeCnt; Assert(m_CakeArray != NULL);for(int i = 0; i m_nMaxSwap)return;/ 如果已 经排好序,即翻转完成,输出结果if(IsSorted(m_ReverseCakeArray, m_nCakeCnt)if(step pCakeArrayi)return false;return true;/ 翻转烙饼 信息/ void Revert(int nBegin, int nEnd
4、)Assert(nEnd nBegin);int i, j, t;8/ 翻 转烙饼信息for(i = nBegin, j = nEnd; i = 0; j-)for(int i = 0; i optij)optij = x;9return optV0;代码清单 1-10intV + 1T + 1 opt; / 子问题的记录项表,假设从i到T 种饮料中,/ 找出容量总和为V 的一个方案,满意度最多能够达到/ opt(V,i,T-1),存储于optVi,/ 初始化时opt中存储值为-1 ,表示该子问题尚未求解。int Cal(int V, int type)if(type = T)if(V = 0
5、)return 0;elsereturn -INF;if(V ret)ret = temp;return optVtype = ret;代码清单 1-11int nPerson; / nPersoni表示到第i 层的乘客数目int nFloor, nMinFloor, nTargetFloor;nTargetFloor = -1;for(i = 1; i nFloor) 10nMinFloor = nFloor;nTargetFloor = i;return(nTargetFloor, nMinFloor);代码清单 1-12int nPerson; / nPersoni表示到第i层的乘客数目
6、int nMinFloor, nTargetFloor;int N1, N2, N3;nTargetFloor = 1;nMinFloor = 0;for(N1 = 0, N2 = nPerson1, N3 = 0, i = 2; i MaxColor)MaxColor = nColorUsing; elsenColorUsing-;代码清单 1-15while(true)bool isDownloadCompleted;isDownloadCompleted = GetBlockFromNet(g_buffer);WriteBlockToDisk(g_buffer);if(isDownloa
7、dCompleted) break; 代码清单 1-16class Thread public:/ initialize a thread and set the work function Thread(void (*work_func)();/ once the object is destructed, the thread will be abortedThread();/ start the thread void Start();/ stop the threadvoid Abort();class Semaphore public:/ initialize semaphore c
8、ounts Semaphore(int count, int max_count);Semaphore();/ consume a signal (count-), block current thread if count = 0void Unsignal();/ raise a signal (count+)void Signal();12;class Mutex public:/ block thread until other threads release the mutex WaitMutex();/ release mutex to let other thread wait f
9、or itReleaseMutex();代码清单 1-17#define BUFFER_COUNT 100Block g_bufferBUFFER_COUNT;Thread g_threadA(ProcA);Thread g_threadB(ProcB);Semaphore g_seFull(0, BUFFER_COUNT);Semaphore g_seEmpty(BUFFER_COUNT, BUFFER_COUNT);bool g_downloadComplete;int in_index = 0;int out_index = 0;void main()g_downloadComplete
10、 = false;threadA.Start();threadB.Start();/ wait here till threads finishedvoid ProcA()while(true)g_seEmpty.Unsignal();g_downloadComplete = GetBlockFromNet(g_buffer + in_index);in_index = (in_index + 1) % BUFFER_COUNT;g_seFull.Signal();if(g_downloadComplete)break;void ProcB()while(true)g_seFull.Unsig
11、nal();WriteBlockToDisk(g_buffer + out_index);out_index = (out_index + 1) % BUFFER_COUNT;g_seEmpty.Signal();if(g_downloadComplete 13代码清单 1-18:C#自底向上的解法static bool nim(int x, int y)/ speical caseif(x = y)return true; / I win/ swap the numberif(x y)int t = x; x = y; y = t; / basic casesif(x = 1 / I los
12、eArrayList al = new ArrayList();al.Add(2); int n = 1; int delta = 1;int addition = 0; while(x n)/ find the next n;while(al.IndexOf(+n) != -1);delta+; al.Add(n + delta); addition+;if(al.Count 2 addition = 0; if(x != n) | (al.IndexOf(y) = -1)return true; / I win14elsereturn false; / I losestatic void
13、ShrinkArray(ArrayList al, int n)for(int i = 0; i n)al.RemoveRange(0, i);return; 代码清单 1-19bool nim(int n, int m) double a, b;a = (1 + sqrt(5.0) / 2;b = (3 + sqrt(5.0) / 2;if(n = m) / 两堆石头数量相同return true;if(n m)swap(n, m); / 我们假设所有的状态中xm,则交换二者if(n = (long)floor(m-n)*a) / floor为取下整数的操作符return false;els
14、ereturn true;代码清单 1-20/ Comments: Python codefalse_table = dict()true_table = dict()def possible_next_moves(m, n):15for i in range(0, m):yield(i, n)for i in range(0, n):if m n:m, n = n, mname = str(m) + + + str(n)if name in false_table:return false_tablenameif name in true_table:return true_tablenam
15、echeck = quick_check(m, n, name)if check:return checkfor possible in possible_next_moves(m, n):r = nim(possible0, possible1)if r0 = False:true_tablename = (True, possible)return (True, possible)elif can_reach(m, n, r10, r11):true_tablename = (True, r1)return (True, r1)false_tablename = (False, (m, n
16、)return (False, (m, n) #for testing16def assert_false(m, n):size = 0for possible in possible_next_moves(m, n):size = size + 1r = nim(possible0, possible1)if r0 != True:print error!, m, n,should be false but it has false sub move,possiblereturnprint all, size, possible moves are checked! 很快,这位工程师又想出了
17、另一种解法,不过这次他不是从 n = 1 的不安全局面自底向上推理的,而是反其道行之,自顶向下查找,代码如清单 1-21,读者不妨研究一下:代码清单 1-21/ Result indicates position(X,Y) is whether true or false/ true means when m = X and n = Y, then the first one will win/ false vice versapublic class Resultpublic override string ToString()string ret = string.Format(“0 (1
18、, 2)“, State.ToString(),X, Y);return ret;public Result(bool s, uint x, uint y)State = s;X = x;Y = y;public bool State;public uint X, Y; public static Result nim(uint m, uint n)if(m = n | m = 0 | n = 0)return new Result(true, m, n);if(m n - y)steps = n - y;for(uint i = 1; i 0)c.PickNextValidValue();i
19、f(coCurrent.x = this.Size - 1 / we reach the end of the matrixelse / keep going to the next onecoCurrent = NextCoord(coCurrent);20else/ if we reach the beginning, break outif(coCurrent.x = 0 elsec.Clear();coCurrent = PrevCoord(coCurrent);return true; 代码清单 1-24f(Array)if(Array.Length x;numberi = x;it
20、oa(x, buffer, 10);resulti = buffer;if(PointsGame(CardsNumber)cout 0And AreaOffsetX + iOffsetY + j 5) Then Score -= 15 / 超过5个洞额外扣除15分If (OffsetY = 1;return num;代码清单 2-3int Count(BYTE v)int num = 0;while(v)v 26num+;return num;代码清单 2-4int Count(BYTE v)int num = 0;switch (v)case 0x0:num = 0;break;case 0
21、x1:case 0x2:case 0x4:case 0x8:case 0x10:case 0x20:case 0x40:case 0x80:num = 1;break;case 0x3:case 0x6:case 0xc:case 0x18:case 0x30:case 0x60:case 0xc0:num = 2;break;/.return num; 代码清单 2-5/* 预定义的结果表 */int countTable256 =0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3,3, 4
22、, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3,4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4,3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3,4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6,6, 7, 1,
23、 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4,5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3,4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4,4, 5, 4, 5,
24、5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6,7, 6, 7, 7, 827;int Count(BYTE v)/check parameterreturn countTablev;代码清单 2-6ret = 0;for(i = 1; i = 1;Ret += N;return Ret;代码清单 2-8Type Find(Type* ID, int N)Type candidate;int nTimes, i;for(i = nTimes = 0; i p ? Sa.Append(Si) : Sb.Append(Si
25、)/ 将 p加入较小的组,可以避免分组失败,也使分组/ 更均匀,提 高 效 率 length Sa delta)Vmid = Vmin + (Vmax - Vmin) * 0.5;if(f(arr, N, Vmid) = K)Vmin = Vmid;30elseVmax = Vmid;代码清单 2-13if(X h0)h0 = X;p = 0;while(p = K) break;if(q = 0; v-)sumCount += countv;if(sumCount = K)break;return v;代码清单 2-15BigInt gcd(BigInt x, BigInt y)if(x y)return gcd(y, x);if(y = 0)return x;elsereturn gcd(x - y, y);代码清单 2-16BigInt gcd(BigInt x, BigInt y)