• <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 閱讀(241) 評論(0)  編輯 收藏 引用 所屬分類: Data Structures
            久久精品国产亚洲精品2020| 99久久成人18免费网站| 东方aⅴ免费观看久久av| 久久99热只有频精品8| 久久精品无码免费不卡| 国产免费久久精品99re丫y| 国产精品久久久久久久| 最新久久免费视频| 国产精品免费看久久久| 最新久久免费视频| 国产精品美女久久久久AV福利| 偷窥少妇久久久久久久久| 久久综合欧美成人| 久久久无码精品亚洲日韩蜜臀浪潮| 国产99久久久久久免费看| 国产成人精品免费久久久久| 香港aa三级久久三级老师2021国产三级精品三级在 | 久久免费视频观看| 久久综合香蕉国产蜜臀AV| 亚洲精品无码久久久| 久久精品不卡| 国内精品久久久久久不卡影院| 97久久香蕉国产线看观看| 人妻少妇久久中文字幕| 天天躁日日躁狠狠久久| 久久免费视频1| 2021国内久久精品| 性做久久久久久久久浪潮| 久久久久久午夜精品| 噜噜噜色噜噜噜久久| 深夜久久AAAAA级毛片免费看| 狠狠人妻久久久久久综合蜜桃 | 久久精品国产只有精品2020| 精品熟女少妇a∨免费久久| 看久久久久久a级毛片| 成人综合伊人五月婷久久| 国产精品久久久久9999高清| 久久91精品国产91久久小草| 欧美777精品久久久久网| 国内精品久久久久久中文字幕| 久久精品无码av|