CSU 1374: Restore Calculation

本文探讨了一种特定类型的加法谜题解决方法,通过解析输入的三行数据(包含未知数?),寻找所有可能的数字组合使得第一行加上第二行等于第三行。采用动态规划算法实现,并详细展示了核心代码逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

就是输入三行数据,使得第一行的数据加上第二行的数据等于第三行的。每组数据可能会有'?',代表一个未知的数,每个?代表的数字可以不一样,每行最多有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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值