搜尋此網誌

2011年7月29日 星期五

ACM-3n+1 Problem

It is a Java solution.
You should focus on the green part and fix input part for your use.


package acm;

import java.io.*;
import java.util.*;

public class ThreeNPlusOne {
public static void main(String[] args) throws IOException {
ArrayList<String> userInput = new ArrayList<String>();
int intCases = 0;
System.out.println("How many test cases to run?");
BufferedReader bs = new BufferedReader(new InputStreamReader(System.in));
String cases = bs.readLine();
intCases = Integer.parseInt(cases);
while (intCases != userInput.size()) {
System.out
.println("Please enter 2 numbers m and n,separate them by dot.");
userInput.add(bs.readLine());
}
Iterator it = userInput.iterator();
while (it.hasNext()) {
String numbers = (String) (it.next());
int[] ns = getInts(numbers);
int first = ns[0];
int second = ns[1];
System.out.println(first + " " + second + " "+ threeN(first, second));
}

}

public static int threeN(int a, int b) {
int max= 0;
for (int i = a; i <= b; ) {
int tmp=i;
int count = 0;
while (true) {
count++;
if (i == 1) {
break;
} else {
if (i % 2 != 0) {
i = 3 * i + 1;
} else {
i /= 2;
}
}
}
if (count > max) {
max = count;
}
i=++tmp;
}
return max;
}

public static int[] getInts(String str) {
int totalNumbers = str.split(",").length;
int[] ints = new int[totalNumbers];
for (int i = 0; i < totalNumbers; i++) {
ints[i] = Integer.parseInt(str.split(",")[i].trim());
}
return ints;
}

}

CPU 規格解釋

Processor Number:用來區別同一公司的不同產品的編號.

#cores:有多少個核心在一顆cpu上.

#threads:一個核心有一個process,一個process有一個thread.

clock speed:時脈,也就是頻率.
i.e:
2 MHz (2 million cycles/second):每秒執行2百萬次運算.
3 GHz (3 billion cycles/second):每秒執行30億次運算.
cpu執行的速度越快,價格當然越高.

L2 cache:cpu每次會從ram,也就是記憶體裡面拿資料出來做運算,但因為cpu速度太快了,因此處理器製造商,便在cpu和記憶體之間加了諸如L1, L2的cache(快取記憶體)來當作橋樑,也就是
cpu-L2 cache(Store data)-physical memory

FSB Speed: Front side bus又稱為north bridge or 北橋, 負責處理cpu和記憶體的溝通,
通常L2 cache包含在FSB裡面.速度當然是越快越好.

Lithography:製程. 每個製成會使用到不同的化學方法或物理方法來製造這各chip,數字越小代表需要越先進的技術來開發,想當然爾會越貴

Max TDP(Thermal design power):cpu運算會產生熱,這個數字代表冷卻系統需要耗費多少瓦特的電力來散熱,所以越快的cpu比起低階的cpu,會更快產生更多的熱,因此也就耗電.

部落格經營記事--當月瀏覽人次首度超過100

賀,我的部落格這個月竟然瀏覽人次超過100哩,超乎我預期,之前有一搭沒一搭的寫,所以來逛的人也

是稀稀疏疏的,因此我下定決心每天至少要發2~3篇文章才可以,我把部落格當成小花圃一般來照料,

果然有付出,月底就看到收穫了.很高興,要再接再厲的發文才行.