• <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 幻浪天空領主 閱讀(225) 評論(0)  編輯 收藏 引用 所屬分類: USACO

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案(2)

            文章分類(23)

            文章檔案(22)

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品极品盛宴观看| 日韩精品久久久久久免费| 国产精品国色综合久久| 久久99国产精品99久久| 日韩一区二区久久久久久| 久久精品国产99久久无毒不卡| 久久天天躁夜夜躁狠狠躁2022| 国产精品久久久久蜜芽| 国产午夜免费高清久久影院 | 亚洲va久久久噜噜噜久久天堂| 模特私拍国产精品久久| 婷婷五月深深久久精品| 久久精品国产精品亚洲艾草网美妙| 久久免费视频1| 久久久av波多野一区二区| 国产精品久久久久乳精品爆| 伊人久久精品影院| 国产日韩久久免费影院| 狠狠色婷婷综合天天久久丁香| 97香蕉久久夜色精品国产| 国产精品免费久久久久电影网| 亚洲午夜无码久久久久| 久久天天躁狠狠躁夜夜2020| 久久99精品免费一区二区| 99久久免费国产精品热| 97久久精品人妻人人搡人人玩| 97精品依人久久久大香线蕉97| 久久久久99精品成人片三人毛片| 国产精品一区二区久久国产| 国产精品99久久99久久久| 国产精品久久成人影院| 91精品国产色综合久久| 久久99精品久久久久久不卡| 久久久国产视频| 久久国产亚洲精品麻豆| 精品免费久久久久国产一区| 日韩久久久久中文字幕人妻| 久久久久久精品久久久久| 免费观看久久精彩视频| 精品无码人妻久久久久久| 人妻少妇久久中文字幕一区二区|