public class PalindromeNumber2 { /*產生一位元字的迴文數*/ private void adigit(long parentNumber, long tenID) { long tenIDResult = (long) Math.pow(10, tenID); for (int i = 0; i <= 9; i++) { System.out.println((long)
/*一球從100米高度自由落下,每次落地後反跳回原高度的一半;再落下,求它在第10次落地時,共經過多少米。第10次反彈多高這裡用BigDecimal避免精度丟失*/public class FreeFallingBody { public void tradition(){ //傳統演算法 迴圈10次計算這裡用BigDecimal避免精度丟失 BigDecimal distance = new BigDecimal(100.0);
/** * * @author Fly */public class InsertSort { public int[] insertSort(int[] a) { int size = a.length; int j; for (int i = 1; i < size; i++) { int temp = a[i]; for (j = 0; j < i; j++) {
/** * * @author Fly */public class QuickSort { public void quicksort(int[] a, int f, int e) { int left = f, right = e; if (f < e) { while (f < e) { while (a[f] < a[e] && f < e) {
例子,假設是個賣票系統,多個線程(例子中有2個線程)操作同一個變數ticketCount(餘票的數量)。 Expected : 多個線程對同個變數會同步操作,不會造成混亂。 public class Test implements Runnable { private String threadName; private int ticketCount; public Test(String threadName,
5隻猴子摘了一堆桃子,約好第二天早上來分。第一隻猴子來得早,它將桃子平分成成5堆,多出1個,它把多出的一個吃了,把屬於自己的一堆拿走了,將剩下的還混成一堆,其他猴子來了也正好按一樣的方法處理。 編程求出原來最少有多少個桃子。(用遞迴函式。) 下面是我寫的遞迴演算法: public class PeachDivideAlgorithm { private