有些內容使用中英雙語,有些只有英文或中文。歡迎使用與分享任何內容,但先來信告知並標示此部落格為出處。
Some parts use both Chinese and English, but some parts use only one language. Feel free to share, but please contact me first and list this blog as your reference.

2014年3月29日 星期六

Python 學習小筆記 (三) - Date and Time

Python 學習小筆記 (三) - Date and Time

Python 學習小筆記 for Codecademy Python



1.        要使用時間需要import datetime 才能用
用法: from datetime import datetime

2.        datetime.now() 表示現在時間
Ex: print datetime.now() à 2014-03-28 15:51:36.039938

也能用變數來存取這個"現在時間"
Ex: now = datetime.now()

3.        datetime.now() 也可以只取出年、月、日、時、分、秒等
Ex: print datetime.now().year   à 2014
Ex: print datetime.now().month  à 3
Ex: print datetime.now().day   à 28
Ex: print datetime.now().hour   à12
Ex: print datetime.now().minute à6
Ex: print datetime.now().second à32

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,可以用兩個雙引號assign。Ex: 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: 輸出,直接打 print。Ex: 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"

2014年3月15日 星期六

Python 學習小筆記 (一) - Python Syntax and Tip Calculator

Python 學習小筆記 for Codecademy Python

用於自我複習跟快速入門,因我對C, C++, JAVA更有經驗,所以這份筆記是以學過前述語言的角度所撰,如果以前學過這三種語言之一,可能會看得比較順,如果沒有請去找正規一點的教學吧XD

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


1.          statement結尾不用加上。Ex: tmp_int = 5

2.          變數不用宣告,直接用
3.          變數型態有: int, float, bool (用True, False 而非true, false)
4.          變數命名。Ex: variable_bool = True

5.          用# 來加上一行的註解(不是用//開頭)
6.          用"""和"""來加上多行的註解 (不是使用 /* …. */,是使用三個雙引號)

7.          基本IO: 輸出,直接打 print。Ex: print variavle_name

8.          運算式,加(+) 減(-) 乘(*) 除(/) modulo求餘數(%)都一樣
   開方不同,使用兩個*。 Ex: 2 ** 3 à2的3次方 (不是 2^3)


9.          Function 不使用{},用縮排(tab or 空格)來表示
  Function EX:

  def spam():
      eggs = 12
      return eggs
  print spam()

2014年3月13日 星期四

Codeforces - 401A Vanya and Cards

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

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

2014年3月12日 星期三

[中譯題目] Codeforces - 379A New Year Candles

379 A. New Year Candles

時間限制: 每個 test case 1 秒
記憶體限制: 每個 test case 256 M

Vasily 這個程式設計師非常喜歡浪漫,所以今年他打算用蠟燭來點亮他的房間!

Vasily 有 a 個蠟燭。每當 Vasily 點燃一個新蠟燭,它會燃燒一個小時才熄滅。Vasily 非常聰明,他把 b 個熄滅的蠟燭組合成一個新的蠟燭! 這個組合而成的新蠟燭使用方法就跟其他新蠟燭一樣。

現在 Vasily 想知道: 如果他操作得當,他的蠟燭最多能照亮他的房間多久呢?

請你幫幫他找到答案吧!