• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            USACO Section 2.3 Zero Sum

            Zero Sum

            Consider 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: zerosum

            INPUT FORMAT

            A single line with the integer N (3 <= N <= 9).

            SAMPLE INPUT (file zerosum.in)

            7
            

            OUTPUT FORMAT

            In 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+7
            1+2-3-4+5+6-7
            1-2 3+4+5+6+7
            1-2 3-4 5+6 7
            1-2+3+4-5+6-7
            1-2-3-4-5+6+7
            
            Analysis

            This problem is a very simple DFS problem. Thanks to the little limitation, we are easy to search all of the situations because we only need to search 3^8=6561 situations. So,just search.

            Code

            /*
            ID:braytay1
            PROG:zerosum
            LANG:C++
            */

            #include 
            <iostream>
            #include 
            <fstream>
            #include 
            <string>
            using namespace std;
            ofstream fout(
            "zerosum.out");
            ifstream fin(
            "zerosum.in");
            string oper[3]={" ","+","-"};
            string equt;
            int N;
            int cal(string s){
                
            string tmp;
                
            for(int i=0;i<s.size();i++){
                    
            if (s[i]!=' '{tmp.push_back(s[i]);}
                }

                
            int ls=1,sum=0;
                
            char op=' ';
                
            for (int i=1;i<tmp.size();i++){
                    
            if (tmp[i]>='1'&&tmp[i]<='9'{
                        ls
            =10*ls+(tmp[i]-48);
                    }

                    
            else {
                        
            if (op=='+'{sum+=ls;op=tmp[i];ls=0;continue;}
                        
            if (op=='-'{sum-=ls;op=tmp[i];ls=0;continue;}
                        
            else{sum+=ls;op=tmp[i];ls=0;}
                    }

                }

                
            switch (op){
                
            case '+':sum+=ls;break;
                
            case '-':sum-=ls;break;
                
            default: sum=-1;
                }
                
                
            return sum;
            }

            void DFS(int step){
                
            if (step>N-1){
                    
            if (cal(equt)==0{fout<<equt<<endl;return;}
                    
            else return;
                }

                
            for (int i=0;i<3;i++){
                    equt
            +=oper[i];
                    equt.push_back(step
            +1+48);
                    DFS(step
            +1);
                    equt.erase(equt.end()
            -2,equt.end());
                }

            }

            int main(){
                fin
            >>N;
                equt
            +="1";
                DFS(
            1);
                
            return 0;
            }


            posted on 2008-08-12 00:45 幻浪天空領主 閱讀(222) 評論(0)  編輯 收藏 引用 所屬分類: USACO

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案(2)

            文章分類(23)

            文章檔案(22)

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            色综合久久久久综合99| 99久久er这里只有精品18| 久久水蜜桃亚洲av无码精品麻豆| 99久久精品免费| 亚洲午夜精品久久久久久人妖| 欧美精品久久久久久久自慰| 久久久SS麻豆欧美国产日韩| 国内精品伊人久久久久妇| 亚洲Av无码国产情品久久| 亚洲伊人久久成综合人影院 | 一本一本久久aa综合精品| 久久AV高潮AV无码AV| 久久亚洲AV成人无码| 亚洲中文字幕无码久久2020 | 日韩中文久久| 久久大香萑太香蕉av| 中文字幕热久久久久久久| 久久w5ww成w人免费| 久久亚洲综合色一区二区三区| 99久久99久久精品国产片果冻| 久久99精品久久久久久野外| 性做久久久久久免费观看| 久久精品国产亚洲AV大全| 久久精品国产一区| 亚洲精品WWW久久久久久| 伊人色综合久久天天人手人婷 | 亚洲精品无码久久久久AV麻豆| 中文字幕乱码久久午夜| 中文字幕亚洲综合久久| 亚洲性久久久影院| 女人香蕉久久**毛片精品| 色综合久久久久综合99| 久久精品免费一区二区三区| 一本大道久久香蕉成人网| 久久久久亚洲AV无码专区体验| 久久久久久国产精品免费免费| 亚洲乱码精品久久久久..| 人妻无码久久精品| 亚洲综合精品香蕉久久网97| 中文国产成人精品久久不卡| 久久久久国产|