【发布时间】:2011-03-23 04:59:35
【问题描述】:
在下面的面试问题中:
给定一个数字 n,给我数字 (在
3..5和偶数个 数字),其加法将返回 原号码。结果数字 应尽可能平衡, 这意味着不是返回3和5,例如,返回4和4。例如:7 = 3 + 4 16 = 4 + 4 + 4 + 4 rather than 3 + 5 + 4 + 4 24 = 12 + 12 or 6 + 6 + 6 + 6
我想到了以下方法:
splitnumber(int n)
{
//check if the number is even
if(n%2==0)
{
print(n/2,n/2);
//check if x=2^m multiple exists or
// not..like 4,8,16 etc
print (n/x...n/x);
}
else //else if the no is odd... this part is incomplete
{
if(n-3>0)
{
print (3);
}
n-=3;
if(n>0)
{
if (n>5)
{
print(3)
n-=3;
}
}
}
}
但我仍然无法完成所有案例......我应该如何检查答案何时有不平衡的解决方案??
【问题讨论】:
-
多么烦人的面试问题。
-
如果工作是 html 开发人员,则可以加分。
-
为什么
24被分解成6 + 6 + 6 + 6,而不是4 + 4 + 4 + 4 + 4 + 4? -
你可以有偶数......是的 4 + 4 + 4 + 4 + 4 + 4 也是一个答案
-
如果你可以使用任何整数,
3..5是什么意思?