这是编程之美里的一个题目,我改写原来的代码为:
- #include<windows.h>
- #include<math.h>
- int main()
- {
- //如果是双核请不要把这一句注释掉
- //SetProcessAffinityMask(GetCurrentProcess(),0x1);
- const double STEP=0.01,PI=3.14159265359;
- const int INTERVAL=100;
- double x=0.0;
- DWORD busy,idle,startTime;
- while(true)
- {
- busy=(DWORD)(INTERVAL*.5*(sin(PI*x)+1));
- idle=INTERVAL-busy;
- startTime=GetTickCount();
- while((GetTickCount()-startTime)<=busy)
- ;
- Sleep(idle);
- x+=STEP;
- }
- return 0;
- }