• <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++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              95 隨筆 :: 0 文章 :: 7 評(píng)論 :: 0 Trackbacks

            這道題目是大數(shù)加法。總結(jié)一些一點(diǎn)經(jīng)驗(yàn)。

            (1)整值函數(shù)的應(yīng)用。(這個(gè)不懂的話,去看我Math欄目下有這個(gè)分析)

            (2)sscanf、sprintf的應(yīng)用

            (3)分塊計(jì)算的格式問題。

            先直接看代碼:
                
             1 #include <iostream>

             2 #include <cstdio>
             3 #include <vector>
             4 #include <cstring>
             5 using namespace std;
             6 struct BigInteger {
             7     static const int BASE = 100000000;
             8     static const int WIDTH = 8;
             9     vector<int> s;
            10     
            11     BigInteger(long long num = 0) {
            12         *this = num;
            13     }
            14     
            15     BigInteger operator=(long long num) {
            16         s.clear();
            17         do {
            18             s.push_back(num % BASE);
            19             num /= BASE;
            20         }while (num > 0);
            21         return *this;
            22     }
            23     
            24 
            25     BigInteger operator=(const string& str) {
            26         s.clear();
            27         int x;
            28         int len = (str.size() - 1) / WIDTH + 1;//這里有人看不懂,請看我寫的整值函數(shù) 
            29         for (int i = 0; i < len; i++) {
            30             int end = str.size() - i * WIDTH;
            31             int start = max(0, end - WIDTH);//確定分割的區(qū)間為[start, end)是左閉右開,長度就是end - start 
            32             sscanf(str.substr(start, end - start).c_str(), "%d", &x);
            33             s.push_back(x);
            34         }
            35         return *this;
            36     }
            37     
            38     BigInteger operator+(const BigInteger& bi) const{
            39         BigInteger temp;
            40         temp.s.clear();
            41         for (int i = 0,g = 0;;i++) {
            42             
            43             if (g == 0 && i >= s.size() && i >= bi.s.size()) break;
            44             int x = g;
            45             if (i < s.size()) x += s[i];
            46             if (i < bi.s.size()) x += bi.s[i];
            47             temp.s.push_back(x % BASE);
            48             g = x / BASE;
            49         }
            50         return temp;    
            51     }    
            52     
            53     BigInteger operator +=(const BigInteger& b) {
            54         *this = *this + b;return *this;
            55     }
            56 };
            57 
            58 istream& operator >>(istream &in, BigInteger& x) {
            59     string s;
            60     if (!(in >>s)) return in;
            61     x = s;
            62     return in;
            63 }
            64     
            65 ostream& operator <<(ostream &out, BigInteger&bi) {
            66     out << bi.s.back();
            67     for (int i = bi.s.size()-2; i >= 0; i--) {//再從倒數(shù)第二個(gè)輸出 
            68         char buf[20];
            69         sprintf(buf, "%08d", bi.s[i]);
            70         //cout << buf;
            71         for (int j =0; j < strlen(buf); j++) out << buf[j];
            72     }
            73     return out;
            74 }
            75 
            76 int main() {
            77     int n;
            78     BigInteger a,b,c;
            79     cin >> n;
            80     int kase = 1;
            81     while (n) {
            82         cin >> a >> b;
            83         c = a + b;
            84         if (n != 1)
            85            cout <<"Case " << kase++ << ":" << "\n" << a << " + " << b << " = " << c << endl << endl;
            86         else
            87            cout <<"Case " << kase++ << ":" << "\n" << a << " + " << b << " = " << c << endl;
            88         n--;
            89     }
            90     return 0;
            91 }
            (1)(2)略。
            (3)分塊處理的坑:

            前面兩個(gè)我就不總結(jié)了,這里主要說下,分塊計(jì)算的坑。假設(shè)有這個(gè)字符串"123400000001",由于我們要將其
            按照沒8位字符分成一塊所以第一塊就是00000001、第二快就是1234,然后按照小端存儲(chǔ)格式、低地址對(duì)應(yīng)低位
            但是但他們用sscanf格式化成整數(shù)時(shí)候,00000001不可能還是這樣,這樣就是八進(jìn)制了,而是變成了1,所以我們
            在輸出的時(shí)候,除了最高位對(duì)應(yīng)的那一塊可能不足8位,剩下的塊肯定都要8位處理,所以上面代碼,才從第二快進(jìn)行
            輸出,而且格式是%08d.
            posted on 2014-09-04 14:56 swp 閱讀(1477) 評(píng)論(0)  編輯 收藏 引用 所屬分類: algorithm
            久久精品国产乱子伦| 久久久久久久97| 国产亚洲婷婷香蕉久久精品| 性做久久久久久久久老女人| 精品久久久久久无码人妻蜜桃| 国产成人精品久久亚洲| 一本久久久久久久| 久久精品国产亚洲7777| 久久久久无码精品| 久久久久久精品免费免费自慰 | 无码八A片人妻少妇久久| 久久精品极品盛宴观看| 精品久久久久成人码免费动漫 | 国产无套内射久久久国产| 91久久精品国产成人久久| 国产毛片久久久久久国产毛片 | segui久久国产精品| 国产精品成人99久久久久 | 久久伊人精品青青草原高清| 66精品综合久久久久久久| 青草久久久国产线免观| 亚洲精品乱码久久久久久自慰| 久久精品中文无码资源站| 久久青青草原综合伊人| 天天做夜夜做久久做狠狠| 午夜精品久久久久久中宇| 好久久免费视频高清| 亚洲Av无码国产情品久久| 久久婷婷成人综合色综合| 久久天天躁狠狠躁夜夜不卡| 亚洲精品无码成人片久久| 国产精品免费看久久久香蕉| 性欧美丰满熟妇XXXX性久久久| 久久婷婷久久一区二区三区| 欧美一区二区久久精品| 久久综合综合久久97色| 狠狠色综合网站久久久久久久高清| 精品一区二区久久| 久久夜色精品国产噜噜亚洲AV| 久久99精品国产麻豆不卡| 国产亚洲欧美精品久久久|