• <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 2.3 Zero Sum


            簡單的回溯題。考點可能在于表達式的求值吧。
            由于' '操作符的優(yōu)先級大于+,-,可以用一個棧來實現(xiàn)表達式求值。
            不過比較麻煩。
            這里通過保存last_op,如果當(dāng)前的操作符是' '的話,則last_op不變,直到遇到新的+/-操作符。
            然后開始計算。

            #include?<iostream>
            #include?
            <fstream>

            using?namespace?std;

            ifstream?
            in("zerosum.in");
            ofstream?
            out("zerosum.out");


            // express保存回溯產(chǎn)生的操作符序列
            char?express[10];
            void?backtraing(int?depth);
            int?compute_express();

            int?n;
            ????
            static?int?cnt?=?0;

            void?solve()
            {
            ???
            in>>n;?

            ???backtraing(
            0);
            }

            void?backtraing(int?depth)
            {
            ????
            if(depth==n-1){
            ????????
            int?res?=?compute_express();
            ????????
            if(res==0){
            ????????????
            for(int?i=1;i<n;++i)
            ????????????????
            out<<i<<express[i-1];
            ????????????
            out<<n<<endl;
            ????????}
            ????????
            return?;
            ????}

            ????express[depth]
            ='?';
            ????backtraing(depth
            +1);
            ????express[depth]
            ='+';
            ????backtraing(depth
            +1);
            ????express[depth]
            ='-';
            ????backtraing(depth
            +1);
            }

            //計算表達式的值
            int?compute_express()
            {
            ????
            char?last_op?=?'+';
            ????
            int?res?=?0;

            ????
            int?operand?=?1;

            ????
            for(int?i=0;i<n-1;++i){
            ????????
            if(express[i]=='?'){
            ????????????operand?
            =?operand*10+i+2;
            ????????}
            else?{
            ????????????
            if(last_op=='+')
            ????????????????res
            +=operand;
            ????????????
            else
            ????????????????
            if(last_op=='-')
            ????????????????????res
            -=operand;
            ????????????operand
            =i+2;
            ????????????last_op?
            =?express[i];
            ????????}
            ????}

            ????
            if(last_op=='+')
            ????????res
            +=operand;
            ????
            if(last_op=='-')
            ????????res
            -=operand;

            ????
            return?res;
            }

            int?main(int?argc,char?*argv[])
            {
            ????solve();?
            ????
            return?0;
            }


            posted on 2009-06-24 21:35 YZY 閱讀(1170) 評論(0)  編輯 收藏 引用 所屬分類: AlgorithmUSACO回溯法

            導(dǎo)航

            <2009年6月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            統(tǒng)計

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            国产免费福利体检区久久| 久久国产成人| 久久精品国产免费| 国产成人久久久精品二区三区 | 色综合久久88色综合天天| 99久久免费国产特黄| 99热热久久这里只有精品68| 久久午夜福利电影| 亚洲精品白浆高清久久久久久| .精品久久久麻豆国产精品| 国产ww久久久久久久久久| 中文字幕精品无码久久久久久3D日动漫| 久久天天躁狠狠躁夜夜不卡 | 欧美久久一级内射wwwwww.| 久久午夜无码鲁丝片秋霞| 久久亚洲欧美国产精品| 久久精品国产亚洲7777| 久久久久久久人妻无码中文字幕爆| 亚洲国产精品久久久久久| 久久人妻少妇嫩草AV蜜桃| 蜜桃麻豆www久久| 久久AV高清无码| 色婷婷久久综合中文久久一本| 久久99精品国产麻豆| 久久SE精品一区二区| 久久精品18| 日韩精品国产自在久久现线拍| 国产美女亚洲精品久久久综合| 国产精品久久久久久久久久免费| 伊人久久综合精品无码AV专区| 久久久网中文字幕| 亚洲国产成人久久精品动漫| 久久久精品人妻一区二区三区蜜桃| 日本精品久久久久影院日本| 国产精品成人久久久久三级午夜电影| av午夜福利一片免费看久久| 亚洲国产另类久久久精品| 久久久噜噜噜久久中文字幕色伊伊 | 久久久精品波多野结衣| 99久久无码一区人妻a黑| 色偷偷88888欧美精品久久久|