1.1 cpu使用問題
[cpp]
#include <iostream>
#include <ctime>
#include <cmath>
#include <Windows.h>
using namespace std;
//第一種方式
void main()
{
INT64 start=0;
int busy=10;
int idle=busy;
cout<<"CPU使用率問題";
while(true)
{
start=GetTickCount();
while((GetTickCount()-start)<=busy);
Sleep(idle);
}
}
//第二種方式
int main()
{
for(;;)
{
for(int i = 0; i < 9600000; i++);
//for(int i = 0; i < 21360000; i++);//2.67Ghz 4核
Sleep(10);
}
return 0;
}
//正玄曲線
const double SPLIT=0.01;
const int COUNT=200;
const double PI=3.14159265;
const int INTERVAL = 300;
void main()
{
DWORD busy[COUNT],idle[COUNT];
int half=INTERVAL/2;
double radian=0.0;
for(int i=0;i<COUNT;i++)
{
busy[i]=DWORD(sin(PI*radian)*half+half);
idle[i]=INTERVAL-busy[i];
radian+=0.01;
}
DWORD start=0;
int j=0;
while(true)
{
start=GetTickCount();
j=j%COUNT;
while((GetTickCount()-start)<=busy[j]);
Sleep(idle[j]);
j++;
}
}
#include <iostream>
#include <ctime>
#include <cmath>
#include <Windows.h>
using namespace std;
//第一種方式
void main()
{
INT64 start=0;
int busy=10;
int idle=busy;
cout<<"CPU使用率問題";
while(true)
{
start=GetTickCount();
while((GetTickCount()-start)<=busy);
Sleep(idle);
}
}
//第二種方式
int main()
{
for(;;)
{
for(int i = 0; i < 9600000; i++);
//for(int i = 0; i < 21360000; i++);//2.67Ghz 4核
Sleep(10);
}
return 0;
}
//正玄曲線
const double SPLIT=0.01;
const int COUNT=200;
const double PI=3.14159265;
const int INTERVAL = 300;
void main()
{
DWORD busy[COUNT],idle[COUNT];
int half=INTERVAL/2;
double radian=0.0;
for(int i=0;i<COUNT;i++)
{
busy[i]=DWORD(sin(PI*radian)*half+half);
idle[i]=INTERVAL-busy[i];
radian+=0.01;
}
DWORD start=0;
int j=0;
while(true)
{
start=GetTickCount();
j=j%COUNT;
while((GetTickCount()-start)<=busy[j]);
Sleep(idle[j]);
j++;
}
}CPU核心運行周期數
[cpp]
#include <iostream>
using namespace std;
inline __int64 GetCPUTickCount()
{
__asm
{
rdtsc;
}
}
void main()
{
cout<<"CPU核心運行周期數"<<GetCPUTickCount()<<endl;
system("pause");
}
#include <iostream>
using namespace std;
inline __int64 GetCPUTickCount()
{
__asm
{
rdtsc;
}
}
void main()
{
cout<<"CPU核心運行周期數"<<GetCPUTickCount()<<endl;
system("pause");
}
1.2 將帥問題
[cpp]
#include <iostream>
using namespace std;
//第一種方式
struct {
unsigned char a:4;
unsigned char b:4;
} i;
void main()
{
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);
system("pause");
}
//第二種方式
#define HALF_BITS_LENGTH 4
// 這個值是記憶存儲單元長度的一半,在這道題裡是4bit
#define FULLMASK 255
// 這個數字表示一個全部bit的mask,在二進制表示中,它是11111111。
#define LMASK (FULLMASK << HALF_BITS_LENGTH)
// 這個宏表示左bits的mask,在二進制表示中,它是11110000。
#define RMASK (FULLMASK >> HALF_BITS_LENGTH)
// 這個數字表示右bits的mask,在二進制表示中,它表示00001111。
#define RSET(b, n) (b = ((LMASK & b) ^ n))
// 這個宏,將b的右邊設置成n
#define LSET(b, n) (b = ((RMASK & b) ^ (n << HALF_BITS_LENGTH)))
// 這個宏,將b的左邊設置成n
#define RGET(b) (RMASK & b)
// 這個宏得到b的右邊的值
#define LGET(b) ((LMASK & b) >> HALF_BITS_LENGTH)
// 這個宏得到b的左邊的值
#define GRIDW 3
// 這個數字表示將帥移動范圍的行寬度。
#include <stdio.h>
#define HALF_BITS_LENGTH 4
#define FULLMASK 255
#define LMASK (FULLMASK << HALF_BITS_LENGTH)
#define RMASK (FULLMASK >> HALF_BITS_LENGTH)
#define RSET(b, n) (b = ((LMASK & b) ^ n))
#define LSET(b, n) (b = ((RMASK & b) ^ (n << HALF_BITS_LENGTH)))
#define RGET(b) (RMASK & b)
#define LGET(b) ((LMASK & b) >> HALF_BITS_LENGTH)
#define GRIDW 3
int main()
{
unsigned char b;
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));
system("pause");
return 0;
}
#include <iostream>
using namespace std;
//第一種方式
struct {
unsigned char a:4;
unsigned char b:4;
} i;
void main()
{
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);
system("pause");
}
//第二種方式
#define HALF_BITS_LENGTH 4
// 這個值是記憶存儲單元長度的一半,在這道題裡是4bit
#define FULLMASK 255
// 這個數字表示一個全部bit的mask,在二進制表示中,它是11111111。
#define LMASK (FULLMASK << HALF_BITS_LENGTH)
// 這個宏表示左bits的mask,在二進制表示中,它是11110000。
#define RMASK (FULLMASK >> HALF_BITS_LENGTH)
// 這個數字表示右bits的mask,在二進制表示中,它表示00001111。
#define RSET(b, n) (b = ((LMASK & b) ^ n))
// 這個宏,將b的右邊設置成n
#define LSET(b, n) (b = ((RMASK & b) ^ (n << HALF_BITS_LENGTH)))
// 這個宏,將b的左邊設置成n
#define RGET(b) (RMASK & b)
// 這個宏得到b的右邊的值
#define LGET(b) ((LMASK & b) >> HALF_BITS_LENGTH)
// 這個宏得到b的左邊的值
#define GRIDW 3
// 這個數字表示將帥移動范圍的行寬度。
#include <stdio.h>
#define HALF_BITS_LENGTH 4
#define FULLMASK 255
#define LMASK (FULLMASK << HALF_BITS_LENGTH)
#define RMASK (FULLMASK >> HALF_BITS_LENGTH)
#define RSET(b, n) (b = ((LMASK & b) ^ n))
#define LSET(b, n) (b = ((RMASK & b) ^ (n << HALF_BITS_LENGTH)))
#define RGET(b) (RMASK & b)
#define LGET(b) ((LMASK & b) >> HALF_BITS_LENGTH)
#define GRIDW 3
int main()
{
unsigned char b;
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));
system("pause");
return 0;
}
1.12 電梯調度
[cpp]
#include <iostream>
using namespace std;
#define N 6
void main()
{
int nPerson[N]={55,66,77,88,99,44};
int N1=0,N2=0,N3=0;
int nTargetFloor=0,nMinFloor=0,i;
for (i=1,N1=0,N2=nPerson[0],N3=0;i<N;i++)
{
N3+=nPerson[i];
nMinFloor+=nPerson[i+1]*i;
}
for (i=1;i<N;i++)
{
if (N1+N2<N3)
{
nTargetFloor=i+1;
nMinFloor+=(N1+N2-N3);
N1+=N2;
N2=nPerson[i];
N3-=nPerson[i];
}
else
break;
}
cout<<"nTargetFloor "<<nTargetFloor<<"\nnMinFloor "<<nMinFloor<<endl;
system("pause");
}
#include <iostream>
using namespace std;
#define N 6
void main()
{
int nPerson[N]={55,66,77,88,99,44};
int N1=0,N2=0,N3=0;
int nTargetFloor=0,nMinFloor=0,i;
for (i=1,N1=0,N2=nPerson[0],N3=0;i<N;i++)
{
N3+=nPerson[i];
nMinFloor+=nPerson[i+1]*i;
}
for (i=1;i<N;i++)
{
if (N1+N2<N3)
{
nTargetFloor=i+1;
nMinFloor+=(N1+N2-N3);
N1+=N2;
N2=nPerson[i];
N3-=nPerson[i];
}
else
break;
}
cout<<"nTargetFloor "<<nTargetFloor<<"\nnMinFloor "<<nMinFloor<<endl;
system("pause");
}
1.13 NIM兩堆石頭
[cpp]
#include <iostream>
#include <cmath>
using namespace std;
#define swap(x,y) ((x)^=(y),(y)^=(x),(x)^=(y))
void main()
{
double a,b;
a=(1+sqrt(5.0))/2;
b=(3+sqrt(5.0))/2;
int m,n;
bool nim=false;
cout<<"輸入兩堆石頭的書數目\n";
cin>>m>>n;
if (m==n)
nim=true;
if(n>m)
swap(n,m);
if (n-m==(long)floor(n*a))
nim=false;
else
nim=true;
if(nim)
cout<<"先取石頭玩家先贏\n";
else
cout<<"後取石頭玩家先贏\n";
system("pause");
}
#include <iostream>
#include <cmath>
using namespace std;
#define swap(x,y) ((x)^=(y),(y)^=(x),(x)^=(y))
void main()
{
double a,b;
a=(1+sqrt(5.0))/2;
b=(3+sqrt(5.0))/2;
int m,n;
bool nim=false;
cout<<"輸入兩堆石頭的書數目\n";
cin>>m>>n;
if (m==n)
nim=true;
if(n>m)
swap(n,m);
if (n-m==(long)floor(n*a))
nim=false;
else
nim=true;
if(nim)
cout<<"先取石頭玩家先贏\n";
else
cout<<"後取石頭玩家先贏\n";
system("pause");
}