P*******b 发帖数: 1001 | 1 Fibonacci Numbers: A number is said to be Fibonacci number if it follows the
fibonacci property. (Ex: 112, 1123, etc). But additionally, it need not nec
essarily start with 1, as with the normal fibonacci series. So, in this new
definition, 112(1,1,2) is a fibonacci number and so is 121224(12,12,24), and
so is 252550(25,25,50).
So, given any two numbers as input, print out all the Fibonacci Numbers with
in that range.. | c**********e 发帖数: 2007 | 2
the
nec
new
and
with
Which range?
【在 P*******b 的大作中提到】 : Fibonacci Numbers: A number is said to be Fibonacci number if it follows the : fibonacci property. (Ex: 112, 1123, etc). But additionally, it need not nec : essarily start with 1, as with the normal fibonacci series. So, in this new : definition, 112(1,1,2) is a fibonacci number and so is 121224(12,12,24), and : so is 252550(25,25,50). : So, given any two numbers as input, print out all the Fibonacci Numbers with : in that range..
| s*********t 发帖数: 1663 | 3 given (a,b)
for(i=1; ; i++){
//if "ii(i+i)" is larger than b, break
//else, find all fib numbers start with i, till it's larger than b, put
those larger than a into a set
}
the
nec
new
and
with
【在 P*******b 的大作中提到】 : Fibonacci Numbers: A number is said to be Fibonacci number if it follows the : fibonacci property. (Ex: 112, 1123, etc). But additionally, it need not nec : essarily start with 1, as with the normal fibonacci series. So, in this new : definition, 112(1,1,2) is a fibonacci number and so is 121224(12,12,24), and : so is 252550(25,25,50). : So, given any two numbers as input, print out all the Fibonacci Numbers with : in that range..
| P*******b 发帖数: 1001 | 4 thanks
put
【在 s*********t 的大作中提到】 : given (a,b) : for(i=1; ; i++){ : //if "ii(i+i)" is larger than b, break : //else, find all fib numbers start with i, till it's larger than b, put : those larger than a into a set : } : : the : nec : new
| I**A 发帖数: 2345 | 5 能不能详细说说?
【在 P*******b 的大作中提到】 : thanks : : put
| s*********t 发帖数: 1663 | 6 我随便写了一个,凑合看下吧。。
std::set fibRange(int m, int n)
{
assert(m<=n);
std::set Set;
std::stringstream ss;
std::string s;
int n1,n2, tmp;
for(int i=1; ; i++){
n1 = i;
n2 = i+i;
ss.str("");
ss<
s = ss.str();
if(atoi(s.c_str()) > n){
break;
}
while( atoi(s.c_str()) < m){
tmp = n1 + n2;
n1 = n2;
n2 = tmp;
ss<
s = ss.str();
【在 I**A 的大作中提到】 : 能不能详细说说?
| I**A 发帖数: 2345 | 7 没有特别看明白
你看我理解的对不对啊?
就是说
对每一个i, we will check whether ii(i+i)是不是在range内,在的话就放进去
然后看ii(i+i)(i+i+i)在不在range内,在的话就放进去,直到>range为止,再检查下
一个i。。。
这个题目所谓的fibnumber 必须是start with two same numbers?
ij(i+j)算不算?
【在 s*********t 的大作中提到】 : 我随便写了一个,凑合看下吧。。 : std::set fibRange(int m, int n) : { : assert(m<=n); : std::set Set; : std::stringstream ss; : std::string s; : int n1,n2, tmp; : for(int i=1; ; i++){ : n1 = i;
| s*********t 发帖数: 1663 | 8 那样就无穷无尽了
【在 I**A 的大作中提到】 : 没有特别看明白 : 你看我理解的对不对啊? : 就是说 : 对每一个i, we will check whether ii(i+i)是不是在range内,在的话就放进去 : 然后看ii(i+i)(i+i+i)在不在range内,在的话就放进去,直到>range为止,再检查下 : 一个i。。。 : 这个题目所谓的fibnumber 必须是start with two same numbers? : ij(i+j)算不算?
| I**A 发帖数: 2345 | 9 哈哈哈,也是。。
不会无穷无尽的,有很多倒是真的。。
还有,直觉上觉得,因为有个start value参数
循环的时候,是不是不要从1开始更快些?
对于你的logic,我理解的没错?
【在 s*********t 的大作中提到】 : 那样就无穷无尽了
| s*********t 发帖数: 1663 | 10 没错
【在 I**A 的大作中提到】 : 哈哈哈,也是。。 : 不会无穷无尽的,有很多倒是真的。。 : 还有,直觉上觉得,因为有个start value参数 : 循环的时候,是不是不要从1开始更快些? : 对于你的logic,我理解的没错?
| I**A 发帖数: 2345 | 11 thanks
我自己又想了一下
是要从1开始
【在 s*********t 的大作中提到】 : 没错
| c********t 发帖数: 1756 | 12 while( atoi(s.c_str()) < m){
tmp = n1 + n2;
n1 = n2;
n2 = tmp;
ss<
s = ss.str();
}
这段代码看不出来有啥用,是否可以去掉?
while( atoi(s.c_str()) <= n && atoi(s.c_str())>m )
即可. | s*********t 发帖数: 1663 | 13 如果开头几个数如果小于m则进不去循环了
改的话可以这样:
去掉前面的while(...
在while(...
if( xxx > m ) xxx.insert(xxx)
即可
【在 c********t 的大作中提到】 : while( atoi(s.c_str()) < m){ : tmp = n1 + n2; : n1 = n2; : n2 = tmp; : ss<: s = ss.str(); : } : 这段代码看不出来有啥用,是否可以去掉? : while( atoi(s.c_str()) <= n && atoi(s.c_str())>m ) : 即可.
| x***y 发帖数: 633 | 14 what if the # of characters in b > the # of characaters in a? Then, even if
ii(i+i)> the first correponding characters in b, we can not eliminate it...
put
【在 s*********t 的大作中提到】 : given (a,b) : for(i=1; ; i++){ : //if "ii(i+i)" is larger than b, break : //else, find all fib numbers start with i, till it's larger than b, put : those larger than a into a set : } : : the : nec : new
|
|