由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 大家看看这几道亚麻面试题怎么做?
相关主题
请教几道对我来说高深的面试题看到一个c的面试题,求教。
问一个关于xor的题问几道面试题
probably XOR problem恭喜几道面试题
几道微软面试题今天一道面试题主动跪了
请教一个面试题Facebook Hacker Cup
Careercup上看到的一个google的题目 下面有个人回复很好玩面试题
看一道面试题Given an array of N integers from range [0, N] and one is missing. Find the missing number.
几道面试题职业杯另外一道
相关话题的讨论汇总
话题: here话题: proof话题: expressed话题: unsigned话题: sum
进入JobHunting版参与讨论
1 (共1页)
h******8
发帖数: 55
1
1. 设计一个算法,判断一个integer n是不是可以表示成k(k>=2)个连续正整数的和
2. implement adding two unsigned numbers without using "+"
3. implement sqrt
多谢
j*****u
发帖数: 1133
2
1. 除了2^N外的数都可以
2. a+b = (a XOR b) + (a AND b)<<1,递归
3. binary search, sqrt(n): min=1, max=n,循环计算mid^2直到它接近n

【在 h******8 的大作中提到】
: 1. 设计一个算法,判断一个integer n是不是可以表示成k(k>=2)个连续正整数的和
: 2. implement adding two unsigned numbers without using "+"
: 3. implement sqrt
: 多谢

g*****k
发帖数: 623
3
n=k*m+(k-1)*k/2, 2<=k
【在 h******8 的大作中提到】
: 1. 设计一个算法,判断一个integer n是不是可以表示成k(k>=2)个连续正整数的和
: 2. implement adding two unsigned numbers without using "+"
: 3. implement sqrt
: 多谢

g*****k
发帖数: 623
4

This is interesting. How to prove it?
It seems it is easy to show the prime case.

【在 j*****u 的大作中提到】
: 1. 除了2^N外的数都可以
: 2. a+b = (a XOR b) + (a AND b)<<1,递归
: 3. binary search, sqrt(n): min=1, max=n,循环计算mid^2直到它接近n

g*****k
发帖数: 623
5
all right, I got the proof.
yeah, only 2^m cannot be expressed as a sum of k consequtive integers.

【在 g*****k 的大作中提到】
:
: This is interesting. How to prove it?
: It seems it is easy to show the prime case.

s*******e
发帖数: 93
6
For 3 you can use Newton–Raphson method to solve x^2=n and get a more exact
answer.
x*****p
发帖数: 1707
7
It is not correct.
Here is simple example, n = 10, k = 3, then n can not be expressed as
the sum of 3 consecutive integers.
Here is the solution.
Suppose n = m + (m+1) + ... + (m+k-1) = km + k(k-1)/2
We only need to calculate n - k(k-1)/2 and check if it is a multiple of
k.
However, the problem can have another meaning. That is, k is not given
and can be any positive integer. then only 2^m can not be expressed as a
sum of k consecutive integers for any given k>1.
Here is a proof. If n is odd, then n = 2m+1 = m + (m+1). If n is even
but not 2^m, then n = 2^t * r, where r is odd. Let r = 2s + 1, then we
have
n = (2^t - s) + (2^t - s + 1) + ... + (2^t) + (2^t + 1) + ... + (2^t +
s).

【在 g*****k 的大作中提到】
: all right, I got the proof.
: yeah, only 2^m cannot be expressed as a sum of k consequtive integers.

g*****k
发帖数: 623
8
k can be any number >=2.
so for n=10, it can be expressed as 1234.

【在 x*****p 的大作中提到】
: It is not correct.
: Here is simple example, n = 10, k = 3, then n can not be expressed as
: the sum of 3 consecutive integers.
: Here is the solution.
: Suppose n = m + (m+1) + ... + (m+k-1) = km + k(k-1)/2
: We only need to calculate n - k(k-1)/2 and check if it is a multiple of
: k.
: However, the problem can have another meaning. That is, k is not given
: and can be any positive integer. then only 2^m can not be expressed as a
: sum of k consecutive integers for any given k>1.

g*********s
发帖数: 1782
9

if and only if there exists m >= 0 and n > 0 such that t = 2^m*(2n+1)?
just follow the hardware implementation of int adder.
numeric method. use binary search or newton?

【在 h******8 的大作中提到】
: 1. 设计一个算法,判断一个integer n是不是可以表示成k(k>=2)个连续正整数的和
: 2. implement adding two unsigned numbers without using "+"
: 3. implement sqrt
: 多谢

