1第1章游戏之乐——游戏中碰到的题目1.intmain(){for(;;{for(inti=0;i9600000;i++);Sleep(10);}return0;}intbusyTime=10;//10msintidleTime=busyTime;//sameratiowillleadto50%cpuusageInt64startTime=0;while(true){startTime=GetTickCount();//busyloopwhile((GetTickCount()-startTime)=busyTime);//idleloopSleep(idleTime);}2.//C#codestaticvoidMakeUsage(floatlevel){代码清单1-3代码清单1-2代码清单1-12PerformanceCounterp=newPerformanceCounter(Processor,%ProcessorTime,_Total);if(p==NULL){return}while(true){if(p.NextValue()level)System.Threading.Thread.Sleep(10);}}//C++codetomaketaskmanagergeneratesinegraph#includeWindows.h#includestdlib.h#includemath.hconstdoubleSPLIT=0.01;constintCOUNT=200;constdoublePI=3.14159265;constintINTERVAL=300;int_tmain(intargc,_TCHAR*argv[]){DWORDbusySpan[COUNT];//arrayofbusytimesDWORDidleSpan[COUNT];//arrayofidletimesinthalf=INTERVAL/2;doubleradian=0.0;for(inti=0;iCOUNT;i++){busySpan[i]=(DWORD)(half+(sin(PI*radian)*half));idleSpan[i]=INTERVAL-busySpan[i];radian+=SPLIT;}DWORDstartTime=0;intj=0;while(true){j=j%COUNT;startTime=GetTickCount();while((GetTickCount()-startTime)=busySpan[j]);Sleep(idleSpan[j]);j++;}return0;}代码清单1-43_PROCESSOR_POWER_INFORMATIONinfo;CallNTPowerInformation(11,//queryprocessorpowerinformationNULL,//noinputbuffer0,//inputbuffersizeiszero&info,//outputbufferSizeof(info));//outbufsize__int64t_begin=GetCPUTickCount();//dosomething__int64t_end=GetCPUTickCount();doublemillisec=((double)t_end–(double)t_begin)/(double)info.CurrentMhz;#defineHALF_BITS_LENGTH4//这个值是记忆存储单元长度的一半,在这道题里是4bit#defineFULLMASK255//这个数字表示一个全部bit的mask,在二进制表示中,它是11111111。#defineLMASK(FULLMASKHALF_BITS_LENGTH)//这个宏表示左bits的mask,在二进制表示中,它是11110000。#defineRMASK(FULLMASKHALF_BITS_LENGTH)//这个数字表示右bits的mask,在二进制表示中,它表示00001111。#defineRSET(b,n)(b=((LMASK&b)^n))//这个宏,将b的右边设置成n#defineLSET(b,n)(b=((RMASK&b)^(nHALF_BITS_LENGTH)))//这个宏,将b的左边设置成n#defineRGET(b)(RMASK&b)//这个宏得到b的右边的值#defineLGET(b)((LMASK&b)HALF_BITS_LENGTH)//这个宏得到b的左边的值#defineGRIDW3//这个数字表示将帅移动范围的行宽度。#includestdio.h#defineHALF_BITS_LENGTH4#defineFULLMASK255#defineLMASK(FULLMASKHALF_BITS_LENGTH)#defineRMASK(FULLMASKHALF_BITS_LENGTH)#defineRSET(b,n)(b=((LMASK&b)^n))#defineLSET(b,n)(b=((RMASK&b)^(nHALF_BITS_LENGTH)))#defineRGET(b)(RMASK&b)#defineLGET(b)((LMASK&b)HALF_BITS_LENGTH)#defineGRIDW3intmain(){代码清单1-6代码清单1-54unsignedcharb;for(LSET(b,1);LGET(b)=GRIDW*GRIDW;LSET(b,(LGET(b)+1)))for(RSET(b,1);RGET(b)=GRIDW*GRIDW;RSET(b,(RGET(b)+1)))if(LGET(b)%GRIDW!=RGET(b)%GRIDW)printf(A=%d,B=%d\n,LGET(b),RGET(b));return0;}struct{unsignedchara:4;unsignedcharb:4;}i;for(i.a=1;i.a=9;i.a++)for(i.b=1;i.b=9;i.b++)if(i.a%3!=i.b%3)printf(“A=%d,B=%d\n”,i.a,i.b);/****************************************************************/////烙饼排序实现///****************************************************************/classCPrefixSorting{public:CPrefixSorting(){m_nCakeCnt=0;m_nMaxSwap=0;}~CPrefixSorting(){if(m_CakeArray!=NULL){deletem_CakeArray;}if(m_SwapArray!=NULL){deletem_SwapArray;}if(m_ReverseCakeArray!=NULL){deletem_ReverseCakeArray;}if(m_ReverseCakeArraySwap!=NULL){deletem_ReverseCakeArraySwap;代码清单1-8代码清单1-75}}////计算烙饼翻转信息//@param//pCakeArray存储烙饼索引数组//nCakeCnt烙饼个数//voidRun(int*pCakeArray,intnCakeCnt){Init(pCakeArray,nCakeCnt);m_nSearch=0;Search(0);}////输出烙饼具体翻转的次数//voidOutput(){for(inti=0;im_nMaxSwap;i++){printf(%d,m_arrSwap[i]);}printf(\n|SearchTimes|:%d\n,m_nSearch);printf(TotalSwaptimes=%d\n,m_nMaxSwap);}private:////初始化数组信息//@param//pCakeArray存储烙饼索引数组//nCakeCnt烙饼个数//voidInit(int*pCakeArray,intnCakeCnt){Assert(pCakeArray!=NULL);Assert(nCakeCnt0);m_nCakeCnt=nCakeCnt;//初始化烙饼数组m_CakeArray=newint[m_nCakeCnt];Assert(m_CakeArray!=NULL);for(inti=0;im_nCakeCnt;i++){m_CakeArray[i]=pCakeArray[i];}6//设置最多交换次数信息m_nMaxSwap=UpBound(m_nCakeCnt);//初始化交换结果数组m_SwapArray=newint[m_nMaxSwap+1];Assert(m_SwapArray!=NULL);//初始化中间交换结果信息m_ReverseCakeArray=newint[m_nCakeCnt];for(i=0;im_nCakeCnt;i++){m_ReverseCakeArray[i]=m_CakeArray[i];}m_ReverseCakeArraySwap=newint[m_nMaxSwap];}////寻找当前翻转的上界////intUpBound(intnCakeCnt){returnnCakeCnt*2;}////寻找当前翻转的下界////intLowerBound(int*pCakeArray,intnCakeCnt){intt,ret=0;//根据当前数组的排序信息情况来判断最少需要交换多少次for(inti=1;inCakeCnt;i++){//判断位置相邻的两个烙饼,是否为尺寸排序上相邻的t=pCakeArray[i]-pCakeArray[i-1];if((t==1)||(t==-1)){}else{ret++;}}returnret;}//排序的主函数voidSearch(intstep){inti,nEstimate;7m_nSearch++;//估算这次搜索所需要的最小交换次数nEstimate=LowerBound(m_ReverseCakeArray,m_nCakeCnt);if(step+nEstimatem_nMaxSwap)return;//如果已经排好序,即翻转完成,输出结果if(IsSorted(m_ReverseCakeArray,m_nCakeCnt)){if(stepm_nMaxSwap){m_nMaxSwap=step;for(i=0;im_nMaxSwap;i++)m_arrSwap[i]=m_ReverseCakeArraySwap[i];}return;}//递归进行翻转for(i=1;im_nCakeCnt;i++){Revert(0,i);m_ReverseCakeArraySwap[step]=i;Search(step+1);Revert(0,i);}}////true:已经排好序//false:未排序//boolIsSorted(int*pCakeArray,intnCakeCnt){for(inti=1;inCakeCnt;i++){if(pCakeArray[i-1]pCakeArray[i]){returnfalse;}}returntrue;}////翻转烙饼信息//voidRevert(intnBegin,intnEnd){Assert(nEndnBegin);inti,j,t;//翻转烙饼信息8for(i=nBegin,j=nEnd;ij;i++,j--){t=m_ReverseCakeArray[i];m_ReverseCakeArray[i]=m_ReverseCakeArray[j];m