10317-Equating Equations

Source: Internet
Author: User

/*

A search question requires pruning to convert equation a + B = c + d to a + B-c-d to calculate the sum of all values. If it is not an even number, no is output directly.

Calculate the number of the plus signs (lp), that is, the sum of the number of lp + 1, and search.

Dfs requires several layers of pruning: if the current tsum = sum/2, perform the next layer of Search

*/

# Include <cstdio>

# Include <cstring>
# Include <cstdlib>


Char s [11000], char [1100];
Int num [1100], a [1100], B [1010], vis [1100];
Int sum, lp, ln, po;
Int change ()
{
Int l = strlen (s );
Ln = 0;
Sscanf (s, "% d", & num [ln ++]);
Sum = num [0];
For (int I = 1; I <l; I ++)
{
If (s [I] = '| s [I] ='-'| s [I] =' + ')
{
If (s [I] = ') po = ln;
Listen [ln] = s [I];
Sscanf (s + I + 1, "% d", & num [ln]);
Sum + = num [ln ++];
}
}
Lp = 1;
For (int I = 1; I <ln; I ++)
If (I <po & signature [I] = '+ ')
Lp ++;
Else if (I> po & products [I] = '-')
Lp ++;
}
Int dfs (int cur, int pos, int tsum)
{
If (lp-cur> ln-pos)
Return 0;
If (cur = lp)
{
If (tsum * 2 = sum)
Return 1;
Return 0;
}
If (pos <ln)
{
If (tsum + num [pos]) * 2 <= sum)
{
Vis [pos] = 1;
B [cur] = num [pos];
If (dfs (cur + 1, pos + 1, tsum + num [pos])
Return 1;
}
}
Vis [pos] = 0;
If (pos <ln & dfs (cur, pos + 1, tsum ))
Return 1;
Return 0;
}


Int main ()
{
While (gets (s ))
{
Memset (vis, 0, sizeof (vis ));
Change ();
If (sum % 2 = 0 & dfs (0, 0 ))
{
Int la = 1;
A [0] = B [0];
For (int I = 1; I <ln; I ++)
{
If (I <po & signature [I] = '+ ')
A [I] = B [la ++];
Else if (I> po & products [I] = '-')
A [I] = B [la ++];
}
Int k = 1;
For (int I = 0; I <ln; I ++)
If (! Vis [I])
{
While (k <po & signature [k] = '+' & k <ln) ++ k;
While (k> po & signature [k] = '-' & k <ln) ++ k;
A [k ++] = num [I];
}
Printf ("% d", a [0]);
For (int I = 1; I <ln; I ++)
Printf ("% c % d", outputs [I], a [I]);
Printf ("\ n ");
}
Else
Puts ("no solution ");
}
Return 0;
}
 

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.