描述:
给出一个整数n,返回2的从0到n的所有乘方的数组。
例如:
n = 0 -> 2^0 -> [1]
n = 1 -> 2^0, 2^1 -> [1,2]
n = 2 -> 2^0, 2^1, 2^2 -> [1,2,4]
MyCode:
using System.Numerics;
public class Kata
{
public static BigInteger[] PowersOfTwo(int n)
{
BigInteger[] ret = new BigInteger[n+1];//定义一个要返回的数组
for(int i = 0;i <= n;i++)//利用for循环把每个2的乘方值赋给数组
{
ret[i]= BigInteger.Pow(2,i);//BigInteger.Pow(底数,指数)
}
return ret;
}
}
CodeWar:
using System.Numerics;
using System.Linq;
public class Kata
{
public static BigInteger[] PowersOfTwo(int n)
{
using System.Linq;
using System.Numerics;
public class Kata
{
public static BigInteger[] PowersOfTwo(int n)
{
return Enumerable.Range(0, n+1).Select(x => BigInteger.Pow(2, x)).ToArray();
}
}
}
}