洛谷P1149 火柴棒等式

题目描述

输入格式

一个整数n(n<=24)。

输出格式

一个整数,能拼成的不同等式的数目。

输入输出样例
输入 #1
14
输出 #1
2
输入 #2
18
输出 #2
9
说明/提示

【输入数出样例1解释】

2个等式为0 + 1 = 1 和 1 + 0 = 1.

【输入输出样例2解释】
9个等式为:

0+4=4
0+11=11
1+10=11
2+2=4
2+7=9
4+0=4
7+2=9
10+1=11
11+0=11
分析

本题的算法是暴力枚举。确定合格的情况,就是a、b、c三者的火柴数目和正好等于要求的情况,且c=a+b。然后就是确定搜索范围,为了避免出现漏掉的情况,能取多大尽量取多大,a、b范围肯定超不过999。

#include <iostream>
using namespace std;
int NeedMatchNumber[10]={
   6,2,5,5,4,5,6,3,7,6};//不同数字对应的火柴数量
int count=0;
int CalculateMatchNumber(int n)//计算数字对应需要的火柴数目
{
   
    int sum=0;
    if(n==0)
        return NeedMatchNumber[0];
    while(n)
    {
   
        sum+=NeedMatchNumber[n%10];
        n/=10;
    };
    return sum;
}
int main()
{
   
    int match_number;//拥有火柴的数量
    cin >> match_number;
    match_number-=4;//减去+和=需要的火柴数
    for (int i = 0; i < 999; ++i) {
    //单个数不会超过80
        for (int j = 0; j < 999; ++j) {
   //要求:火柴全部用上
            if((CalculateMatchNumber(i)+CalculateMatchNumber(j)+CalculateMatchNumber(i+j))==match_number)
            {
   
                count++;
            }
        }
    }
    cout << count;
    return  0;
}
全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
04-08 00:50
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务