著名的斐波那契数列:Fn = Fn-1 + Fn-2 (F1=F2=1),前几个斐波那契数为,1,1,2,3,5,8,13,21….
某一个整数可以拆成若干彼此不同的斐波那契数之和,比如13=13,13=5+8,13=2+3+8;16=1+2+13,16=1+2+5+8,16=3+13,16=3+5+8
现在求解这样一个问题,给定某个数值n,求有多少种方案可以把该数分解成若干彼此不同的斐波那契数之和。
求各位大神帮忙,明天就要交代码了,现在还没思路。。。可以用数组存储吗?
斐波那契数列的应用问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答
- devmiao 2014-12-14 18:02关注
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int n = 13; foreach (var item in Resolve(Fibonacci(n).ToArray(), new int[] { 0 }, n)) Console.WriteLine(string.Join(" + ", item.Select(x => x.ToString())) + " = " + n.ToString()); } static IEnumerable<IEnumerable<int>> Resolve(IEnumerable<int> source, IEnumerable<int> cur, int seed) { if (seed == cur.Sum()) return new List<IEnumerable<int>> { cur.Where(x => x > 0) }; return source.Except(cur) .Where(x => x + cur.Sum() <= seed && x > cur.Last()) .SelectMany(x => Resolve(source, cur.Concat(new int[] { x }), seed)); } static IEnumerable<int> Fibonacci(int max) { int m = 1, n = 1; yield return m; while (n <= max) { yield return n; n = n + m; m = n - m; } } } }
2 + 3 + 8 = 13
5 + 8 = 13
13 = 13
Press any key to continue . . .解决 无用评论 打赏 举报
悬赏问题
- ¥50 有数据,怎么建立模型求影响全要素生产率的因素
- ¥50 有数据,怎么用matlab求全要素生产率
- ¥15 TI的insta-spin例程
- ¥15 完成下列问题完成下列问题
- ¥15 C#算法问题, 不知道怎么处理这个数据的转换
- ¥15 YoloV5 第三方库的版本对照问题
- ¥15 请完成下列相关问题!
- ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
- ¥15 求daily translation(DT)偏差订正方法的代码
- ¥15 js调用html页面需要隐藏某个按钮