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

            T9的空間

            You will never walk alone!

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              69 隨筆 :: 0 文章 :: 28 評論 :: 0 Trackbacks
            //用習慣了STL_heap自己來寫一個,類實現。

            #include
            <iostream>
            #include
            <string>
            #include
            <cmath>
            #include
            <algorithm>
            using namespace std;

            #define N 100

            class my_heap{    
            public:
                
            int array[N];
                
            int cnt;

                my_heap()
                
            {
                    
            for(int i=0;i<N;i++)
                        array[i]
            =0;
                    cnt
            =0;
                }

                
            void push(int num);
                
            int pop();
            }
            ;

            void my_heap::push(int num)
            {
                array[
            ++cnt]=num;
                
            int i=cnt,j=i/2;
                
            while(j>0)
                
            {
                    
            if(array[i]>array[j])
                        swap(array[i],array[j]);
                    
            else break;
                    i
            =j;j=i/2;
                }

            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
            int my_heap::pop()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
            {
                
            int num=array[1],maxc;
                swap(array[
            1],array[cnt]);
                cnt
            --;
                
            int i=1,j=2*i;
                
            while(j<=cnt)
                
            {
                    
            if(j+1>cnt) maxc=j;
                    
            else maxc=array[j]>array[j+1]?j:j+1;
                    
            if(array[maxc]>array[i])
                        swap(array[i],array[maxc]);
                    
            else break;
                    i
            =maxc;j=2*i;
                }

                
            return num;
            }
               
                                                                                                          
            int main()
            {
                
            int num;
                my_heap h;
                
            while(scanf("%d",&num),num)
                    h.push(num);
                
            while(h.cnt)
                    printf(
            "%d ",h.pop());
                printf(
            "\n");
                
            for(int i=1;h.array[i]&&i<N;i++)
                    printf(
            "%d ",h.array[i]);
                printf(
            "\n");
                
            return 0;
            }
            測試數據及結果:
            1 4 9 88 56 21 45 7 6 3 99 1452 122 34 2 0
            1452 122 99 88 56 45 34 21 9 7 6 4 3 2 1
            1 2 3 4 6 7 9 21 34 45 56 88 99 122 1452
            Press any key to continue
            posted on 2008-11-23 12:42 Torres 閱讀(235) 評論(0)  編輯 收藏 引用 所屬分類: Data Structures
            久久露脸国产精品| 93精91精品国产综合久久香蕉| 久久露脸国产精品| 国内精品伊人久久久影院| 久久九九兔免费精品6| 四虎国产永久免费久久| 香蕉aa三级久久毛片| 国产精品99久久免费观看| 久久久久国产成人精品亚洲午夜| 亚洲国产精品无码久久| 国内精品久久久久久久涩爱| 7777精品久久久大香线蕉| 久久久久亚洲AV成人网人人网站| 伊人久久大香线焦AV综合影院| 99久久99久久精品国产片| 亚洲AV无码久久寂寞少妇| 99久久国产亚洲高清观看2024| 亚洲欧美日韩中文久久| 久久久久无码中| 色成年激情久久综合| 久久精品亚洲一区二区三区浴池| 久久久中文字幕日本| 伊人久久精品线影院| 97久久国产亚洲精品超碰热| 久久久久亚洲AV成人网人人网站 | 伊人久久综合无码成人网| 热久久这里只有精品| 成人国内精品久久久久影院| 久久亚洲AV成人无码软件| 亚洲精品无码久久不卡| 久久无码国产| 久久夜色精品国产| 久久久久亚洲爆乳少妇无| 久久99精品国产麻豆不卡| 91久久婷婷国产综合精品青草 | 久久综合给合综合久久| 一级做a爱片久久毛片| 久久精品这里热有精品| 国产91色综合久久免费分享| 国产精品免费福利久久| 九九久久99综合一区二区|