Title Description: http://www.oj.swust.edu.cn/problem/show/2469
Description
Recently, little Y has been fascinated by mathematics and is always thinking about various mathematical problems. One day, he accidentally spilled ink on the draft paper. What he can see now is "2?3?1?4" (? Indicates a place to see). Small Y's memory is good, he knows: 1, each? Only one of the three symbols is "+", "-", "=". 2. There is a total of only one "=". 3, the original formula must be an equation. Like "2+3-1=4" now he suddenly wondered how many possibilities there were to meet the above 3 requirements.
Input
multiple sets of inputs. The first row of each group has a number n. Represents a small y from left to right and can see n numbers altogether. (2<=n<=15) The second row of each group has n digits. Indicate what these n numbers are. Ensure that each number is a non-negative integer and less than 10^7.
Output
For each group, the output line, which has only one number, indicates how many possibilities satisfy the test instructions.
42 3 1 441 1 1 1
26
Hint
There must be only one symbol between the numbers, and there is no sign in front of the first number.
AC Code:
1#include <stdio.h>2 3 inta[ -];4 Chars[ -];5 intN;6 intFlag;7 intans;8 9 voidDFS (intLevel//possibility of Dfs search +-= appearingTen { One A if(level==n-1) - { - intI=0, l=a[0],r=0; the - while(s[i]!='='&&i<n-1) { - - if(s[i]=='+') l+=a[i+1]; + Else if(s[i]=='-') l-=a[i+1]; - +i++; A at } - - while(i<n-1) { - - if(s[i]=='+') r+=a[i+1]; - Else if(s[i]=='-') r-=a[i+1]; in Elser=a[i+1]; - toi++; + - } the * if(L==r&&flag) ans++; $ Panax Notoginseng return; - the } + As[level]='+'; theDFS (level+1); + -s[level]='-'; $DFS (level+1); $ - if(flag)return; - thes[level]='='; -flag=1;WuyiDFS (level+1); theflag=0; - Wu } - About intMain () $ { - - while(SCANF ("%d", &n)! =EOF) - { A + inti; the -ans=0; $ the for(i=0; i<n;i++) scanf ("%d",&a[i]); the theDFS (0); the -printf"%d\n", ans); in the } the About return 0; the the}
2469: Little Y's Puzzle 1---swust Information Institute OJ