两个数的调和平均数可用如下方法得到:首先对两数的倒数取平均值,最后再取倒数。使用#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; }
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题