如何使用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 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短