實驗2-4-5 簡單實現x的n次方 (10 分)
本題要求實現一個計算x
n
(n≥0)的函數。
函數接口定義:
double mypow( double x, int n );
函數mypow應返回x的n次冪的值。題目保證結果在雙精度範圍內。
裁判測試程序樣例:
#include <stdio.h>
double mypow( double x, int n );
int main()
{
double x;
int n;
scanf("%lf %d", &x, &n);
printf("%f\n", mypow(x, n));
return 0;
}
/* 你的代碼將被嵌在這裏 */
輸入樣例:
0.24 4
輸出樣例:
0.003318
double mypow( double x, int n )
{
double res=1;
while(n--) res=res*x;
return res;
}