• <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


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

            #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);
            }

            //計(jì)算表達(dá)式的值
            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) 評(píng)論(0)  編輯 收藏 引用 所屬分類: AlgorithmUSACO 、回溯法

            導(dǎo)航

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

            統(tǒng)計(jì)

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            搜索

            積分與排名

            最新評(píng)論

            閱讀排行榜

            久久av高潮av无码av喷吹| 2021最新久久久视精品爱| 久久久婷婷五月亚洲97号色| 久久中文骚妇内射| 精品国产综合区久久久久久| 一本久道久久综合狠狠躁AV| 久久亚洲精品无码AV红樱桃| 精品无码久久久久久国产| 国产精品中文久久久久久久| 99国产精品久久| 欧美精品乱码99久久蜜桃| 91精品无码久久久久久五月天| 久久夜色精品国产| 久久免费美女视频| 亚洲精品无码专区久久久 | 99久久精品费精品国产一区二区 | 91精品国产91久久综合| 久久久久国产一区二区| 国产成人精品久久一区二区三区 | 久久精品国产亚洲av麻豆图片| 91精品国产高清久久久久久io| 一本一道久久a久久精品综合| 久久精品国产精品青草app| 久久综合噜噜激激的五月天| 久久人妻少妇嫩草AV蜜桃| 久久久久国产| 久久久无码精品午夜| 国产三级精品久久| 中文字幕一区二区三区久久网站| 久久精品国产99久久无毒不卡| 伊人久久亚洲综合影院| 色天使久久综合网天天| 久久国产欧美日韩精品免费| 国产日韩欧美久久| 久久精品无码一区二区app| 久久久久99精品成人片牛牛影视| 中文字幕亚洲综合久久| 久久精品国产欧美日韩| 色老头网站久久网| 99久久国产宗和精品1上映| 欧美黑人激情性久久|