昨晚师兄去SJTU笔Baidu,结果被鄙了。。今年的经济危机把金融、IT类的就业形势搞得太烂了,很多像样的地方都不招人,NND。
回到主题,据说第一题大概是这样的:
这个描述转自水母 某帖 一台计算机,只能进行下列原子运算:赋值
+1操作
固定次数的循环
只能操作零和正整数
不会溢出
请用伪代码分别实现加减乘除的操作
关键的困难在于,怎样用+1操作实现减法和除法。我昨晚怎么也没想出来。
刚才看到水母上帖子的讨论,搞明白-1操作怎么做了,昨天还在想对机器字长做些假设然后求补码。。
不过,这种考查真的有意义么?就算短时间内写出来说明什么能力了?算了。。至少比那无尽的sizeof题目要好~
人生啊,上代码:
猛击加号展开!
def add1(x): # O(1)
return (x + 1)
def add(x, y): # O(y)
for loop in range(0, y):
x = add1(x)
return x
def mul(x, y): # O(xy)
ret = 0
for loop in range(0, y):
ret = add(ret, x)
return ret
def sub1(x): # O(x)
"Notice sub1(0) is always 0"
ret = tmp = 0;
for loop in range(0, x):
ret = tmp
tmp = add1(tmp)
return ret
def sub(x, y): # O(xy)
"Since sub1(0) is 0, if x < y, sub(x, y) will be 0"
for loop in range(0, y):
x = sub1(x)
return x
def sgn(x): # O(x)
"sgn(x) is 0 if x==0 and 1 if x>0"
ret = 0
for loop in range(0, x):
ret = 1
return ret
def div(x, y): # O(x(xy+x)) worst case
"Assume a positive number divides by 0 is still that number"
quotient = 0
tmp = add1(x) # x++, in case y divides x so the remainder for the loop
# in which tmp == y is 0, but it SHOULD be counted a '1' in the quotient
for loop in range(0, x): # if x >= y, there should be at most x outer loops
# and when tmp < y, sub(tmp, y) is 0
# if x < y, sub(tmp, y) is always 0
tmp = sub(tmp, y)
quotient = add(quotient, sgn(tmp))
# every time the reminder still greater than 0, quotient++
return quotient
def lt_or_eq(x, y): # O(x)
return sgn(sub(x, y)) == 0
def gt(x, y): # O(x)
return sgn(sub(x, y)) > 0
def add1(x): # O(1)
return (x + 1)
def add(x, y): # O(y)
for loop in range(0, y):
x = add1(x)
return x
def mul(x, y): # O(xy)
ret = 0
for loop in range(0, y):
ret = add(ret, x)
return ret
def sub1(x): # O(x)
"Notice sub1(0) is always 0"
ret = tmp = 0;
for loop in range(0, x):
ret = tmp
tmp = add1(tmp)
return ret
def sub(x, y): # O(xy)
"Since sub1(0) is 0, if x < y, sub(x, y) will be 0"
for loop in range(0, y):
x = sub1(x)
return x
def sgn(x): # O(x)
"sgn(x) is 0 if x==0 and 1 if x>0"
ret = 0
for loop in range(0, x):
ret = 1
return ret
def div(x, y): # O(x(xy+x)) worst case
"Assume a positive number divides by 0 is still that number"
quotient = 0
tmp = add1(x) # x++, in case y divides x so the remainder for the loop
# in which tmp == y is 0, but it SHOULD be counted a '1' in the quotient
for loop in range(0, x): # if x >= y, there should be at most x outer loops
# and when tmp < y, sub(tmp, y) is 0
# if x < y, sub(tmp, y) is always 0
tmp = sub(tmp, y)
quotient = add(quotient, sgn(tmp))
# every time the reminder still greater than 0, quotient++
return quotient
def lt_or_eq(x, y): # O(x)
return sgn(sub(x, y)) == 0
def gt(x, y): # O(x)
return sgn(sub(x, y)) > 0
BTW:第二题好像是设计和数据库查询相关的数据结构,第三题貌似是一道很综合的题,可爱的师兄说记不清了。。O_O