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