为了账号安全,请及时绑定邮箱和手机立即绑定

c语言 x的n次方 用函数

c语言 x的n次方 用函数

C++
qq_我是超人_4 2017-11-16 11:37:44
查看完整描述

1 回答

已采纳
?
天将明96

TA贡献15条经验 获得超13个赞

#include <stdio.h>

//循环版
int myPow1(int x, int n) {
    int result = 1;
    if (n == 0) {
        return result;
    }
    for (int i = 0; i < n; i++) {
        result *= x;
    }
    return result;
}

//递归版
int myPow2(int x, int n) {
    if (n == 0)
        return 1;
    if (n == 1)
        return x;
    if (n > 1)
        return myPow2(x, n - 1) * x;
}

int main() {
    printf("%d\n", myPow1(5, 4));
    printf("%d\n", myPow2(5, 4));
}

//img1.sycdn.imooc.com//5a0d0f2c000164fc04950170.jpg

如果满足要求,望采纳!<(▰˘◡˘▰)>

查看完整回答
2 反对 回复 2017-11-16
  • 1 回答
  • 0 关注
  • 3123 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信