zoukankan      html  css  js  c++  java
  • poj 2506 Tiling(递推 大数)

    题目:http://poj.org/problem?id=2506

    题解:f[n]=f[n-2]*2+f[n-1],主要是大数的相加;

    以前做过了的

     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 int ans[251][260];//ans数组的第一个下标表示瓷砖数目,第二个表示对应下的方法数
     5 //数组是倒序存储 的
     6 int main()
     7 {
     8     int n, i, j, count, b, p;
     9     while (scanf("%d", &n) != EOF)
    10     {
    11         memset(ans, 0, sizeof(ans));
    12         ans[0][0] = 1;
    13         ans[1][0] = 1;
    14         ans[2][0] = 3;
    15         if (n <= 2)
    16             printf("%d
    ", ans[n][0]);
    17         else
    18         {
    19             count = 1;//count表示对应的瓷砖数目下的方法数的位数
    20             for (i = 3; i <= n; i++)
    21             {
    22                 b = 0;
    23                 p = 0;
    24                 for (j = 0; j < count; j++)//从个位开始每一位相加
    25                 {
    26                     p = ans[i-2][j]*2 + ans[i-1][j]+b;
    27                     ans[i][j] = p % 10;
    28                     b = p / 10;
    29                 }
    30                 if (b)//如果大于等于10位数加1
    31                 {
    32                     ans[i][count] = b;
    33                     count ++;
    34                 }
    35             }
    36             for (i = count-1; i >= 0; i--)//逆序输出
    37             {
    38                 printf("%d", ans[n][i]);
    39             }
    40             printf("
    ");
    41         }
    42     }
    43     return 0;
    44 }
  • 相关阅读:
    Pascal's Triangle
    Pascal's Triangle II
    贪心算法入门
    Jump Game
    Symmetric Tree
    Reverse Words in a String
    [BZOJ2342][Shoi2011]双倍回文
    [HDU3068]最长回文
    [POJ1984]Navigation Nightmare
    [BZOJ3295][Cqoi2011]动态逆序对
  • 原文地址:https://www.cnblogs.com/bfshm/p/3223855.html
Copyright © 2011-2022 走看看