• <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
            久久免费大片| A级毛片无码久久精品免费| 精品久久久久久无码人妻热| 亚洲va国产va天堂va久久| 一级做a爰片久久毛片毛片| 国产精品成人99久久久久| 久久久久亚洲av无码专区导航| 亚洲午夜久久久久久噜噜噜| 免费久久人人爽人人爽av| 久久精品成人| 亚洲伊人久久综合影院| 国内精品伊人久久久影院| 99精品久久精品一区二区| 久久91精品国产91| 亚洲日本va中文字幕久久| 伊人久久精品无码av一区| 久久久精品人妻一区二区三区四| 国产精品99久久99久久久| 久久精品国产亚洲欧美| 国产精品美女久久久m| 久久久91精品国产一区二区三区| 伊人久久免费视频| 久久免费大片| 久久亚洲欧美国产精品| 1000部精品久久久久久久久| 国产免费久久精品丫丫| 久久经典免费视频| 久久精品国产亚洲精品2020| 国产亚洲美女精品久久久久狼| 9191精品国产免费久久| 久久福利片| 久久影院综合精品| 久久久久久亚洲精品无码| 亚洲AV无码久久精品色欲| 国产高潮久久免费观看| 97精品依人久久久大香线蕉97 | 久久九色综合九色99伊人| 久久国产亚洲精品| 精品久久综合1区2区3区激情| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 成人妇女免费播放久久久 |