• <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
            国产精品久久久久久久人人看| 久久久久九国产精品| 一本大道久久香蕉成人网| 久久男人AV资源网站| 久久精品国产AV一区二区三区 | 久久se这里只有精品| 久久综合五月丁香久久激情| 久久强奷乱码老熟女网站| 国产V亚洲V天堂无码久久久| 狠狠精品久久久无码中文字幕| 久久综合久久伊人| 久久香蕉一级毛片| 久久这里有精品视频| 久久99精品久久久久婷婷| 狠狠色丁香婷婷综合久久来来去 | 日韩亚洲国产综合久久久| 精品久久久无码21p发布| 久久久久国产精品| 亚洲国产欧洲综合997久久| 久久精品无码一区二区三区日韩| 无码国内精品久久综合88| 国产精品美女久久久久av爽| 久久精品aⅴ无码中文字字幕重口| 精品久久国产一区二区三区香蕉 | 亚洲?V乱码久久精品蜜桃 | 九九久久99综合一区二区| 久久这里都是精品| 久久久久无码精品国产app| 亚洲国产精品热久久| 久久人人爽爽爽人久久久| 中文字幕精品久久久久人妻| 久久成人18免费网站| 久久精品国产一区二区三区日韩| 天天爽天天狠久久久综合麻豆| 日韩一区二区三区视频久久| 亚洲欧洲久久久精品| 人妻系列无码专区久久五月天| 久久午夜无码鲁丝片午夜精品| 国产ww久久久久久久久久| 久久99精品久久久久久9蜜桃| 大蕉久久伊人中文字幕|