g*********s
发帖数: 1782
10

u still use "+".

【在 j*****u 的大作中提到】
: 1. 除了2^N外的数都可以
: 2. a+b = (a XOR b) + (a AND b)<<1,递归
: 3. binary search, sqrt(n): min=1, max=n,循环计算mid^2直到它接近n

相关主题
Careercup上看到的一个google的题目 下面有个人回复很好玩看到一个c的面试题,求教。
看一道面试题问几道面试题
几道面试题恭喜几道面试题
进入JobHunting版参与讨论
C*****n
发帖数: 1872
11
(a&b)<<1 will reach 0 through iteration or recursion, which means a^b is the
sum

【在 g*********s 的大作中提到】
:
: u still use "+".

J*********r
发帖数: 5921
12
I think what he actually meant is like below since he mentioned recursion.
unsigned add(unsigned a, unsigned b){
if(b==0)
return a;
return add(a^b, (a&b)<<1);
}
(a&b)<<1 stores the carries and will be ultimately left-shifted out of all
bits, leaving a 0, which is the base case here.

【在 g*********s 的大作中提到】
:
: u still use "+".

J*********r
发帖数: 5921
13
zan, I considered both cases too...got a little confused at first, but I
think the 2nd one should be the intended case.

【在 x*****p 的大作中提到】
: It is not correct.
: Here is simple example, n = 10, k = 3, then n can not be expressed as
: the sum of 3 consecutive integers.
: Here is the solution.
: Suppose n = m + (m+1) + ... + (m+k-1) = km + k(k-1)/2
: We only need to calculate n - k(k-1)/2 and check if it is a multiple of
: k.
: However, the problem can have another meaning. That is, k is not given
: and can be any positive integer. then only 2^m can not be expressed as a
: sum of k consecutive integers for any given k>1.

g*********s
发帖数: 1782
14
yes, this is much more clear.

recursion.
all

【在 J*********r 的大作中提到】
: I think what he actually meant is like below since he mentioned recursion.
: unsigned add(unsigned a, unsigned b){
: if(b==0)
: return a;
: return add(a^b, (a&b)<<1);
: }
: (a&b)<<1 stores the carries and will be ultimately left-shifted out of all
: bits, leaving a 0, which is the base case here.

B********n
发帖数: 384
15
This is good but not accurate proof. All numbers are supposed to be positibe
, that is not the case in your proof.
n = (2^t - s) + (2^t - s + 1) + ... + (2^t) + (2^t + 1) + ... + (2^t +
s).

【在 x*****p 的大作中提到】
: It is not correct.
: Here is simple example, n = 10, k = 3, then n can not be expressed as
: the sum of 3 consecutive integers.
: Here is the solution.
: Suppose n = m + (m+1) + ... + (m+k-1) = km + k(k-1)/2
: We only need to calculate n - k(k-1)/2 and check if it is a multiple of
: k.
: However, the problem can have another meaning. That is, k is not given
: and can be any positive integer. then only 2^m can not be expressed as a
: sum of k consecutive integers for any given k>1.

l*********r
发帖数: 674
16
这种鸟题如果事先没看过,面试问到了还真是眼前一黑阿。
有没有什么解这类题的general的idea么?

【在 J*********r 的大作中提到】
: I think what he actually meant is like below since he mentioned recursion.
: unsigned add(unsigned a, unsigned b){
: if(b==0)
: return a;
: return add(a^b, (a&b)<<1);
: }
: (a&b)<<1 stores the carries and will be ultimately left-shifted out of all
: bits, leaving a 0, which is the base case here.

g*********s
发帖数: 1782
17
you may not be asked to write the code in 5 minutes. but you definitely
need to know how the addition is implemented by bit operation, as this is
the basic concepts in an entry level computer hardware course like cs 101.

【在 l*********r 的大作中提到】
: 这种鸟题如果事先没看过,面试问到了还真是眼前一黑阿。
: 有没有什么解这类题的general的idea么?

1 (共1页)
进入JobHunting版参与讨论
相关主题
职业杯另外一道请教一个面试题
求教一道面试题Careercup上看到的一个google的题目 下面有个人回复很好玩
问个最近面试里的题目看一道面试题
两个Amazon面试题几道面试题
请教几道对我来说高深的面试题看到一个c的面试题,求教。
问一个关于xor的题问几道面试题
probably XOR problem恭喜几道面试题
几道微软面试题今天一道面试题主动跪了
相关话题的讨论汇总
话题: here话题: proof话题: expressed话题: unsigned话题: sum