Introduction to the course competition of garlic----a study of the arithmetic process of garlic

Source: Internet
Author: User

Garlic math is so bad, so the teacher put him into a small black house let him practice behind closed doors. The teacher and he a piece of paper, the top row of 1, 2, 3...N this n number, in the middle with white space separated. 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 N=7, 1 2 3 4 5 6 7 in a row, a caret scheme is 1+2-3+4-5-6+7=0. is not very interesting, come to help garlic June solve this problem (*´▽ ')??

The input is a row that contains an integer N (3≤n≤9).

The output is a sequence of all numbers that can be obtained and zero after inserting "+" or "-" between each pair of digits, and arranged in dictionary (ASCII code) 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, the first priority for each position "+", and then put "-", so the reason is because "+" ASCII code than "-" small.

Sample input
7
Sample output
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

1#include <bits/stdc++.h>2 using namespacestd;3 intN//save read-in integer n4 BOOLopr[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 "-". 5 BOOLFound =false;//indicates if a set of solutions is found, and if not found, it needs to output "None" at the end.6 //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. 7 //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. 8 voidDfsintDeepintsum) {9     //please fill in the corresponding value in the conditional expression below. Ten     if(Deep = =N) { One         if(Sum = =0) { AFound =true; -             //Please output the final scenario below, with the scheme format referenced in the topic description.  -             //finally remember to change the line Oh.  the              for(intI=0; i<n;i++) -             { -i==n-1? printf ("%d\n", i+1):p rintf ("%d%c", i+1, opr[i+1]==true?'+':'-'); -             } +         } -         return ; +     } A     //please fill out the correct logic in the following statement.  atOpr[deep] =true; -DFS (deep+1, Sum +deep+1  ); -Opr[deep] =false; -DFS (deep+1, sum-deep-1 ); - } -  in //main function Please do it yourself oh, come on! - intMain () { toscanf"%d",&n); +Dfs1,1); -     if(!found) thecout<<"none\n"; *     return 0; $}


Introduction to the course competition of garlic----a study of the arithmetic process of garlic

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.