發表文章

目前顯示的是 10月, 2008的文章

[發佈] NTU-New-Books-Feeds

圖片
想知道台大圖書館新進了哪些新書嗎 推薦給館方購買的書總是來不及第一個看到嗎 臺大新書推薦服務小工具新發佈 絕對是你最佳的選擇 小工具預覽圖: 點圖或者 這裡 連結到這個小工具在google的網頁 使用步驟如下: 1. 這個RSS小工具特別針對iGoogle首頁開發 所以想要使用的話第一步得先擁有google帳號 如果沒有帳號可以按 這裡 申請 2. 有了帳號之後點下 將小工具加入iGoogle 可視情況選擇是否將 iGoogle 設為首頁 加入後的小工具會出現在 http://www.google.com/ig 3. 加入小工具後可以點右上角的倒三角形變更設定 顯示篇數可設定小工具上顯示的新書介紹篇數 文章顯示方式設定為標題時只顯示新書書名超連結 設定為首段時會有書的封面以及簡單的資訊 設定為全文的話將有簡介或書評 以上三個簡單步驟就是使用這個小工具的說明 另外若是覺得這個小工具不錯也可以把它內嵌在blog或個人網頁喔 只要到 這裡 就可以設定長寬顏色邊框等等 最後再按下取得程式碼將程式碼貼上自己的網頁即可 做出來的效果會像下面一樣

走訪List的方法

