• <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>
            posts - 183,  comments - 10,  trackbacks - 0

            一個關于數組的問題

            一個數組 {5, 2, 9, 4, 7}
            這個數組有 5 個元素
            這 5 個元素的位置依次是 1 2 3 4 5
            這 5 個元素的從小到大的順序是 3 1 5 2 4

            數組中的一個元素,有三個屬性即:
            元素本身    A   5 2 9 4 7
            原來在數組中的位置  B 1 2 3 4 5
            從小到大的順序      C 3 1 5 2 4

            給定一個數組,如何得到每個元素的這三個屬性?

            對于每個元素,知道其中一個屬性,如何得到另外兩個屬性
            B 和 C 都是從 1 到 5 的。
            對 B 可以排個序,然后按索引取即可。
            C 也是如此。

            對于 A ,因為其是有間隔的,如果直接按索引,可能會浪費空間。可以采用哈希去做 O(1) 。
            也可以直接對其進行一遍掃描 O(N) 。
            或者建立平衡二叉樹 O(logN) 。

             1 #include <iostream>
             2 #include <cstdlib>
             3 using namespace std;
             4 
             5 struct Element
             6 {
             7     int value;
             8     int position;
             9     int order;
            10 };
            11 
            12 int cmpByValue(const void* a, const void* b)
            13 {
            14     return ((Element*)a)->value - ((Element*)b)->value;
            15 }
            16 
            17 int cmpByPosition(const void* a, const void* b)
            18 {
            19     return ((Element*)a)->position - ((Element*)b)->position;
            20 }
            21 
            22 int cmpByOrder(const void* a, const void* b)
            23 {
            24     return ((Element*)a)->order - ((Element*)b)->order;
            25 }
            26 
            27 int main()
            28 {
            29     int a[] = {52947};
            30     Element els[5];
            31     for (int i = 0; i != 5++i)
            32     {
            33         els[i].value = a[i];
            34         els[i].position = i + 1;
            35     }
            36     qsort(els, 5sizeof (*els), cmpByValue);
            37 
            38     for (int i = 0; i != 5++i)
            39     {
            40         els[i].order = i + 1;
            41     }
            42     
            43     for (int i = 0; i != 5++i)
            44     {
            45         cout << els[i].value << ' ' << els[i].position << ' ' << els[i].order << endl;
            46     }
            47     cout << endl;
            48     
            49     qsort(els, 5sizeof (*els), cmpByPosition);
            50     
            51     for (int i = 0; i != 5++i)
            52     {
            53         cout << els[i].value << ' ' << els[i].position << ' ' << els[i].order << endl;
            54     }
            55     cout << endl;
            56     
            57     qsort(els, 5sizeof (*els), cmpByOrder);
            58     for (int i = 0; i != 5++i)
            59     {
            60         cout << els[i].value << ' ' << els[i].position << ' ' << els[i].order << endl;
            61     }
            62     
            63     return 0;
            64 }

             


            http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/
            http://www.slyar.com/blog/stdlib-qsort.html
            http://www.shnenglu.com/qywyh/articles/3405.html
            http://pubs.opengroup.org/onlinepubs/009695399/functions/qsort.html
            http://linux.die.net/man/3/qsort
            http://en.wikipedia.org/wiki/Qsort
            http://msdn.microsoft.com/en-us/library/aa272872(v=vs.60).aspx
            posted on 2011-07-20 11:32 unixfy 閱讀(91) 評論(0)  編輯 收藏 引用
            精品综合久久久久久97超人| 亚洲精品视频久久久| 成人国内精品久久久久一区| 久久97精品久久久久久久不卡| 久久高清一级毛片| 久久天天躁狠狠躁夜夜avapp| 国产精品99久久久久久人| 精品久久久久久无码人妻热| 久久久久亚洲AV成人网人人网站| 97久久超碰国产精品旧版| 看全色黄大色大片免费久久久| 久久久精品国产sm调教网站| 久久精品二区| 2021精品国产综合久久| 久久成人国产精品免费软件| 久久激情五月丁香伊人| 久久久久久久久久久久中文字幕 | 最新久久免费视频| 久久97精品久久久久久久不卡| 久久久久人妻一区二区三区| 99久久精品无码一区二区毛片| 久久精品人成免费| 亚洲精品乱码久久久久久蜜桃图片 | 久久久久久国产精品无码超碰| 久久精品无码专区免费| 久久精品国产精品青草| 91精品国产91久久综合| 日日噜噜夜夜狠狠久久丁香五月| 一级女性全黄久久生活片免费 | 精品国产青草久久久久福利| 国产精品一久久香蕉国产线看观看| 久久精品国产亚洲av麻豆蜜芽| 精品久久久久久无码免费| 久久中文字幕一区二区| 国产精品久久久久无码av| 996久久国产精品线观看| 精品久久久无码人妻中文字幕豆芽| 新狼窝色AV性久久久久久| 日产精品久久久久久久| 久久精品国产亚洲AV麻豆网站| 国产Av激情久久无码天堂|