Re: [問題] 我這樣很爛嗎?

看板C_and_CPP作者 (喲)時間11年前 (2012/09/04 07:43), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串8/10 (看更多)
※ 引述《skizzy (活著是件難過的事)》之銘言: : 把我應徵某公司考題的答案在這裡給這邊的大大鞭策 : 使用編譯程式 codeblock C++ : 1. There are four digits on the face of a 12‐hr digital clock. How many : times in oneday (24 hours) can you see three or more of the same number in a : row (e.g.,01:11, 02:22, etc) on the clock face? In addition, please use any : programming language, pseudo‐code, or flow‐chart to write an algorithm to : compute the answer. 我覺得在此解題,主要是告訴閱讀者這題求解的方法. 所以,不要寫迴圈瞎算,也不要把你認定的唯一解硬寫在程式裏. 而是要把解題概念寫進程式中. 所以是這樣: 小時部份很清楚,只有 01, 02, 03, ..., 11, 12. 其中有個數字 11 特別,是二個相同的數字. 題目說要找到每個至少三個同樣數字的鐘面,所以,以 01 來說, 就是對 0 ,在分鐘部份找至少二個 0 的鐘面數字, 並且對 1 也找至少二個 1 的鐘面數字. 這樣說來,我需要一個函數: 求至少 n 個 d ,但上限 limit 個同數字, 可以產生多少種情況, int or_more(int n, int d, int limit) { int i; int sum = 0; if ((n == 1 || n == 2) && limit == 2) if (d >= 0 && d <= 5) if (n == 1 && limit == 2) sum += 10 + 6; else sum += 10 + 6; else if (d == 6) sum += 1 + 6; else if (d >= 7 && d <= 9) sum += 6; return sum; } 要寫進程式的邏輯則是: 1. 看到小時區域二個數字不同, 二個數字分別為 d, k, 則這個小時看得到三個 相同數字的情況有 or_more(2, d, 2) + or_more(2, k, 2) 種. 2. 看到小時區域二個數字相同, 則這個小時看得到三個相同數字的情況有 or_more(1, 1, 2) 種. 3. 午後部份與午前相同. 所以程式我會寫成: int i; int sum = 0; for (i=1; i<=12; i++) { if (i < 11) sum += or_more(2, 0, 2) + or_more(2, i, 2); else if (i == 11) sum += or_more(1, 2); else sum += or_more(2, 1, 2) + or_more(2, 2, 2); } sum *= 2; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.167.52.7 ※ 編輯: yauhh 來自: 118.167.52.7 (09/04 08:08)

09/04 15:11, , 1F
坦謝分享ˊˋ
09/04 15:11, 1F
文章代碼(AID): #1GHK2hsp (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1GHK2hsp (C_and_CPP)