n*c 发帖数: 228 | 1 int f(int m,int n)
{
int ret;
int k;
int b;
if(n==1)
{
return (1<
}
else{
ret=f(m,n-1);
k=(n-1)/m;
b=(n-1)%m;
if(b==0)
ret=ret-(1<<(k-1))+(1<
else
ret=ret-(1<<(m+k-b))+(1<<(m+k-b+1));
return ret;
}
}
Can some one explain
if(b==0)
ret=ret-(1<<(k-1))+(1<
else
ret=ret-(1<<(m+k-b))+(1<<(m+k-b+1));
for me?
Many thanks! | O*******d 发帖数: 20343 | 2 You may print the output integer in binary format to see the result. | n*c 发帖数: 228 | 3 Is there any intuitive explanation for those two lines involving bitwise
shift?
【在 O*******d 的大作中提到】 : You may print the output integer in binary format to see the result.
| m********a 发帖数: 1312 | 4 没溢出的前提下,
ret-(1<<(k-1))+(1< | r*********r 发帖数: 3195 | 5 是面试题吗? 是不是叫你笔算 f(m,n), 给定某个 m 和 n?
这种题不能一步一步算, 要先算算共递归多少步, 找出规律,
就能算出解的表达式. |
|