The garlic learns the arithmetic

Source: Internet
Author: User

1000ms 65536K garlic is really bad math, so the teacher put him in the dark room to let him practice behind closed doors. Teacher with him a piece of paper, the upper row of 1,2,3... n the number of n, separated by blanks in the middle. The teacher asked him to fill in the blanks with a plus or minus sign. He asked the garlic king to find out how many methods of adding operators make the entire expression a value of 0, and output all the schemes. For example, when N=7,1 2 3 4 5 67 in a row, a caret scheme is 1 +2-3+4-5-6+7=0。 is not very interesting, come and help the garlic June to solve this problem (*´▽ ')?? The input is a row that contains an integer n (3≤n≤9). The output is all inserted between each pair of digits "+ "or"-"Then you can get and zero the sequence of numbers and follow the dictionary (ASCII) order. If there is no solution, the output line is none. Do not know the dictionary sequence and ASCII also does not matter, we see the sample output is clear, 1 to N in a row, first priority in each position"+", then put"-", the reason for this is because"+"The ASCII code is better than"-"Small. Sample 1 Input:7Output:1+2-3+4-5-6+71+2-3-4+5+6-71-2+3+4-5+6-71-2-3-4-5+6+7
TopicsAnalysis and follow the steps to achieve, write code avoid impatience!
#include <bits/stdc++.h>using namespacestd;intN//save read-in integer nBOOLopr[Ten];//You can save all n-1 symbols with a bool array, we use true to denote the plus sign "+", and false to denote a minus "-". BOOLFound =false;//indicates if a set of solutions is found, and if not found, it needs to output "None" at the end.//A Dfs function that holds two states: deep represents the recursive depth, which is the symbol to the left of the first number of digits, and sum represents the value of the previous partial expression. //if the value of sum is exactly 0 after the enumeration is complete, the set of scenarios is output. We need to save the state with the OPR array during the search. voidDfsintDeepintsum) {    //printf ("sum=%d", sum); //please fill in the corresponding value in the conditional expression below.     if(Deep = =N) {if(Sum = =0) {found=true; //Please output the final scenario below, with the scheme format referenced in the topic description. //finally remember to change the line Oh.              for(inti =1; I < n;i++) {printf ("%d", i); if(opr[i]==0) printf ("%c", opr[i]+ $); Elseprintf ("%c", opr[i]+ the); } printf ("%d\n", N); }        return ; }    //please fill out the correct logic in the following statement. Opr[deep] =1; DFS ( deep+1, sum+deep+1); Opr[deep]=0; DFS ( deep+1, sum-deep-1);}//main function Please do it yourself oh, come on!intMain () {scanf ("%d",&N); DFS (1,1); if(found==false) printf ("%s\n","None"); return 0;}
Code

The garlic learns the arithmetic

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.