Answers
int fuck(int length)
{
if(length <0)
{
return 0;
}
if(length == 1 || length == 0)
return 1;
return fuck(length-1)+fuck(length-2)+fuck(length-3);
}
指数的时间复杂度,因为产生了重复计算,按照斐波那契数列的方法,改成迭代或者在递归函数里多传点参数,时间复杂度能变成线性的。
艹,看错题目了,将错就错
void p(const list<int> &state)
{
for(auto it=state.begin();it!=state.end();it++)
if(it != --state.end())
cout<<*it<<"m,";
else
cout<<*it<<'m';
cout<<endl;
}
list<int> append(list<int> m,int k)
{
m.push_back(k);
return m;
}
int cfuck(int length,list<int> state)
{
if(length < 0){
return 0;
}
if(length == 0) {
p(state);
return 1;
}
return cfuck(length-1,append(state,1))+cfuck(length-2,append(state,2))+cfuck(length-3,append(state,3));
}
哎,C++知识全忘光了(反正我也进不了雅虎)。不过这道题目无论怎么样时间复杂度都是指数级的,所以就无所谓在最短的时间内了
Easter
answered 10 years, 5 months ago
用C写了一段:
unsigned int count(unsigned int n)
{
switch(n) {
case 0: return 0;
case 1: return 1;
case 2: return 2;
case 3: return 4;
default: return (count(n - 1) + count(n - 2) + count(n - 3));
}
return 0;
}
运行结果:
n = 0; t = 0
n = 1; t = 1
n = 2; t = 2
n = 3; t = 4
n = 4; t = 7
n = 5; t = 13
n = 6; t = 24
n = 7; t = 44
n = 8; t = 81
n = 9; t = 149
...
好吧,原来是要详细列出每种方法的方案,我没认真看…… =_,=b
liu-tc
answered 10 years, 5 months ago
根据 @AUV_503516 的思路, 写以下代码, 存储结构和空间还可以优化
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# @file robot_steps_calculator.py
# @author kaka_ace
# @date Mar 27 2015
# @breif
#
import copy
class RobotBrain(object):
CACHE_INIT = {
1: [['1']],
2: [['1', '1'], ['2']],
3: [['1', '1' ,'1'], ['1', '2'], ['2', '1'], ['3']],
}
CACHE_KEY_MAP_INIT = dict() # 缓存如 '11', '21' etc. 去重作用
for k, v in CACHE_INIT.items():
for l in v:
s = "".join(l)
CACHE_KEY_MAP_INIT[s] = True
def __init__(self):
self.cache = copy.deepcopy(self.CACHE_INIT)
self.cache_key_map = copy.deepcopy(self.CACHE_KEY_MAP_INIT)
def output_solution(self, n: "total length, positive number") -> None:
if n <= 3:
print(RobotBrain._ouput_result(self.cache[n]))
return
i = 4
while i <= n:
"""
f(i) = 1 + f(i-1)
= 2 + f(i-2)
= 3 + f(i-3)
= f(i-1) + 1
= f(i-2) + 2
= f(i-3) + 3
we need to remove duplicates
"""
self.cache[i] = []
for step in range(1, 4):
self._add_to_cache(1, i, True)
self._add_to_cache(2, i, True)
self._add_to_cache(3, i, True)
self._add_to_cache(1, i, False)
self._add_to_cache(2, i, False)
self._add_to_cache(3, i, False)
i += 1
self._ouput_result(self.cache[n])
def _add_to_cache(self, delta: int, i: int, reverse: bool) -> None:
for sl in self.cache[i-delta]:
s = ''.join(sl)
delta_str = str(delta)
if reverse is True:
# delta + f(i - delta)
new_key = delta_str + s
else:
# f(i - delta) + delta
new_key = s + delta_str
if new_key not in self.cache_key_map:
self.cache_key_map[new_key] = True
self.cache[i].append([delta_str] + sl)
@staticmethod
def _ouput_result(cache_list) -> None:
for cache_result in cache_list:
print(",".join([i+"m" for i in cache_result]))
if __name__ == "__main__":
r = RobotBrain()
r.output_solution(5)
本人极其猥琐
answered 10 years, 5 months ago
谢谢邀请。
出这道题,明显是为了考察你 DP。我也不多说,直接放码过来了:
cpp
void step(int n, std::string str) { if (n == 0) { std::cout << str << "\b " << std::endl; } if (n >= 1) step(n-1, str+"1m,"); if (n >= 2) step(n-2, str+"2m,"); if (n >= 3) step(n-3, str+"3m,"); }
当
n == 4
的时候,调用:
step(4, "");
原样输出你想要的。
这里只是用最短的代码表述我的思路,怕爆栈的,自行修改。
Mr.Rex
answered 10 years, 5 months ago