如何使用C#计算三的幂?
对于3的幂,将幂设置为3并应用递归代码,如以下代码段所示-
if (p!=0) {
return (n * power(n, p - 1));
}假设数字为5,则迭代次数为-
power(5, 3 - 1)); // 25 power (5,2-1): // 5
上面将返回5*25,即125,如下所示-
示例
using System;
using System.IO;
public class Demo {
public static void Main(string[] args) {
int n = 5;
int p = 3;
long res;
res = power(n, p);
Console.WriteLine(res);
}
static long power (int n, int p) {
if (p!=0) {
return (n * power(n, p - 1));
}
return 1;
}
}输出结果
125
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短