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

            f(sixleaves) = sixleaves

            重劍無鋒 大巧不工

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              95 隨筆 :: 0 文章 :: 7 評論 :: 0 Trackbacks
            @import url(http://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); 最近在學習游戲開發,又得重新看C++鳥,為了進行語法的熟悉決定再次進行刷oj,我刷的oj時杭電的oj。在1002題時候,卡了一下,但最終還是順利通過。
            大數加法是一項十分十分基本的編程技能,好鳥不啰嗦鳥。

            算法核心思想:1.將字符串按照權重轉換為整型數組中相應的位(0索引對應最低位,權重為1,是個位)。2.然后進行按照位相加運算。

            具體代碼如下。
             2 //  main.cpp
             3 //  oj
             4 //
             5 //  Created by sixleaves on 14-7-23.
             6 //  Copyright (c) 2014年 sixleaves. All rights reserved.
             7 //
             8 
             9 #include <iostream>
            10 #include <string>
            11 #include <cstdlib>
            12 const int ArSize = 1024;
            13 using namespace std;
            14 char *psResult = new char[ArSize];//   分配于堆中,不是局部變量
            15 char* sum(string a, string b);

            16 int main(int argc, const char * argv[])
            17 {
            18 
            19     int nTestCase;
            20     int i = 0;
            21     cin >> nTestCase;
            22     while (i < nTestCase) {
            23         string a,b;
            24         while (cin >> a >> b) {
            25             cout << "Case " << i + 1 <<":"<< endl;
            26             cout << a + " + " + b + " = "
            27                  <<sum(a, b) << endl;
            28             if(i + 1 != nTestCase)
            29                 cout << endl;
            30             i++;
            31             break;
            32         }
            33     }
            34     return 0;
            35 }
            36 
            37 char* sum(string a, string b) {
            38     //   進行數據的轉換,把字符串數據轉換為整數
            39     //   char *psResult = new char[ArSize];
                    //   為了提高程序速度,把這個放在了外面,不用每次都申請
            40     int nR[ArSize] = {0}, nA[ArSize] = {0}, nB[ArSize] = {0};//   并且都初始化為0
            41     int nLenA = a.length(), nLenB = b.length();
            42     for(int i = 0; i < nLenA; i++) {
            43         nA[i] = a[nLenA - i - 1] - '0';
            44     }
            45     for(int i = 0; i < nLenB; i++) {
            46         nB[i] = b[nLenB - i - 1] - '0';
            47     }
            48     //   進行相加運算
            49     int nLenMax = nLenA > nLenB? nLenA : nLenB;
            50     for(int i = 0; i < nLenMax; i++) {
            51         nR[i] += nA[i] + nB[i];
            52         if(nR[i] > 9) {
            53             nR[i] -= 10;
            54             nR[i + 1]++;
            55         }
            56     }
            57     //   轉換為字符串
            58     if(nR[nLenMax] != 0)//   如果最后一位相加有近位,則總長度加1
            59         nLenMax++;
            60     for(int i = 0; i < nLenMax; i++) {
            61         psResult[i] = nR[nLenMax - i - 1] + '0';
            62     }
            63     psResult[nLenMax] = '\0';
            64     return psResult;
            65 }
            66 
            posted on 2014-07-24 02:58 swp 閱讀(172) 評論(0)  編輯 收藏 引用 所屬分類: algorithm
            国产韩国精品一区二区三区久久| 热久久视久久精品18| 狠狠色丁香久久婷婷综合五月| 久久人爽人人爽人人片AV| 精品久久久久久综合日本| 久久丝袜精品中文字幕| 无码人妻久久一区二区三区免费| 久久亚洲国产欧洲精品一| 四虎影视久久久免费观看| 99久久久国产精品免费无卡顿| 伊人久久免费视频| 午夜精品久久久久久毛片| 久久精品亚洲欧美日韩久久| 人妻无码中文久久久久专区| 久久丝袜精品中文字幕| 狠狠狠色丁香婷婷综合久久俺| 合区精品久久久中文字幕一区| 国产精品久久毛片完整版| 久久人妻AV中文字幕| 精品久久久久一区二区三区| 亚洲国产另类久久久精品黑人| 麻豆久久| 久久国产美女免费观看精品 | 91精品国产色综久久 | 国内精品伊人久久久久| 久久久久人妻一区二区三区| 国产国产成人久久精品| 中文字幕久久欲求不满| 久久久精品一区二区三区| 国产亚洲色婷婷久久99精品| 色偷偷偷久久伊人大杳蕉| 久久WWW免费人成一看片| 久久天天躁夜夜躁狠狠躁2022| 色偷偷91久久综合噜噜噜噜| 久久黄视频| 伊人久久大香线蕉综合5g | 亚洲级αV无码毛片久久精品| 久久综合九色综合网站| 国产精品久久新婚兰兰| 久久婷婷五月综合色奶水99啪| 亚洲天堂久久久|