1,判斷兩個浮點數是否相等。
#define ISZERO(x, e) (((x) >= -(e)) && ((x) <= (e)))
cout << boolalpha << (ISZERO(float1 - float2, 0.0001) ? true : false) << endl;
2,求某個無符號整數是否為2的整數次冪,要求高效。
((value & (value - 1)) ? false : true
3,求兩個無符號整數的最大公約數,要求高效。
#include<stdio.h>
int main()
{
int a,b,num1,num2,temp;
printf("Input a & b:");
scanf("%d%d",&num1,&num2);
if(num1>num2) /*找出兩個數中的較大值*/
{
temp=num1; num1=num2; num2=temp; /*交換兩個整數*/
}
a=num1; b=num2;
while(b!=0) /*采用輾轉相除法求最大公約數*/
{
temp=a%b;
a=b;
b=temp;
}
printf("The GCD of %d and %d is: %d\n",num1,num2,a); /*輸出最大公約數*/
printf("The LCM of them is: %d\n",num1*num2/a); /*輸出最小公倍數*/
}