如何使用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 生日广州男生祝福语简短