發表文章

目前顯示的是 十二月 6, 2009的文章

求職問題:數字拆解(using C#)

//題目:數字拆解
//題目是這樣的:
//3 = 2+1 = 1+1+1 所以3有三種拆法
//4 = 3 + 1 = 2 + 2 = 2 + 1 + 1 = 1 + 1 + 1 + 1 共五種
//5 = 4 + 1 = 3 + 2 = 3 + 1 + 1 = 2 + 2 + 1 = 2 + 1 + 1 + 1 = 1 + 1 +1 +1 +1 //共七種
//依此類推,請問一個指定數字NUM的拆解方法個數有多少個?

//#請計算出Num=40共多少解法,需花多少時間(須印出所有合法解法)
// num = 40, count = 37338, time = 1.188
//收到此信時, 請先回覆 email告知已成功接獲此信.

//請於三天內將撰寫好的程式碼 email 給我

//Answer:
//此code的缺點是速度慢
//C# code
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        public static int num = 40;
        public static int solution = 0;
        static void Main(string[] args)
        {
            DateTime StartTime = DateTime.Now;
            int[] ntable = new int[num];
            int index = 0, rindex = 0;
            int nlength = num;
            for (int i = 0; i < num; i++)
            {
                ntable[i] = 1;
            }
            print(ntable, nlength);
            // top = ntable[0]
            while (nlength != 1)
     …