• <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>
            SmartPtr
            本博客已搬至:http://www.cnblogs.com/baiyanhuang/
            posts - 29,comments - 176,trackbacks - 0

            By SmartPtr(http://www.shnenglu.com/SmartPtr/)

              從0~13中任取出7個數,然后判斷這7個數中是否存在連續的5個數, 規則如下:
            1) 7個數可以是重復的數.
            2) 0可以表示任意數
            例子如下:
            0, 1, 4, 3, 8, 0, 13--->true: 1-2-3-4-5
            0, 1, 1, 1, 9, 10, 0--->false
            0, 1, 3, 9, 10, 11, 12->true: 9-10-11-12-13
            0, 0, 0, 0, 0, 0, 0->true: 0-1-2-3-4

            這是最近看到的一個算法題, 粗粗一看,覺得很簡單, 可是慢慢往里面想,覺得要考慮的還是挺多的。現在把它實現出來放在這里,當然,加了幾個參數使其更加通用。希望對大家有些參考價值。寫的不明白的地方,有錯誤的地方大家可以指出來;大家如果有好的思路的話也希望能寫下來共享一下。以下是代碼與注釋:


             

            #include <stdio.h>
            #include 
            <iostream>
            using namespace std;

            /*
            從0~13中任取出7個數,然后判斷這7個數中是否存在連續的5個數, 規則如下:
            1) 7個數可以是重復的數.
            2) 0可以表示任意數
            例子如下:
            0, 1, 4, 3, 8, 0, 13--->true: 1-2-3-4-5
            0, 1, 1, 1, 9, 10, 0--->false
            0, 1, 3, 9, 10, 11, 12->true: 9-10-11-12-13 
            0, 0, 0, 0, 0, 0, 0->true: 0-1-2-3-4
            */


            // Helper functions
            void outputarray(int a[], int n)
            {
                
            for(int i = 0; i < n; ++i) cout<<a[i]<<" ";
                cout
            <<endl;
            }

            void outputresult(int nstart, int m)
            {
                cout
            <<"Continuous Array:";
                
            while(m--)    cout<<nstart++<<" ";
                cout
            <<endl;
            }

            // Return if the n elements array contains m continuous numbers, the elements must large than 0
            // this is a common implementation
            bool Is_n_Contains_m_ContinuousNum(int a[], int n, int m) 
            {
                
            // step 1: get num of 0
                int nZeroNum = 0;
                
            for(int i = 0; i < n; ++i)
                    
            if(0 == a[i]) ++nZeroNum;

                cout
            <<"Original Array:  "; outputarray(a, n);

                
            // step 2: if we have enough 0, get continuous num is easy.
                if(nZeroNum >= m-1)
                {
                    
            int min = a[0];
                    
            for(int i = 1; i < n; ++i)
                        
            if(a[i] < min || 0 == min) min = a[i];
                    outputresult(min, m);
                    
            return true;
                }
                
            // not enough zero, we need to refine the judgement
                else
                {
                    
            // step 2.1: sort the array. (bubble sort, ascending)
                    bool bIsDone = false;
                    
            for(int i = n-1; i >= 0 && !bIsDone; --i)
                    {
                        bIsDone 
            = true;
                        
            for(int j = 0; j < i; ++j)
                        {
                            
            if(a[j+1< a[j])
                            {
                                bIsDone 
            = false;
                                
            int tmp = a[j+1];
                                a[j
            +1= a[j];
                                a[j] 
            = tmp;
                            }
                        }
                    }

                    cout
            <<"Sorted Array:    "; outputarray(a, n);

                    
            // step 2.2: remove redundant num except 0
                    int aa[256];
                    aa[
            0= a[0];
                    
            int j = 1;
                    
            for(int i = 0; i < n-1++i)
                    {
                        
            if(a[i+1!= a[i] || 0 == a[i+1])
                            aa[j
            ++= a[i+1];
                    }
                    memcpy(a, aa, j 
            * sizeof(aa[0]));
                    n 
            = j;
                    
            if(n < m) return false;

                    cout
            <<"Unique Array:    "; outputarray(a, n);


                    
            // step 2.3: get index of first non-zero element
                    int nIndex = 0;
                    
            for(int i = 0; i < n; ++i)
                    {
                        
            if(a[i] != 0
                        {
                            nIndex 
            = i;
                            
            break;
                        }
                    }

                    
            // step 2.4: refined judgement
                    
            // if n = 7; m = 5; nZeroNum = 2;
                    
            // if we can get continious number without zero or only with 1 zero, then with 2 zero is ok too.
                    
            // so if we got x zeros, we need to check if can success with (0 ~ x-1) zeros
                    for(int k = 0; k <= nZeroNum; ++k)
                    {
                        
            int nInterval = m - k - 1;
                        
            for(int i = nIndex; i < n-nInterval; ++i)
                        {
                            
            // when k = nZeroNum = 2;
                            
            // if the a[i+nInterval] - a[i] ranged in (nInterval, m-1), then it is continuous)
                            
            // means a[i+2] - a[i] ranged in (2, 4), for example:
                            
            // 1 3 5;  1 2 3; 1 2 4;
                            if(a[i+nInterval] - a[i] <= m-1 && a[i+nInterval] - a[i] >= nInterval)
                            {
                                outputresult(a[i], m);
                                
            return true;
                            }
                        }
                    }
                }

                
            return false;
            }


            int main(int argc, char *argv[])
            {
                
            int a[] = {0000000};
                
            if(!Is_n_Contains_m_ContinuousNum(a, sizeof(a)/sizeof(a[0]),5))
                    cout
            <<"Continuous Array:Not Found ";
                
            return 0;

             



            PS:網友建議

            fflush:不需要這么復雜吧,題目說明了是從0~13中任取出7個數,那么建立一個int A[13],記錄哪些數有哪些數沒有(有的話置A[i]為1,否則是0),然后檢測A中連續的5個位置的情況就可以了

            SmartPtr:fflush的思路對我很有啟發, 但我們還要考慮多個0的情況, 按著這個思路的話我覺得可以這么做:針對0~13建立一個數組A[14], A[0], A[1]...分別對應0, 1...的個數。然后依次檢測A中連續的5個位置, 如果其0的個數小于A[0],那么就存在連續的數。(當然還有一些邊緣情況要處理)。

            這個算法我覺得十分有效, 通過引入一個數組大大的簡化了問題。但是有個缺點就是如果要判斷任意范圍內的5個連續數就不容易了。如:
            0, 1, 122, 678, 10000, 3, 6

            posted on 2007-08-26 20:49 SmartPtr 閱讀(1111) 評論(0)  編輯 收藏 引用
            亚洲午夜久久久久久噜噜噜| 亚洲精品高清久久| 久久久久久噜噜精品免费直播| 国产精品伊人久久伊人电影| 久久久久久毛片免费看| 久久这里有精品| 99久久99这里只有免费费精品| 色综合久久精品中文字幕首页 | 日韩精品久久久久久| 久久久久久综合一区中文字幕| 久久精品视频91| 亚洲精品无码久久一线| 国产成人精品久久一区二区三区av| 久久强奷乱码老熟女网站| 亚洲国产精品无码久久| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 国产激情久久久久影院| 久久天天躁夜夜躁狠狠| 精品视频久久久久| 国产精品一区二区久久不卡| 久久久久国色AV免费看图片| 久久夜色精品国产噜噜噜亚洲AV| 久久93精品国产91久久综合| 久久精品国产亚洲AV无码麻豆| 久久丝袜精品中文字幕| 色综合久久综合网观看| 久久精品www人人爽人人| 狠狠色丁香婷婷久久综合| 精品久久久久久无码中文野结衣 | 人妻无码久久一区二区三区免费| 国产精品久久久99| 国产精品久久久久无码av| 老色鬼久久亚洲AV综合| 国产成人久久精品一区二区三区| 青青热久久国产久精品| 国产日韩久久久精品影院首页| 精品国产乱码久久久久久郑州公司| 久久久久久精品无码人妻| 欧美亚洲国产精品久久| 国产欧美久久久精品影院| 伊人久久大香线蕉综合网站|