記一道筆試題

題目
  4=1 + 1 + 1 + 1
    =1 + 1 + 2
    =1 + 3
    =2 + 2c++

  好比數字 4 能夠被拆解成爲如上的四種狀況。那麼如今給你一個vector< vector<int> >  把全部的結果所有的保存到這個 vector<  vector<int>   >  中。算法

函數的函數體,應該是這個樣子的: vector<  vector<int>  >  &    fun(int   n){    //函數體   }c#

分析函數

  從題目的意思是將整數拆分爲多個整數的和。有幾個注意點:字體

    1. 不作差。也就是不出現 4 = 5 - 1(或 -1 + 5)。spa

    2. 1 爲最小可加數。也就是不出現 1 = 1 + 0。 blog

    3. 加法交換率。1 + 3 和 3 + 1 是同樣的。遞歸

    4. 結果保存在集合當中。如:[[1,1,1,1],[1,1,2],[1,3],[2,2]]string

  根據題目能夠推斷出以下結果:it

  0 = 0

  1 = 1

  2 = 1 + 1

 

  3 = 2 + 1

  3 = 1 + 1 + 1

 

  4 = 3 + 1

  4 = 2 + 2

  4 = 2 + 1 + 1

  4 = 1 + 1 + 1 + 1

 

  5 = 4 + 1

  5 = 3 + 2

      5 = 3 + 1 + 1

  5 = 2 + 2 + 1

  5 = 2 + 1 + 1 + 1

  5 = 1 + 1 + 1 + 1 + 1

  ......

  仔細觀察上面過程,對於數字i,紅色字體部分就是 i - 1 的結果。因此用遞歸算法就能夠實現。

實現

  題目用的是c++,這裏用c#實現,也歡迎你們提出更好的方法。效果:

       

源代碼

 class Program
    {
        static void Main(string[] args)
        {
            int num = -4;
            Console.WriteLine("數字" + num.ToString() + "的結果是:");
            List<List<int>> result = BreakNumber(num);
            foreach (var numbers in result)
            {
                for (int i = 0, length = numbers.Count; i < length; i++)
                {
                    Console.Write(numbers[i]);
                    if (i + 1 < length)
                    {
                        if (num > 0)
                        {
                            Console.Write("+");
                        }
                    }
                }
                Console.WriteLine();
            }
        }

        static List<List<int>> BreakNumber(int num)
        {
            if (num == 0 || num == 1 || num == -1)
            {
                return new List<List<int>>() { new List<int>() { num } };
            }
            return RecurBreak(num);
        }

        static List<List<int>> RecurBreak(int num)
        {
            List<List<int>> result = new List<List<int>>();
            if (num == 2)
            {
                result.Add(new List<int>() { 1, 1 });
            }
            else if (num == -2)
            {
                result.Add(new List<int>() { -1, -1 });
            }
            else
            {
                double dNum = Convert.ToDouble(num);
                dNum = num > 0 ? Math.Ceiling(dNum / 2) : Math.Floor(dNum / 2);
                int harfAdd = Convert.ToInt32(dNum);
                if (num > 0)
                {
                    while (harfAdd < num)
                    {
                        result.Add(new List<int>() { harfAdd, num - harfAdd });
                        harfAdd++;
                    }
                }
                else
                {
                    while (harfAdd > num)
                    {
                        result.Add(new List<int>() { harfAdd, num - harfAdd });
                        harfAdd--;
                    }
                }
                int last = num > 0 ? 1 : -1;
                List<List<int>> preResult = RecurBreak(harfAdd - last);
                foreach (var pr in preResult)
                {
                    pr.Add(last);
                }
                result.AddRange(preResult);
            }
            return result;
        }
    }
相關文章
相關標籤/搜索