List的功能非常強大 而且又可以放入所需要的類別所以最近常常出現在程式之中 通常我們都會需要循序把List中的所有資料抓出來處理 這種模式好像可以叫做走訪或者循序訪問 英文大概叫做iterate來著 以下程式示範了三種走訪List的方法 第一個是平常我們常用的for loop 第二個則是使用迭代子Iterator 關於迭代子Iterator的用法可以參考 Iterator模式 第三種則是Java 1.5之後才支援的Enhanced for loop 這簡化了我們原本使用for loop的繁複 用法可見 Java 1.5 特性 : Enhanced for Loop 最後一小段程式則是示範Enhanced for Loop應用在Array的用法 也就是說Enhanced for Loop不只可以用在List類別上 程式碼: import java . util . ArrayList ; import java . util . Iterator ;   public class IteratorDemo {   public static void main ( String [] args ) {   ArrayList < String > stringList = new ArrayList < String >(); stringList . add ( "ant" ); stringList . add ( "bear" ); stringList . add ( "cat" ); stringList . add ( "dog" );   // 一般使用for loop走訪的方法 for ( int i = 0 ; i < stringList . size (); i ++) { System . out . println ( stringList . get ( i )); }   // 使用iterator走訪 Itera

在網頁中嵌入顯示程式碼:HighLight 軟體

圖片
(2012.02.05更新) 全系列顯示效果比較已完成,可參考: 在網頁中嵌入顯示程式碼:全系列效果比較及教學整理 , 選擇自己較想要的效果後再進入單篇文章看每種外掛的設定教學。 (2012.2.3重新編輯整理) 工作了將近三個月其實學到了非常多東西, 最近才突然發覺應該要好好做些記錄下來, 一方面可以讓自己回顧之前的成果, 一方面也可以把新學到的東西做些 memo, 這樣下次才不會找不到又得從頭摸起, 等 blog 內容充實後說不定也可以讓有相同問題的網友搜尋到解決之道 :p。 因為有了這樣的想法, 所以第一個產生的需求就是要將程式碼放到網頁上, 讓有相同問題的網友能藉由程式碼來了解怎麼解決問題, 否則只講解觀念大概還是很難讓以後的自己或是別人理解吧。 後來找到最讓我滿意的方法是用軟體來解決, 主要的原因是因為他的輸出可以選擇不同的 IDE 效果, 像我一直使用 eclipse,所以看到輸出同樣的效果就覺得很親切。 如果有使用其他的 IDE 也可以試試,應該也有 NetBean 等其他的顯示效果。 安裝這個軟體後便可以讀取程式碼把 code 轉換為 html 格式, 除了 html 外,也還可以轉成 LaTeX, SVG and BBCode 等, 若看得懂英文可以直接上官方網站看 manual: Highlight Code Converter , 不喜歡英文的話可以參考別人的教學: HighLight:程式碼顯示的美化工具 。 下面是  Highlight Code Converter  這個軟體操作預覽圖: HighLight這個程式十分簡便又很好用, 設定好輸出格式後再按 convert files 即可在設定的輸出資料夾看到html檔, 如果無法在 blog上嵌入 css 檔的話請勾選: 1. Enbed style in output 和 2. Inline CSS 再來只要把輸出的 html 以文字編輯器開啟, 複製全部後再到貼到部落格即可。 另外有個選項是 output as ordered list, 可以讓輸出程式碼時用 li 的 html tag顯示行號, 這麼做的好處是讀者在複製程式碼時不會複製到行號, 但在 firefox 和 chrome 中似乎得

將List做排序的方法

有時候我們會需要將一個List做排序 List裡頭可能是單一基本型別如int, String等等 當然也有可能是自訂的類別 無論是對基本型別或自訂的類別作排序 我們都可以透過Collections.sort(List l, Comparator c)方法來做排序 而其中的Comparator就是排序的關鍵 以下示範一個 StringIntPair 類別 裡頭包含了 String 及 int 兩種資料類型 透過實作不同的Comparator我們就可以將同一個StringIntPair的List做不同的排序 分別是依 String 及 int大小排列 而可以保持String和int資料的對應 下面是 StringIntPair 類別的程式碼: public class StringIntPair { private String string ; private int integer ; public StringIntPair ( String s , int i ) { string = s ; integer = i ; } protected String getString () { return string ; } protected int getInteger () { return integer ; } public String toString () { return string + " \t " + integer ; } } 接著是執行 List Sort 的程式碼 import java . util . ArrayList ; import java . util . Collections ; import java . util . Comparator ; import java . util . List ; public class ListSort { public static void main ( String [] args ) { List <

Java:JSON.jar 下載與JSON source code 打包教學

圖片
JSON(JavaScript Object Notation) 在資料處理交換上是很方便的格式, 因為他是純文字的格式,所以支援了非常多的程式語言, 也因此和 xml 一樣常被用在 web services 和各種程式語言之間的溝通。 關於 JSON 的格式長怎樣還有他所支援的程式語言, 都可以在  http://www.json.org/  看到介紹。 圖片來源:http://www.exclusivetutorials.com/an-introduction-to-json/ (本文已於2017/10/27重新更新連結及下載點) 如果要將 JSON 應用在 Java 的環境中先到  JSON-java  看一下, 裡面有每個 java class 的功能介紹、API Javadoc、Source Code下載以及 Licience聲明。 當然如本篇的標題所說,這篇的重點是在 source code 的打包教學, 通常實際在使用這類功具性的 library 時, 我們會比較習慣 import .jar 檔而不是直接把 .java檔放入 project 中。 但是因為上面的  JSON-java  僅提供  JSON-java原始檔  而沒有提供 .jar下載, 所以我在網路上查了一下找到了打包 JSON.jar 的方法。 以下是打包的步驟: 從  JSON-java  下載 Source Code 建立資料夾路徑 xxx/org/json/,其中 xxx 可以自訂,org/json/ 不可更改! 解壓縮 zip 檔裡的 JSONArray.java 等所有 java 檔至 xxx/org/json/ 底下,test資料夾不用。 windows執行cmd,Linux執行terminal,切換至 xxx/。輸入指令 javac ./org/json/ *.java 。 在 xxx/ 目錄下繼續輸入指令 jar -cvf json.jar ./org/json/*.class 。 在目錄 xxx/ 底下看到 json.jar 就可以拿去 import 使用了 以下再附上圖示: 資料夾結構及檔案放置路徑: Windows cmd 指令: 如果大家不想自己打包而想直接下載 jar 檔, 也

10929 - You can say 11

UVa網站題目連結 My Solved Problems Performance Introduction to the problem Your job is, given a positive number N, determine if it is a multiple of eleven. Description of the input The input is a file such that each line contains a positive number. A line containing the number 0 is the end of the input. The given numbers can contain up to 1000 digits. Description of the output The output of the program shall indicate, for each input number, if it is a multiple of eleven or not. Sample input: 112233 30800 2937 323455693 5038297 112234 0 Sample output 112233 is a multiple of 11. 30800 is a multiple of 11. 2937 is a multiple of 11. 323455693 is a multiple of 11. 5038297 is a multiple of 11. 112234 is not a multiple of 11. Solution import java . io . BufferedInputStream ; import java . math . BigInteger ; import java . util . Scanner ; public class Main { public static void main ( String [] args ) { Scanner in = new Scanner ( new BufferedInputStream ( System . in ));

10071 - Back to High School Physics

UVa網站題目連結 My Solved Problems Performance A particle has initial velocity and constant acceleration. If its velocity after certain time is v then what will its displacement be in twice of that time? Input The input will contain two integers in each line. Each line makes one set of input. These two integers denote the value of v (-100 <= v <= 100) and t(0<=t<= 200) ( t means at the time the particle gains that velocity) Output For each line of input print a single integer in one line denoting the displacement in double of that time. Sample Input 0 0 5 12 Sample Output 0 120 Solution import java . io . BufferedInputStream ; import java . util . Scanner ; public class Main { public static void main ( String args []) { Scanner in = new Scanner ( new BufferedInputStream ( System . in )); while ( in . hasNext ()) { System . out . println ( 2 * in . nextInt ()* in . nextInt ()); } } } 關鍵字:UVa Online Ju

10055 - Hashmat the Brave Warrior

UVa網站題目連結 My Solved Problems Performance Hashmat is a brave warrior who with his group of young soldiers moves from one place to another to fight against his opponents. Before fighting he just calculates one thing, the difference between his soldier number and the opponent's soldier number. From this difference he decides whether to fight or not. Hashmat's soldier number is never greater than his opponent. Input The input contains two integer numbers in every line. These two numbers in each line denotes the number of soldiers in Hashmat's army and his opponent's army or vice versa. The input numbers are not greater than 2^32. Input is terminated by End of File. Output For each line of input, print the difference of number of soldiers between Hashmat's army and his opponent's army. Each output should be in seperate line. Sample Input: 10 12 10 14 100 200 Sample Output: 2 4 100 Solution import java . io . BufferedInputStream ; import java .

264 - Count on Cantor

圖片
UVa網站題目連結 My Solved Problems Performance One of the famous proofs of modern mathematics is Georg Cantor's demonstration that the set of rational numbers is enumerable. The proof works by using an explicit enumeration of rational numbers as shown in the diagram below. In the above diagram, the first term is 1/1, the second term is 1/2, the third term is 2/1, the fourth term is 3/1, the fifth term is 2/2, and so on. Input and Output You are to write a program that will read a list of numbers in the range from 1 to and will print for each number the corresponding term in Cantor's enumeration as given below. No blank line should appear after the last number. The input list contains a single number per line and will be terminated by end-of-file. Sample input 3 14 7 Sample output TERM 3 IS 2/1 TERM 14 IS 2/4 TERM 7 IS 1/4 Solution import java . io . BufferedInputStream ; import java . util . Scanner ; public class Main { public static void main ( String

113 - Power of Cryptography

圖片
UVa網站題目連結 My Solved Problems Performance Background Current work in cryptography involves (among other things) large prime numbers and computing powers of numbers modulo functions of these primes. Work in this area has resulted in the practical use of results from number theory and other branches of mathematics once considered to be of only theoretical interest. This problem involves the efficient computation of integer roots of numbers. The Problem Given an integer and an integer you are to write a program that determines , the positive root of p . In this problem, given such integers n and p , p will always be of the form for an integer k (this integer is what your program must find). The Input The input consists of a sequence of integer pairs n and p with each integer on a line by itself. For all such pairs , and there exists an integer k , such that . The Output For each integer pair n and p the value should be printed, i.e., the num