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


            簡單的回溯題。考點(diǎn)可能在于表達(dá)式的求值吧。
            由于' '操作符的優(yōu)先級大于+,-,可以用一個棧來實(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 閱讀(1161) 評論(0)  編輯 收藏 引用 所屬分類: AlgorithmUSACO回溯法

            導(dǎo)航

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

            統(tǒng)計(jì)

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            久久亚洲AV成人无码电影| 久久综合亚洲色HEZYO社区| 国产精品免费久久久久电影网| 中文成人无码精品久久久不卡| 中文字幕久久波多野结衣av| 国产成年无码久久久久毛片| 91亚洲国产成人久久精品| 一级做a爰片久久毛片毛片| 久久精品人成免费| 久久综合视频网站| 国产精品一久久香蕉国产线看| 久久五月精品中文字幕| 国产亚洲综合久久系列| 日本亚洲色大成网站WWW久久 | 国产综合精品久久亚洲| 热久久视久久精品18| 久久99精品免费一区二区| 久久精品亚洲中文字幕无码麻豆| 久久久久亚洲AV成人网| 精品一区二区久久| 日本欧美久久久久免费播放网| 日韩久久久久中文字幕人妻| 久久精品国产69国产精品亚洲| 亚洲国产精品一区二区久久hs| 久久精品国产色蜜蜜麻豆| 免费观看久久精彩视频| 久久w5ww成w人免费| 九九久久精品无码专区| 久久婷婷色综合一区二区| 99精品久久精品| 久久久一本精品99久久精品66| 久久久SS麻豆欧美国产日韩| 午夜精品久久久久成人| 欧美激情精品久久久久久| 久久久久亚洲AV无码去区首| 国内精品伊人久久久久网站| 国产精品九九久久免费视频| 国产成人久久精品二区三区| 欧美精品一区二区久久| 久久这里的只有是精品23| 国内精品九九久久精品|