hdu1023:Train Problem II 之大数相加和状态转移方程



Train Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5898    Accepted Submission(s): 3201


Problem Description
As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.
 

Input
The input contains several test cases. Each test cases consists of a number N(1<=N<=100). The input is terminated by the end of file.
 

Output
For each test case, you should output how many ways that all the trains can get out of the railway.
 

Sample Input
  
  
1 2 3 10
 

Sample Output
  
  
1 2 5 16796
Hint
The result will be very large, so you may not process it by 32-bit integers.
 

Author
Ignatius.L
 

Recommend
We have carefully selected several similar problems for you:   1130  1131  1134  2067  1267 
 
#include<iostream>
#include<algorithm>
using namespace std;

const int M = 101;
char f[M][M][M];
int Judge(char ch[])
{//判断字符串ch是否全为,若全为,返回,否则返回
    int i, k;
    k = strlen(ch);

    for (i = 0; i<k; i++)
    if (ch[i] != '0')
        return 0;
    return 1;
}
void BigNumberAdd(char a1[], char b1[])
{
    int i, j, k, lena, lenb;
    int a[1000] = { 0 }, b[1000] = { 0 }, d[1000] = { 0 };

    lena = strlen(a1);
    lenb = strlen(b1);

    //将加数与被加数化为整型数组,并且该数组的其他位为0
    for (i = 0; i<lena; i++)
        a[i] = a1[lena - i - 1] - '0';
    for (i = 0; i<lenb; i++)
        b[i] = b1[lenb - 1 - i] - '0';

    //当数组除了加数和被加数以外的整型数组元素均为时,无需考虑lena和lenb的大小
    k = lena>lenb ? lena : lenb;
    for (i = 0; i<k; i++)
    {
        d[i] = a[i] + b[i] + d[i];
        d[i + 1] = d[i + 1] + d[i] / 10;
        d[i] = d[i] % 10;
    }
    //若高位进
    while (d[k])
        k++;
    while (!d[k - 1])
        k--;//001+0003=4
    //将整型数组逆着转变并赋值给c字符型数组
    for (j = 0; j<k; j++)
        a1[j] = d[k - j - 1] + '0';
    a1[k] = '\0';
    if (Judge(a1))//若全为,则只输出一个
    {
        a1[0] = '0';
        a1[1] = '\0';
    }
}
int main(){
    int n;
    int i, j;
    int k;
    for (i = 0; i < M; i++){
        for (j = 0; j < M;j++)
        for (k = 0; k < M; k++){
            f[i][j][k] = '\0';
        }
    }
    f[1][1][0] = '1';
    f[1][1][1] = '\0';
    for (i = 0; i < M; i++){
        f[i][0][0] = '1';
        f[i][0][1] = '\0';
    }
    for (i = 2; i < M; i++){
        for (j = 0; j <=i; j++){
            if (i != j){
                strcpy(f[i][j],f[i - 1][j]);
                if (j>0)
                    BigNumberAdd( f[i][j],f[i][j - 1]);

            }
            else{
                strcpy( f[i][j], f[i - 1][j - 1]);
                if (j > 1)
                    BigNumberAdd(f[i][j] , f[i][j - 2]);
            }

        }
    }
   
    while (cin >> n){
        int i;
        for (i = 0; i < strlen(f[n][n]); i++){
            cout << f[n][n][i];
        }
        cout << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值