• <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++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              69 隨筆 :: 0 文章 :: 28 評(píng)論 :: 0 Trackbacks
            //用習(xí)慣了STL_heap自己來(lái)寫一個(gè),類實(shí)現(xiàn)。

            #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;
            }
            測(cè)試數(shù)據(jù)及結(jié)果:
            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 閱讀(239) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Data Structures
            久久久久亚洲AV成人网| 亚洲AV无码久久寂寞少妇| 人人妻久久人人澡人人爽人人精品| 69久久夜色精品国产69| 热re99久久精品国产99热| 亚洲天堂久久精品| 欧美亚洲国产精品久久蜜芽| 97久久天天综合色天天综合色hd | 国产99久久久国产精免费| 国产精品久久精品| 亚洲国产天堂久久综合网站| 九九99精品久久久久久| 青青草国产精品久久久久| 精品久久久久久无码中文野结衣 | 国内精品九九久久久精品| 久久久久免费看成人影片| 国产综合久久久久| 久久久久99精品成人片| 久久精品国产亚洲7777| 国内精品伊人久久久久妇| 久久久久久夜精品精品免费啦| 97r久久精品国产99国产精| 国产一级做a爰片久久毛片| 久久艹国产| 色综合久久无码中文字幕| 久久99国产精品二区不卡| 99久久精品费精品国产| 久久久久久亚洲精品影院| 国产一区二区三区久久精品| 久久久久久亚洲精品不卡| 久久永久免费人妻精品下载| 欧美久久综合性欧美| 国产亚洲精久久久久久无码77777| 国产午夜免费高清久久影院| 久久亚洲国产精品123区| 久久99精品久久久久久动态图 | 99国产欧美久久久精品蜜芽| 久久精品综合一区二区三区| 亚洲AV成人无码久久精品老人| 国产精品成人无码久久久久久 | 7777精品久久久大香线蕉|