2009年12月14日 星期一
2009年12月12日 星期六
求職問題:數字拆解(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:
//題目是這樣的:
//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)
{
while (true)
{
if (index < nlength - 1)//last two element
{
ntable[index]++;
if (0 == index) swapr(ntable, rindex, ref nlength);
index++;
fixdec(ntable, ref nlength);
print(ntable, nlength);
}
else break;
}
rindex = rsnotone(ntable, nlength);
rindex = found(ntable, ntable[rindex], nlength);
if (rindex < nlength - 1)//last two element
{
ntable[rindex]++;
swapr(ntable, rindex, ref nlength);
index = rindex + 1;
fixdec(ntable, ref nlength);
print(ntable, nlength);
}
else if (rindex > 0 && ntable[rindex - 1] < ntable[0])
{
rindex = found(ntable, ntable[rindex - 1], nlength);
ntable[rindex]++;
swapr(ntable, rindex, ref nlength);
index = rindex + 1;
fixdec(ntable, ref nlength);
print(ntable, nlength);
}
else
{
index = 0;
rindex = 0;
}
}
Console.Write("num = ");
Console.Write(num);
Console.Write(", count = ");
Console.Write(solution);
DateTime StopTime = DateTime.Now;
TimeSpan duration = StopTime - StartTime;
Console.Write(", time = ");
Console.WriteLine(duration);
}
static int found(int[] ntable, int value, int nlength)
{
int i;
for (i = 0; i < nlength; i++)
{
if(value == ntable[i])break;
}
return i;
}
//reverse scan and return index that value not equal 1
static int rsnotone(int[] ntable, int nlength)
{
int i;
for (i = nlength - 1; i >= 0; i--)
{
if (ntable[i] != 1) break;
}
return i;
}
//clean and fix length of ntable
static void swapr(int[] ntable, int ri, ref int nlength)
{
int remain = num;
for (int i = 0; i <= ri; i++)
{
remain -= ntable[i];
}
for (int i = ri + 1; i <= ri + remain; i++)
{
ntable[i] = 1;
}
nlength = ri + remain + 1;
for (int i = nlength; i < num; i++)
{
ntable[i] = 0;
}
}
static void fixdec(int[] ntable, ref int nlength)
{
int index=0;
int remain = num;
for (int i = 0; i < num; i++)
{
remain = remain - ntable[i];
if (1 == ntable[i] || 0==remain)
{
index = i;
break;
}
}
if (remain > 0)
{
nlength = index + remain + 1;
for (int i = index + 1; i < nlength; i++)
{
ntable[i] = 1;
}
for (int i = nlength; i < num; i++)
{
ntable[i] = 0;
}
}
else
{
nlength = index + 1;
}
}
static void print(int[] ntable, int nlength)
{
bool first = true;
solution++;
Console.Write("=");
for(int i = 0; i < nlength; i++)
{
if(first)
{
Console.Write(ntable[i]);
first = false;
}
else
{
Console.Write("+"+ntable[i]);
}
}
Console.Write("\n");
}
}
}
//num = 40, count = 37338, time = 00:00:06.1562500
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)
{
while (true)
{
if (index < nlength - 1)//last two element
{
ntable[index]++;
if (0 == index) swapr(ntable, rindex, ref nlength);
index++;
fixdec(ntable, ref nlength);
print(ntable, nlength);
}
else break;
}
rindex = rsnotone(ntable, nlength);
rindex = found(ntable, ntable[rindex], nlength);
if (rindex < nlength - 1)//last two element
{
ntable[rindex]++;
swapr(ntable, rindex, ref nlength);
index = rindex + 1;
fixdec(ntable, ref nlength);
print(ntable, nlength);
}
else if (rindex > 0 && ntable[rindex - 1] < ntable[0])
{
rindex = found(ntable, ntable[rindex - 1], nlength);
ntable[rindex]++;
swapr(ntable, rindex, ref nlength);
index = rindex + 1;
fixdec(ntable, ref nlength);
print(ntable, nlength);
}
else
{
index = 0;
rindex = 0;
}
}
Console.Write("num = ");
Console.Write(num);
Console.Write(", count = ");
Console.Write(solution);
DateTime StopTime = DateTime.Now;
TimeSpan duration = StopTime - StartTime;
Console.Write(", time = ");
Console.WriteLine(duration);
}
static int found(int[] ntable, int value, int nlength)
{
int i;
for (i = 0; i < nlength; i++)
{
if(value == ntable[i])break;
}
return i;
}
//reverse scan and return index that value not equal 1
static int rsnotone(int[] ntable, int nlength)
{
int i;
for (i = nlength - 1; i >= 0; i--)
{
if (ntable[i] != 1) break;
}
return i;
}
//clean and fix length of ntable
static void swapr(int[] ntable, int ri, ref int nlength)
{
int remain = num;
for (int i = 0; i <= ri; i++)
{
remain -= ntable[i];
}
for (int i = ri + 1; i <= ri + remain; i++)
{
ntable[i] = 1;
}
nlength = ri + remain + 1;
for (int i = nlength; i < num; i++)
{
ntable[i] = 0;
}
}
static void fixdec(int[] ntable, ref int nlength)
{
int index=0;
int remain = num;
for (int i = 0; i < num; i++)
{
remain = remain - ntable[i];
if (1 == ntable[i] || 0==remain)
{
index = i;
break;
}
}
if (remain > 0)
{
nlength = index + remain + 1;
for (int i = index + 1; i < nlength; i++)
{
ntable[i] = 1;
}
for (int i = nlength; i < num; i++)
{
ntable[i] = 0;
}
}
else
{
nlength = index + 1;
}
}
static void print(int[] ntable, int nlength)
{
bool first = true;
solution++;
Console.Write("=");
for(int i = 0; i < nlength; i++)
{
if(first)
{
Console.Write(ntable[i]);
first = false;
}
else
{
Console.Write("+"+ntable[i]);
}
}
Console.Write("\n");
}
}
}
//num = 40, count = 37338, time = 00:00:06.1562500
// 2025/3/10
// ChatGPT 4 Python solution:
import time
def partition(n, max_num, path, result):
""" 產生所有可能的數字拆分方式 """
if n == 0:
result.append("+".join(map(str, path))) # 找到一種組合,加入結果
return
for i in range(min(n, max_num), 0, -1): # 從 max_num 開始遞減,確保唯一性
partition(n - i, i, path + [i], result)
def count_partitions(n):
""" 計算 n 的所有拆分方法並列印 """
start_time = time.time()
result = []
partition(n, n, [], result) # 呼叫遞迴函數
for r in result:
print(r) # 列出所有拆分組合
print(f"\nnum = {n}, count = {len(result)}, time = {time.time() - start_time:.3f} sec")
# 測試
count_partitions(5)
2009年11月29日 星期日
2009年9月20日 星期日
三人編寫小說系統:一百年的小說山
幾個星期前,聽到有個英文的萬人編寫小說系統上線了。
一開始聽說成效不太好,現在小說作者也大多為一個人,
覺得這是個不錯的開始,
但是wiki系統可能要經過一些修改,再拿來寫小說會比較適合。
如果我設計一個小說系統的話,我想我可能會以三個人為單位。
即一個小說只能有三個人同時在寫,
而目前wiki discussion頁可以在分成,
討論劇情架構、討論場景設定及材料和可能用到的名言佳句及對話內容。
並且在另外加個留言板,以討論其它的相關細節,或者可以在留言相約三個人出來見面,
一起喝咖啡討論劇情。
而管理方式,可以採用自願退出,
則可以在有人退出後補足人數到三人。
又可以另外規定,若是另一個人三天以上沒參加修改,則可以由另兩人投票請他退出。
又為了避免同時有兩個人以上沒在修改,
可以在加一條規則,若是某一個人超過一個月沒在修改,系統強制將其退出該編劇專案外。
2007年,一個英文的萬人編寫小說系統上線,
觸發我想以三個人為單位來開發一個編寫小說系統,
現在雖然我還沒實際動手來開發這個系統,
不過,我想將這個系統,命名為「一百年的小說山」。
除了一開始以三個人為單位的構想外,
我想在加上這個系統中,某些文章、討論、材料…等,可以設成公開,某些可以設成隱藏,
但是如果這個小說專案過了一百年之後,那就強迫把專案內所有的文章、討論、材料…等隱藏屬性的部份都設成新公開。
這樣可以更完整看到一個小說的發展全貌。
至於這個編寫小說系統的經營模式,可以採用參與撰寫是完全免費的,
但是一個小說專案的性質對於讀者可以是免費或收費。
而網站系統經營者從收費模式中的小說專案來抽成(與編寫者拆帳),或當小說專案要發行紙本時,另外抽成。
當然經營者允許免費小說專案可變更成收費小說專案,以增加網站收入。
另外當發行紙本時,採報備制來另外抽成,如不報備就直接或小做修改發行成冊,
則要處以處罰性報復,可能是一筆鉅額的違約金,以嚇阻想偷雞的人。
以上文章,由以下兩篇整合而成(作者都為本人):
一開始聽說成效不太好,現在小說作者也大多為一個人,
覺得這是個不錯的開始,
但是wiki系統可能要經過一些修改,再拿來寫小說會比較適合。
如果我設計一個小說系統的話,我想我可能會以三個人為單位。
即一個小說只能有三個人同時在寫,
而目前wiki discussion頁可以在分成,
討論劇情架構、討論場景設定及材料和可能用到的名言佳句及對話內容。
並且在另外加個留言板,以討論其它的相關細節,或者可以在留言相約三個人出來見面,
一起喝咖啡討論劇情。
而管理方式,可以採用自願退出,
則可以在有人退出後補足人數到三人。
又可以另外規定,若是另一個人三天以上沒參加修改,則可以由另兩人投票請他退出。
又為了避免同時有兩個人以上沒在修改,
可以在加一條規則,若是某一個人超過一個月沒在修改,系統強制將其退出該編劇專案外。
2007年,一個英文的萬人編寫小說系統上線,
觸發我想以三個人為單位來開發一個編寫小說系統,
現在雖然我還沒實際動手來開發這個系統,
不過,我想將這個系統,命名為「一百年的小說山」。
除了一開始以三個人為單位的構想外,
我想在加上這個系統中,某些文章、討論、材料…等,可以設成公開,某些可以設成隱藏,
但是如果這個小說專案過了一百年之後,那就強迫把專案內所有的文章、討論、材料…等隱藏屬性的部份都設成新公開。
這樣可以更完整看到一個小說的發展全貌。
至於這個編寫小說系統的經營模式,可以採用參與撰寫是完全免費的,
但是一個小說專案的性質對於讀者可以是免費或收費。
而網站系統經營者從收費模式中的小說專案來抽成(與編寫者拆帳),或當小說專案要發行紙本時,另外抽成。
當然經營者允許免費小說專案可變更成收費小說專案,以增加網站收入。
另外當發行紙本時,採報備制來另外抽成,如不報備就直接或小做修改發行成冊,
則要處以處罰性報復,可能是一筆鉅額的違約金,以嚇阻想偷雞的人。
以上文章,由以下兩篇整合而成(作者都為本人):
多人編寫小說系統
(原創)一百年的小說山
2009年9月19日 星期六
八二三紀念公園的樹木
八二三紀念公園外圍,種的是樟樹,株高可達20公尺哦!讓我們拭目以待吧!
不好意思,照到路人…我主要是要照樹啦。
依這個比例,目前樹高大約十公尺左右,也就是還有十公尺可長,長成之後,大約會是現在的兩倍高。
成排的樟樹ㄝ,讚!
黃脈刺桐
一排黃脈刺桐
以道路上,紅衣服的路人方向為準,左手邊是黃脈刺桐,右手邊是樟樹。
印度橡膠樹,株高可達30公尺哦!
前方主体是印度橡膠樹,如果可長到30公尺的話,至少可長到目前的兩倍大!
吊呼拉圈的那個,是印度橡樛樹。
啥樹?檳榔樹?(哈,我也不知道)
猜猜看,這樹有多高呢?
可惜這個公園的水道還沒建起來,大概維護費貴的嚇人吧。
這是啥樹?榕樹?
八二三紀念公園地址如下:
2009年8月29日 星期六
2009年6月27日 星期六
2009年6月記~Happy Ending
訂閱:
文章 (Atom)
Basic Blind Chess的兩大問題 (Android version only)
Note: 以下指Andorid version pygame的 v0.8.2之前,後來Unity版出的已有改善 Basic Blind Chess已經好久沒更新了, Windows版可以獲得最好的遊戲体驗, 但是Android版的,不只是比較舊, 它其實存在兩大問題: 1. 拿...
-
關羽跟張飛都掛了 劉備找孔明訴苦 劉備:「雲長魂歸故里,如今三弟也溘然長逝,真乃大悲啊」 孔明:「甜度?」 劉備:「大悲無言」 孔明:「我是問甜度,不是加不加鹽…」
-
今天中午,跟老姐一起看一公升的眼淚DVD, 後來老媽也加入來看, 看完之後,覺得還不錯, 劇中維持日劇一貫的風格,場景不多, 音樂非常優美,出場的人物不多, 主要圍繞在亞也、亞也他媽媽和醫生、同學間, 一公升的眼淚,是在1986年出的,現在也有中譯版了, 有興趣的網友可以去 博客...
-
不曉得是Pygame本身的缺陷或bug,Pygame程式執行一段時間,必需要呼叫event一下,否則程式會變成沒有回應的情況,而程式畫面也會暫時停止更新。解決方法,就是確定程式沒互動時,get一下event,但也不是隨時都可以get event,因為當有event要處理時,例如...