首页 > 试题广场 >

两个数的调和平均数可用如下方法得到:首先对两数的倒数取平均值

[问答题]

两个数的调和平均数可用如下方法得到:首先对两数的倒数取平均值,最后再取倒数。使用#define指令定义一个宏“函数”执行这个运算。编写一个简单的程序测试该宏。

推荐
#include <stdio.h>
#define HARMONIC_MEAN(X, Y) ( 1/(1/X+1/Y) )
int main(void)
{
 double x, y;
 scanf("%lf%lf",&x, &y);
 printf("harmonic mean of %g and %g : %g\n",x, y, HARMONIC_MEAN(x, y));
 return 0;
}

发表于 2018-03-14 14:39:58 回复(2)