Equation Construction (equation) The third problem of Tsinghua's push to avoid vitality

Source: Internet
Author: User

Equation Construction (equation)
"Problem description"
n numbers between 1 and 9 are written in a row in a certain order, requiring that an equal sign and any number of plus signs be inserted to form an equation.
For example, if n=5, the 5 numbers given are 1, 2, 3, 6, 9, then the following equation can be constructed:
12+3=6+9
Sometimes there are more than one equation that can be constructed, for example, when n=4, if the given number is 1, 2, 1, 2, then the following 2 equations can be constructed:
12=12
1+2=1+2
In some cases, it is not possible to form an equation, for example, when n=4, if the given number is 1, 2, 3, 4, it cannot constitute an equation.
"Input Data"
Input file: equation.in
First line: There is an integer N, which represents the number of digits (1<n<=18)
The second line: give the n integers in turn, all of them between 1 and 9.
"Input Data Sample"
===== Example 1 input =====
5
1 2 3) 6 9
===== Example 2 input =====
4
1 2 1 2
===== Example 3 input =====
4
1 2 3 4
"Output Data"
Output file: Equation.out
Only one integer is output, representing the number of equations that can be formed by this column number.
"Output Data Sample"
===== Example 1 output =====
1
===== Example 2 output =====
2
===== Example 3 output =====
0
"Time Space Limit"
Time: 1S
Space: 256M


==========================================================

What I'm thinking is that this is a search question that requires inserting an equal sign and any number of plus signs to make it an equation,

Then there are three choices in each gap before you join the equals sign, one is insert =, one is not inserted, and the other is insert +, if the equal sign is already inserted then the gap in the back can only be inserted + or nothing is inserted.

However, pruning is used to reduce unnecessary searches and thus increase the speed of operation. Since all numbers are between 1 and 9, assuming that the data in the first K (k<n) data is the same as the data that is already less than the right of the equals sign, then this equation must not be satisfied, so it can be pruned.

Well, say too much is not good, or directly to the program code, we understand it well.

#include <cstdio> #include <algorithm> #include <math.h>using namespace Std;int val[20];int n,k;int tot = 0;void search2 (int ni,int lsum,int rsum,bool hasequal) {        if (ni>n)    {        lsum==rsum?tot++:1;        return;    }    if (lsum<=rsum)//pruning conditions can be placed here will have a great effect        return;    if (Hasequal==false)    {        //operator +        search2 (ni+1,lsum+val[ni],rsum,false);        operator =        SEARCH2 (ni+1,lsum,val[ni],true);        No operator        SEARCH2 (ni+1,lsum*10+val[ni],rsum,false);            }    else{        //operator+        search2 (ni+1,lsum,rsum+val[ni],true);        No operator        SEARCH2 (ni+1,lsum,rsum*10+val[ni],true);    }} int main () {    while (true)    {        scanf ("%d", &n);        tot=0;        for (int i=1;i<=n;i++)            scanf ("%d", val+i);        SEARCH2 (2,val[1],0,false);        printf ("%d\n", tot);    }    return 0;}


Equality constructs (equation) The third question of Tsinghua's push to avoid vitality

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.