zoukankan      html  css  js  c++  java
  • 洛谷-P1149 火柴棒等式

    题目描述

    给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A、B、C是用火柴棍拼出的整数(若该数非零,则最高位不能是0)。用火柴棍拼数字0-9的拼法如图所示:

    注意:

    1. 加号与等号各自需要两根火柴棍

    2. 如果A≠B,则A+B=C与B+A=C视为不同的等式(A、B、C>=0)

    3. n根火柴棍必须全部用上

    输入输出格式

    输入格式:

    输入文件matches.in共一行,又一个整数n(n<=24)。

    输出格式:

    输出文件matches.out共一行,表示能拼成的不同等式的数目。

    输入输出样例

    输入样例#1:
    样例输入1:
    14
    样例输入2:
    18
    输出样例#1:
    样例输出1:
    2
    样例输出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

    回溯,之前的回溯知识又快记不起来了,这道题可以巩固一下:

    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #define N 1100
    using namespace std;
    
    int a[N] = {6,2,5,5,4,5,6,3,7,6};
    int b[4];
    int n;
    int tot = 0;
    
    void dfind(int pos){
        for(int i=0;i<=N-1;i++){
            if(n - a[i] >= 0){
                n = n - a[i];
                b[pos] = i;
                if(pos == 3){
                    if(b[1] + b[2] == b[3] && n == 0){
                        tot++;
                    }
                }else{
                    dfind(pos+1);
                }
                n += a[i];
            }
        }
    }
    
    
    int main(void){
        cin >> n;
        n = n - 4;
        for(int i=10;i<=N-1;i++){
            a[i] = a[i/10] + a[i%10];
        }
        dfind(1);
        cout << tot << endl;
    }
  • 相关阅读:
    Codeforces Round #620 (Div. 2)
    Codeforces Round #575 (Div. 3)
    Codeforces Round #619 (Div. 2)
    2014 Nordic Collegiate Programming Contest
    Educational Codeforces Round 82 (Rated for Div. 2)
    模板
    2015-2016 ACM-ICPC Southwestern Europe Regional Contest (SWERC 15)
    模板
    Codeforces Round #618 (Div. 2)
    Codeforces Round #343 (Div. 2)
  • 原文地址:https://www.cnblogs.com/doubest/p/11852886.html
Copyright © 2011-2022 走看看