• <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>

            A Za, A Za, Fighting...

            堅信:勤能補拙

            和為n連續正數序列

            題目來源:
            http://zhedahht.blog.163.com/blog/static/25411174200732711051101/

            題目:輸入一個正數n,輸出所有和為n連續正數序列。

            例如輸入15,由于1+2+3+4+5=4+5+6=7+8=15,所以輸出3個連續序列1-54-67-8

            分析:這是網易的一道面試題。

            這道題和本面試題系列的第10有些類似。我們用兩個數smallbig分別表示序列的最小值和最大值。首先把small初始化為1big初始化為2。如果從smallbig的序列的和大于n的話,我們向右移動small,相當于從序列中去掉較小的數字。如果從smallbig的序列的和小于n的話,我們向右移動big,相當于向序列中添加big的下一個數字。一直到small等于(1+n)/2,因為序列至少要有兩個數字。

            基于這個思路,我們可以寫出如下代碼:

            void PrintContinuousSequence(int small, int big);

            /////////////////////////////////////////////////////////////////////////
            // Find continuous sequence, whose sum is n
            /////////////////////////////////////////////////////////////////////////
            void FindContinuousSequence(int n)
            {
                  if(n < 3)
                        return;

                  int small = 1; 
                  int big = 2;
                  int middle = (1 + n) / 2;
                  int sum = small + big;

                  while(small < middle)
                  {
                        // we are lucky and find the sequence
                        if(sum == n)
                              PrintContinuousSequence(small, big);

                        // if the current sum is greater than n, 
                        // move small forward
                        while(sum > n)
                        {
                              sum -= small;
                              small ++;

                              // we are lucky and find the sequence
                              if(sum == n)
                                    PrintContinuousSequence(small, big);
                        }

                        // move big forward
                        big ++;
                        sum += big;
                  }
            }

            /////////////////////////////////////////////////////////////////////////
            // Print continuous sequence between small and big
            /////////////////////////////////////////////////////////////////////////
            void PrintContinuousSequence(int small, int big)
            {
                  for(int i = small; i <= big; ++ i)
                        printf("%d ", i);

                  printf("\n");
            }



            posted on 2011-06-16 19:33 simplyzhao 閱讀(190) 評論(0)  編輯 收藏 引用 所屬分類: M_面試題集錦

            導航

            <2010年7月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            99久久夜色精品国产网站| 国产精品久久久久乳精品爆| 久久久国产精华液| 日韩精品久久无码中文字幕| 男女久久久国产一区二区三区| 国产午夜精品久久久久免费视| 国产午夜福利精品久久| 国内精品久久久久久久久电影网| 国产亚洲婷婷香蕉久久精品| 伊人久久大香线蕉精品不卡| 成人久久综合网| 一本久久精品一区二区| 99久久亚洲综合精品成人| 久久精品人人做人人爽电影| 国产精品九九久久免费视频| 亚洲精品无码久久久影院相关影片| 国产成人精品久久亚洲| 久久精品无码午夜福利理论片 | 久久av免费天堂小草播放| 亚洲国产精品18久久久久久| 久久精品国产国产精品四凭| 久久久久高潮毛片免费全部播放 | 狠狠色丁香婷婷久久综合| 久久久精品免费国产四虎| 2021最新久久久视精品爱| 99久久精品国产综合一区| 久久久久女人精品毛片| 区久久AAA片69亚洲| 亚洲欧洲精品成人久久奇米网| 大蕉久久伊人中文字幕| 情人伊人久久综合亚洲| 99国产欧美久久久精品蜜芽| 日韩精品久久久久久免费| 中文精品久久久久人妻不卡| 久久AV无码精品人妻糸列| 久久综合亚洲色一区二区三区| 久久综合伊人77777麻豆| 久久男人AV资源网站| 久久国产亚洲精品| 欧美国产成人久久精品| 久久这里的只有是精品23|