USACO 2.3.3 deep search for Zero Sum

Source: Internet
Author: User

Zero SumConsider the sequence of digits from 1 through N (where N = 9) in increasing order: 1 2 3... n. now insert either a' + 'for addition or a'-' for subtraction or A' [blank] to run the digits together between each pair of digits (not in front of the first digit ). calculate the result that of the expression and see if you get zero. write a program that will find all sequences of length N that Produce a zero sum. program name: zerosumINPUT FORMATA single line with the integer N (3 <= N <= 9 ). sample input (file zerosum. in) 7 OUTPUT FORMATIn ASCII order, show each sequence that can create 0 sum with a '+', '-', or ''between each pair of numbers. sample output (file zerosum. out) 1 + 2-3 + 4-5-6 + 71 + 2-3-4 + 5 + 6-71-2 3 + 4 + 5 + 6 + 71-2 3- 4 5 + 6 71-2 + 3 + 4-5 + 6-71-2-3-4-5 + 6 + 7 /*----------------------------------- -------------------------- */Once again, it indicates that USACO will help you review the question. This question is not a dynamic conversion, but a simple deep search. Simply traverse all the situations. A maximum of 9 numbers, 8 symbols, and a maximum of 3 8 power types can be obtained. Program: [cpp]/* ID: zhaorui3 PROG: zerosum LANG: C ++ */# include <iostream> # include <fstream> using namespace std; int operations [9] = {0}; // 0: nothing, 1: +, 2:-int templeft = 0, tempRight = 0; int main () {operations [0] = 1; ifstream fin ("zerosum. in "); ofstream fout (" zerosum. out "); int end; fin> end; while (true) // this layer of loop is used to traverse all symbols {templeft = 0; tempRight = 0; for (int j = 1; j <= end; j ++) // this layer of loop is used to calculate the result under the current symbol {if (operations [J-1] = 1) // + {int k = j; tempRight = j; while (k <= end-1 & operations [k] = 0) {tempRight = tempRight * 10 + k + 1; k ++; j ++;} templeft = templeft + tempRight; tempRight = 0; continue ;} if (operations [J-1] = 2) //-{int k = j; tempRight = j; while (k <= end-1 & operations [k] = 0) {tempRight = tempRight * 10 + k + 1; k ++; j ++ ;} templeft = templeft-tempRight; tempRight = 0; continue ;}}if (templeft = 0) {fout <1; for (int m = 0; m <= end-2; m ++) {switch (operations [m + 1]) {case 1: fout <'+'; break; case 2: fout <'-'; break; case 0: fout <''; break;} fout <m + 2;} fout <endl ;} operations [end-1] ++; for (int m = end-1; m> 1; m --) {if (operations [m]> 2) {operations [m] = 0; operations [M-1] ++ ;}} if (operations [1]> 2) break ;}}

Related Article

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.