CSU 1374: Restore Calculation

来源:互联网 发布:电脑怎么截图淘宝客服 编辑:程序博客网 时间:2024/05/22 02:34

题意:

就是输入三行数据,使得第一行的数据加上第二行的数据等于第三行的。每组数据可能会有'?',代表一个未知的数,每个?代表的数字可以不一样,每行最多有50个字符,首字符不可为0。计算总共有多少种可能。



代码:

#include <cstdio>

#include <cstring>

#include <iostream>

#include <fstream>

using namespace std;


const int mod=1e9+7;

const int maxn=60;


char ch1[maxn],ch2[maxn],ch3[maxn];

int dp[maxn][2];


int main()

{

//freopen("test.txt","r",stdin);

    while(scanf("%s",ch1)&&ch1[0]!='0')

    {

        scanf("%s",ch2);

        scanf("%s",ch3);

        memset(dp,0,sizeof(dp));

        int len=strlen(ch1);

        dp[len][0]=1;  //为什么初始化这个没搞懂

        int i,j,k,l,s,m;

        for(i=len-1;i>=0;i--)

        {

            for(j=(i)?0:1;j<10;j++)  //i=0  j=1

            {

                if(ch1[i]-'0'!=j&&ch1[i]!='?')   continue;

                for(k=(i)?0:1;k<10;k++)

                {

                    if(ch2[i]-'0'!=k&&ch2[i]!='?')   continue;

                    for(l=0;l<2;l++)

                    {

                        s=(j+k+l)/10;   m=(j+k+l)%10;

                        if(m==ch3[i]-'0'||ch3[i]=='?')

                         dp[i][s]=(dp[i+1][l]+dp[i][s])%mod; //这个式子!!!

                    }

                }

            }

        }

        cout<<dp[0][0]<<endl;

    }//while

    return 0;

}

0 0
原创粉丝点击