[Huawei Machine test real problem] [2014]63. Equation transformation

Source: Internet
Author: User

Topic

输入一个正整数X,在下面的等式左边的数字之间添加+号或者-号,使得等式成立。1 2 3 4 5 6 7 8 9 = X比如:12-34+5-67+89 = 51+23+4-5+6-7-8-9 = 5请编写程序,统计满足输入整数的所有整数个数。输入:       正整数,等式右边的数字输出:       使该等式成立的个数样例输入:5样例输出:21

Code

/* ---------------------------------------* Date: 2015-07-06* sjf0115* title: Equation Transformation * Source: Huawei Machine Test real problem------------------------- ----------------*/#include <iostream>#include <string>#include <vector>#include <stack>#include <algorithm>using namespace STD;//int converted to stringstringINT2STR (intNUM) {stringStrif(num = =0) {str =' 0 ';returnStr }//if    intTMP = num; while(num) {Str.insert (Str.begin (), num%Ten+' 0 '); Num/=Ten; }//while    returnSTR;}/ * The number of equations that the current computed value result conforms to count equation result x number of neighbors merged result sequence 1 + 2 + 345 345 Is sequence * *   voidHelper vector<int>&num,intIndexintXintResultintSequenceint&count,stringOP) {if(Index = = num.size ()) {if(Result + sequence = = X | | result-sequence = = x) {++count;if(Result + sequence = = x) {OP + ="+"+INT2STR (sequence); }//if            Else{OP + ="-"+INT2STR (sequence); }//else            cout<<op<<endl; }//if        return; }//if    //Continuous numberHelper (num,index+1, X,result,sequence *Ten+ Num[index],count,op);//addition +    if(Op.size () >0){//With Num[index] as the starting point for sequenceHelper (num,index+1, X,result + sequence,num[index],count,op+"+"+INT2STR (sequence)); }//if    Else{//With Num[index] as the starting point for sequenceHelper (num,index+1, X,result + sequence,num[index],count,op+int2str (sequence)); }//else    if(Op.size () >0){//Subtraction-        //With Num[index] as the starting point for sequenceHelper (num,index+1, x,result-sequence,num[index],count,op+"-"+INT2STR (sequence)); }//if}intTransformationequation ( vector<int>Numintx) {intCount =0;stringOP =""; Helper (num,1X0, num[0],COUNT,OP);returnCount;}intMain () {intN,x;intNum//freopen ("c:\\users\\administrator\\desktop\\acm.in", "R", stdin);     while(Cin&GT;&GT;N&GT;&GT;X) { vector<int>Vec for(inti =0; i < n;++i) {Cin>>num;        Vec.push_back (num); }//for        cout<<transformationequation (vec,x) <<endl; }//while    return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Huawei Machine test real problem] [2014]63. Equation transformation

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.