2014年3月21日 星期五

NTHU OJ - 7665 PD - Cake wants to win

The following program is my ACcepted code for NTHU-7492 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃NTHU 7492 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )

先把卡片的數字累加!
讓card[i] 代表 第一張卡片加到第i張卡片的值
之後只要決定 "開頭" 跟 "結尾" 的index
就可以用 card[結尾]-card[開頭] = 從 "開頭" 到 "結尾" 的合
所以只要用 O(N^2) 得時間複雜度

2014年3月19日 星期三

UVa OJ - 374 Big Mod

The following program is my ACcepted code for UVA-374 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃UVA 374 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )


在乘法時可以先把數字分開 mod,在乘起來 mod,這樣結果會一樣
Ex: B^P mod M = ( (B^(P-1) mod M) * (B mod M) ) mod M

所以我用Divide and Conquer來解決他
先把 B^P 分成 B^(P/2) * B^(P/2)。
(如果P為奇數就再乘一個 P)
用遞迴的方式求出答案
大概是這樣的感覺  B^P mod M = (B^(P/2) mod M) * (B^(P/2) mod M) mod M

NTHU OJ - 7492 - 最後的紅線!(II)

The following program is my ACcepted code for NTHU-7492 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃NTHU 7492 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )


一看題目就知道找兩個數字的最小公倍數... 

我用先找到最大公因數(GCD),再藉此算出最小公倍數!

2014年3月16日 星期日

Python 學習小筆記 (二) - Strings & Console Output

Python 學習小筆記 for Codecademy Python

用於自我複習跟快速入門 (給會一種以上高階語言的各位)

1.          在 print 程式碼後面打 \ 可以往下接續下面一行的程式碼,
             避免一行的一行程式過長造成閱讀困難。
             Ex: print "abcd" \
             "efg"

2.          字串型態string,可以用兩個雙引號assignEx: start = "Hello world!"
3.          字串中的跳脫符號"\" Ex: 要輸出,要在字串中打\’
4.          字串後面加上[index]會回傳字元。Ex: c = "cats" [1] (回傳a)

5.          有關字串的方法(methods)
   回傳長度len() Ex: len("fsdf") à 4
   變小寫 lower() Ex: "ABCdef".lower() à "abcdef"
   變大寫 upper() Ex: "ABCdef".upper() à "ABCDEF"
6.          轉換非字串為字串 str() Ex: 數字轉字串, str(3.14) à "3.14"

7.          基本IO: 輸出,直接打 printEx: print variavle_name
            輸出字串時可以用 + 連接。 Ex: print "A" + " and " + "B" à "A and B"
                                                               Ex: print "Pi is " + str(3.14) à "Pi is 3.14"

8.          print 一樣有標準格式輸出,像是可以用 %s 輸出字串變數
            在print輸出的變數後面需要有 %,在接上(variable)
             Ex:
             str1 = “home”
             str2 = “school”
             print "Let’s go %s, not %s." % (str1, str2)
à "Let’s go home, not school"