用C语言编写一个函数 my_power用循环的方法实现返回一个 float 类型数的某个整数次幂保留 6 位小数。例如:调用 my_power314 -2返回 0101424
#include <stdio.h>
float my_power(float x, int n) {
float result = 1.0;
int i;
if (n >= 0) {
for (i = 0; i < n; i++) {
result *= x;
}
} else {
for (i = 0; i > n; i--) {
result /= x;
}
}
return result;
}
int main() {
float x = 3.14;
int n = -2;
float result = my_power(x, n);
printf("%.6f\n", result);
return 0;
}
输出结果为:
0.101424
原文地址: https://www.cveoy.top/t/topic/h2qb 著作权归作者所有。请勿转载和采集!