2010年1月16日 星期六

用wxPython 寫Hello World

wxPython是Python編程語言的一個GUI工具箱。他使得Python程序員能夠輕松的創建具有健壯、功能強大的圖形用戶界面的程序。

wxPython 專為 Python 程式語言所設計, 必須先安裝 Python 之後,再安裝 wxPython。
wxPython 官方網站:http://www.wxpython.org

個視窗的組成,主要包括「Frame」(框架)、「Panel」(面板)、「Controls」(控制項)


2010年1月14日 星期四

102 - Ecological Bin Packing

Background

Bin packing, or the placement of objects of certain weights into different bins subject to certain constraints, is an historically interesting problem. Some bin packing problems are NP-complete but are amenable to dynamic programming solutions or to approximately optimal heuristic solutions.
In this problem you will be solving a bin packing problem that deals with recycling glass.

The Problem

Recycling glass requires that the glass be separated by color into one of three categories: brown glass, green glass, and clear glass. In this problem you will be given three recycling bins, each containing a specified number of brown, green and clear bottles. In order to be recycled, the bottles will need to be moved so that each bin contains bottles of only one color.
The problem is to minimize the number of bottles that are moved. You may assume that the only problem is to minimize the number of movements between boxes.
For the purposes of this problem, each bin has infinite capacity and the only constraint is moving the bottles so that each bin contains bottles of a single color. The total number of bottles will never exceed 2^31.

在回收玻璃瓶時我們根據其顏色分為三類:棕色、綠色、和透明三種。在問題當中會 有三個回收桶,裡面含有若干個上述三種顏色的瓶子。為了要能徹底做好回收,我們 必須要將一些瓶子從某個回收桶拿出放到另外一個,使得到最後每個桶子裡都只有一 種顏色的瓶子。
我們現在所要做的就只有一個目的,讓移動瓶子的步數盡可能地弄到最少。
我們可以假設這些回收桶都具有無限的容量,所以我們唯一要關心的就只有 如何操作,讓每個桶子裡都只含有一種顏色的瓶子。瓶子的總數量不會超過 2 的 31 次方。

2010年1月13日 星期三

ACM International Collegiate Programming Contest國際型的程式競賽

隨意逛網時,看到的網站ACM International Collegiate Programming Contest國際型的程式競賽, 網站提供了許多的題目,可以讓使用者註冊,並且在線上測試結果,這樣就不怕沒題目好練習。

目前該網站更換新的網站,網址為:http://uva.onlinejudge.org/

找了一題簡單的用Python試解玩玩
#100 ─ The 3n+1 problem

各種程式語言 Hello World 比較…

------------------------------------------------------------

>>> C

#include

main()
{
   printf ("Hello,World!\n");
}

------------------------------------------------------------

Python 概述

Python([KK] 英語發音: /'paɪθɑn/, [DJ] 英語發音: /ˈpaiθən/),是一種物件導向直譯式電腦程式語言,也是一種功能強大而完善的通用型語言,已經具有十多年的發展歷史,成熟且穩定。
這種語言具有非常簡捷而清晰的語法特點,適合完成各種高層任務,幾乎可以在所有的作業系統中執行。

2010年1月10日 星期日

猜數字遊戲(一)

# -*- coding: utf-8 -*-

# Python version: 2.5.4


import random


ans = random.randint(0, 3)

count= 0

num = 0


2010年1月9日 星期六

九九乘法表

# -*- coding: utf-8 -*-
# Python version: 2.5.4

"""九九乘法表"""


範例一、
# -*- coding: utf-8 -*-
# Python version: 2.5.4


for i in range(1, 10):

    for j in range(1, 10):
        print i ,'*', j ,'=' ,i*j

python 寫 剪刀、石頭、布

# -*- coding: utf-8 -*-
# PYTHON version: 2.5.4

import random

hand = ['剪刀', '石頭', '布']

pwin = 0
cwin = 0

c = random.choice(hand)
pc = input('請選擇-0(剪刀),1(石頭),2(布):')
p = hand[pc]

while pwin < 3 and cwin < 3:
   print '目前成績: 電腦贏了', cwin, '次'
   print ' 玩家贏了', pwin, '次\n\n'

   c = random.choice(hand)
   pc = input('請選擇-0(剪刀),1(石頭),2(布):')
   p = hand[pc]
   if c == '剪刀' and p == '布' or c == '石頭' and p == '剪刀' or c == '布' and p == '石頭':
      print '電腦出:', c, '玩家出:', p
      print 'computer win!'
      cwin += 1

   elif c == '剪刀' and p == '石頭' or c == '石頭' and p == '布' or c == '布' and p == '剪刀':
      print '電腦出:', c, '玩家出:', p
      print 'player win!'
      pwin += 1

   elif c == '剪刀' and p == '剪刀' or c == '石頭' and p == '石頭' or c == '布' and p == '布':
      print '電腦出:', c, '玩家出:', p
      print '平手!!'

if pwin > cwin:
   print '恭禧!玩家勝出!'
else:
   print '你輸了!!'