【发布时间】:2013-06-04 11:30:46
【问题描述】:
我有一组数字:1、2、4、8、16、32、64等
现在给定一个数字,比如说 44,我必须确定它有 32、8 和 4 个孩子。 (32 + 8 + 4 = 44)
到目前为止,我有以下代码:
public static long[] GetBTreeChildren(long? parentMask)
{
var branches = new List<long>();
if (parentMask == null) return branches.ToArray();
double currentValue = (double)parentMask;
while (currentValue > 0D)
{
double power = Math.Floor(Math.Log(currentValue, 2.0D));
double exponentResult = Math.Pow(2, power);
branches.Add((long)exponentResult);
currentValue -= exponentResult;
}
return branches.ToArray();
}
但是当给定的数字非常大(例如 36028797018963967)时,上面的代码不起作用
我正在使用 VS2012 (C#)。
【问题讨论】:
-
你尝试过 BigInteger 吗?
-
c#中没有BigInteger
标签: c# binary-tree