zoukankan      html  css  js  c++  java
  • HDU 1023 Train Problem II

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1023

    解题报告:就是求第n个卡特兰数是多少,不过这个n的范围有点大1到100,所以还要用到高精度,其实这题也就是纯粹的高精度加法跟乘法结合起来。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 using namespace std;
     5 
     6 const int MAX = 1000+5;
     7 typedef  __int64 INT;
     8 char dp[105][10000];
     9 
    10 void sum(char* s1,char* s2) {
    11     int len1 = strlen(s1);
    12     int len2 = strlen(s2);
    13     int a1[MAX],a2[MAX];
    14     memset(a1,0,sizeof(a1));
    15     memset(a2,0,sizeof(a2));
    16     for(int i = 0;i<len1;++i)
    17     a1[i] = s1[i] - '0';
    18     for(int i = 0;i<len2;++i)
    19     a2[i] = s2[i] - '0';
    20     int m = max(len1,len2);
    21     for(int i = 0;i<m;++i) {
    22         a1[i] += a2[i];
    23         a1[i+1] += (a1[i]/10);
    24         a1[i]%=10;
    25     }
    26     if(a1[m] != 0)
    27     m++;
    28     strcpy(s1,"");
    29     for(int i = 0;i<m;++i)
    30     s1[i] = a1[i]+'0';
    31     s1[m] = NULL;
    32 }
    33 
    34 char *mult(char* s1,char* s2) {
    35     char s4[MAX],*s3 = new char[MAX];
    36     strcpy(s3,"0");
    37     int len1 = strlen(s1);
    38     int len2 = strlen(s2);
    39     for(int i = 0;i<len2;++i) {
    40         for(int j = 0;j<i;++j)
    41         s4[j] = '0';
    42         int a[MAX];
    43         memset(a,0,sizeof(a));
    44         for(int j = 0;j<len1;++j)
    45         a[j] = s1[j] - '0';
    46         int d = s2[i]-'0';
    47         for(int j = 0;j<len1;++j)
    48         a[j] *= d;
    49         for(int j = 0;j<len1;++j) {
    50             a[j+1] += (a[j]/10);
    51             a[j] %= 10;
    52         }
    53         int m = len1;
    54         if(a[m] != 0)
    55         m++;
    56         for(int j = 0;j<m;++j)
    57         s4[j+i] = a[j]+'0';
    58         s4[m+i] = NULL;
    59         sum(s3,s4);
    60     }
    61     return s3;
    62 }
    63 void dabiao() {
    64     strcpy(dp[1],"0");
    65     strcpy(dp[2],"1");
    66     strcpy(dp[3],"1");
    67     for(int i = 4;i <= 102;++i)
    68     {
    69         char str3[1000] = "0";
    70         for(int j = 2;j <= i - 1;++j)
    71         sum(str3,mult(dp[j],dp[i-j+1]));
    72         strcpy(dp[i],str3);
    73     }
    74 }
    75 
    76 int main( ) {
    77     int n;
    78     dabiao();
    79     while(scanf("%d",&n)!=EOF)
    80     {
    81         int len = strlen(dp[n+2]);
    82         for(int i = len-1;i>=0;--i)
    83         printf("%c",dp[n+2][i]);
    84         puts("");
    85     }
    86     return 0;
    87 }
    View Code
  • 相关阅读:
    python的高级特性
    python方向
    快速搜索
    计算机组成原理——总线
    计算机组成原理——指令系统
    计算机组成原理——cpu
    计算机组成原理——2
    git提交时报错处理办法
    快速的在linux服务器上安装jdk8
    python的包管理软件Conda的用法
  • 原文地址:https://www.cnblogs.com/xiaxiaosheng/p/3324691.html
Copyright © 2011-2022 走看看