• <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 閱讀(173) 評論(0)  編輯 收藏 引用 所屬分類: algorithm
            久久99精品久久久久久久不卡| 亚洲国产婷婷香蕉久久久久久| 伊人久久大香线蕉av不卡| 狠狠色丁香婷婷久久综合五月| 一级做a爰片久久毛片看看| 97久久国产综合精品女不卡| 精品午夜久久福利大片| 亚洲色欲久久久久综合网| 精品多毛少妇人妻AV免费久久| 久久99国内精品自在现线| 久久久久九九精品影院| 91精品国产综合久久精品| 亚洲第一永久AV网站久久精品男人的天堂AV| 亚洲国产日韩欧美久久| 青青青青久久精品国产| 亚洲伊人久久综合中文成人网| 久久综合中文字幕| 亚洲成色WWW久久网站| 久久伊人影视| 国产精品18久久久久久vr| 久久只有这里有精品4| 99久久精品费精品国产一区二区| 久久ZYZ资源站无码中文动漫 | 久久香蕉国产线看观看精品yw| 久久毛片一区二区| 久久er热视频在这里精品| 久久精品国产亚洲AV无码麻豆| 97久久精品午夜一区二区| 亚洲精品国产成人99久久| 三级三级久久三级久久| 精品久久久久久亚洲| 2021国内久久精品| 久久久久亚洲AV成人网人人网站| 久久久亚洲欧洲日产国码是AV| 精品久久人人爽天天玩人人妻| 久久精品国产99国产精偷| 久久精品国产精品青草| 国产精品9999久久久久| 国产一区二区三区久久精品| 久久久一本精品99久久精品88| 久久影院午夜理论片无码|