POJ 2506 递归 + 高精度

博客介绍了如何使用递归和高精度解决POJ 2506问题,作者最初尝试二维数组方法但参数管理复杂,后来通过观察他人代码改用结构体数组,实现了清晰的解决方案。递归公式为f(n) = 2 * f(n - 2) + f(n - 1),博主请教理论依据。
摘要由CSDN通过智能技术生成

     这题一开始自己做的是用二维数组,结果发现传参弄的太乱了 。。

      直接看人代码了,恍然大悟啊。。怎么就没想到用结构体数组诶……

       直接贴人代码了

       递归加高精度。。

       规律是算前几个发现的, 可是硬是从理论方面,不知道怎么得来的。。。知道的大神们求教下。。。

       f(n) = 2 * f(n -2) + f(n - 1)

 

//poj2506
#include <iostream>
#include <algorithm>
using namespace std;
struct numtype {
       int len,a[1000];
}dp[252];
int n;
void add(numtype a, numtype b, numtype &c) {
     int i,len=max(a.len,b.len);
     for (i=1;i<=len;i++)
         c.a[i]=a.a[i]+b.a[i];
     for (i=1;i<=len;i++)
         if (c.a[i]>9) {
                       c.a[i]-=10;
                       c.a[i+1]++;
         }
     if (c.a[len+1]!=0) c.len=len+1;
     else c.len=len;
}
void cheng2(numtype &x) {
     int i;
     for (i=1;i<=x.len;i++)
         x.a[i]*=2;
     for (i=1;i<=x.len;i++)
         if (x.a[i]>9) {
                       x.a[i]-=10;
                       x.a[i+1]++;
         }
     if (x.a[x.len+1]!=0) x.len++;
}
int main() {
    int i;
    while (cin >> n) {
          memset(dp,0,sizeof(dp));
          dp[0].len=1;
          dp[0].a[1]=1;
          dp[1].len=1;
          dp[1].a[1]=1;
          for (i=2;i<=n;i++) {
                   cheng2(dp[i-2]);
                   add(dp[i-2],dp[i-1],dp[i]);
          }
          for (i=dp[n].len;i>=1;i--) cout << dp[n].a[i];
          cout << endl;
    }
    //system("pause");
    return 0;
} 

 

 

http://blog.csdn.net/lencle/article/details/6229589

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值