【程序1】
題目:古典問題:有一對兔子笋妥,從出生后第3個月起每個月都生一對兔子春宣,小兔子長到第三個月后每個月又生一對兔子嫉你,假如兔子都不死幽污,問每個月的兔子總數(shù)為多少?
private static int Fb(int i) {
//不允許小于0的數(shù)
if (i <= 0) {
return -1;
}
//Fb(1)=1, Fb(2)=1
if (i == 1 || i == 2) {
return 1;
}
//遞歸調(diào)用
return Fb(i-1) + Fb(i-2);
}
public static void main(String[] args) {
int M=6; //輸入第幾個月簸搞,自填
System.out.print("Rabbit total: " + Fb(M));
}
【程序2】
題目:判斷101-200之間有多少個素數(shù)准潭,并輸出所有素數(shù)。
解析:質數(shù)又稱素數(shù)寺擂。一個大于1的自然數(shù),除了1和它自身外垦细,不能整除其他自然數(shù)的數(shù)叫做質數(shù)挡逼。
public static void main(String[] args) {
int count = 0;
for(int i=101; i<=200; i++) { //遍歷從101到200所有自然數(shù)
for(int j=2; j < i; j++) { //從2到i-1遍歷
if (i%j == 0) { //除了1和自身外挚瘟,還能被整除饲梭,不是素數(shù)憔涉,跳出內(nèi)循環(huán)。
break;
}
if (j == (i-1)) { //如果j指針遍歷到i-1穿扳,仍然不能被整除国旷,則認為i是素數(shù)
count ++;
System.out.print(i + ", ");
}
}
}
System.out.print("\nTotal: " + count);
}
輸出結果:
101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
Total: 21
算法改進:
第一個對i的for循環(huán)跪但,偶數(shù)肯定不是素數(shù),直接跳過忆首,只遍歷奇數(shù)被环,每次+2;
第二個對j的for循環(huán)浸锨,不需要把i的值完全遍歷悴能,只需要遍歷到i的開平方根即可漠酿,即Math.sqrt(i)。
代碼改進:
public static void main(String[] args) {
int count = 0;
for(int i=101; i<=200; i+=2) { //遍歷從101到200所有奇數(shù)
boolean flag = true;
for(int j=2; j <= Math.sqrt(i); j++) { //從2到sqrt(i)遍歷
if (i%j == 0) { //除了1和自身外宇姚,還能被整除,不是素數(shù)阱持,跳出內(nèi)循環(huán)魔熏。
flag = false;
break;
}
}
if (flag == true) {
count ++;
System.out.print(i + ", ");
}
}
System.out.print("\nTotal: " + count);
}
【程序3】
快速找出一個數(shù)組中的兩個數(shù)字蒜绽,讓這兩個數(shù)字之和等于一個給定的值、
例如給定一個無序數(shù)組:{3, 7, 10, 4, 9, 2, 5, 6}鼎姊,找出其中兩個數(shù)字之和等于10
//冒泡排序相寇,使其變成一個從小到大的有序序列
private static void bubbleSort(int[] list) {
int temp;
for (int i=0; i<list.length; i++) {
for (int j=list.length-1; j>i; j--) {
if (list[j] < list[j-1]) {
temp = list[j-1];
list[j-1] = list[j];
list[j] = temp;
}
}
}
}
private static void sum(int[] list) {
int i = 0;
int j = list.length-1;
while (i < j) {
if (list[i] + list[j] == 10) {
System.out.print( list[i] + " + " + list[j] + " = 10");
return;
} else if (list[i] + list[j] > 10) {
j --;
} else if (list[i] + list[j] < 10) {
i ++;
}
}
System.out.print( "No number equals 10 in array" );
}
public static void main(String[] args) {
int[] list = {3, 7, 10, 4, 9, 2, 5, 6};
//排序
bubbleSort(list);
//求和
sum(list);
}
【程序4】一個數(shù)組唤衫,負數(shù)放左邊跺嗽,正數(shù)放右邊
例如:給定數(shù)組{-3, 7, 10, -4, -9, 2, 5, -6}
private static void sort(int[] list) {
//設置左右指針,i從左遍歷植兰,j從右遍歷
int i = 0;
int j = list.length-1;
//i始終要小于j楣导,否則數(shù)組越界
while (i < j) {
//若list[i] 為負數(shù)畜挨,繼續(xù)向右遍歷
while (i < list.length && list[i] < 0) {
i ++;
}
//若list[i] 為負數(shù),繼續(xù)向左遍歷
while (j >= 0 && list[j] > 0 ) {
j --;
}
//i始終要小于j
if (i < j) {
//正負數(shù)交換
swap(list, i, j);
}
}
}
private static void swap(int[] list, int left, int right) {
int temp;
temp = list[right];
list[right] = list[left];
list[left] = temp;
}
public static void main(String[] args) {
int[] list = {-3, 7, 10, -4, -9, 2, 5, -6};
sort(list);
for (int i=0; i<list.length; i++) {
System.out.print( list[i] + ", " );
}
}
【程序5】找出數(shù)組中重復次數(shù)最多的數(shù),要求時間復雜度O(n)
public static void main(String[] args) {
int[] arr = {1,1,2,2,3,4,4,4,4,5,5,5,6,6};
//以arr數(shù)組中的數(shù)作為key逮刨,重復一次value值遞增,最后統(tǒng)計key對應最大的value值
//即為重復次數(shù)最多的數(shù)
Map<Integer, Integer> map = new HashMap<Integer, Integer>( );
for(int i=0; i<arr.length; i++) {
if (map.containsKey( arr[i] )) {
map.put( arr[i], map.get( arr[i] ) + 1 );
} else {
map.put( arr[i], 1 );
}
}
//找出map中最大value值對應的key
Collection<Integer> count = map.values();
int maxCount = Collections.max( count );
int maxKey = -1;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() == maxCount) {
maxKey = entry.getKey();
}
}
System.out.print( "Number: " + maxKey );
}
【程序6】將兩個排好序的鏈表合并成一個有序鏈表迎罗,要求時間復雜度O(n)
//定義單鏈表片仿,數(shù)據(jù)域data砂豌,next 指針
static class ListNode {
int data;
ListNode next;
public ListNode (int data) {
this.data = data;
}
}
public static void main(String[] args) {
//初始化第一個單鏈表:1->3->7->9
ListNode list_node = new ListNode( 1 );
ListNode l2 = new ListNode( 3 );
list_node.next = l2;
ListNode l3 = new ListNode( 7 );
l2.next = l3;
ListNode l4 = new ListNode( 9 );
l3.next = l4;
printListNode(list_node);
System.out.print( "\n" );
//初始化第二個單鏈表:2->4->6->8
ListNode node = new ListNode( 2 );
ListNode node2 = new ListNode( 4 );
node.next = node2;
ListNode node3 = new ListNode( 6 );
node2.next = node3;
ListNode node4 = new ListNode( 8 );
node3.next = node4;
printListNode(node);
System.out.print( "\n" );
ListNode merge = mergingSort(list_node, node);
printListNode(merge);
}
private static ListNode mergingSort (ListNode listNode1, ListNode listNode2) {
//如果其中一條鏈表為null阳距,無需合并,直接返回另外一條鏈表
if (listNode1 == null) {
return listNode2;
} else if (listNode2 == null) {
return listNode1;
}
//定義一個臨時鏈表存儲
ListNode mergedNode = new ListNode( 0 );
ListNode temp = mergedNode;
//遍歷鏈表1和鏈表2,data更小的數(shù)復制到臨時鏈表中蓄拣,知道其中一個鏈表遍歷完努隙,跳出循環(huán)
while (listNode1 != null && listNode2 != null) {
if (listNode1.data <= listNode2.data) {
temp.next = listNode1;
listNode1 = listNode1.next;
} else {
temp.next = listNode2;
listNode2 = listNode2.next;
}
temp = temp.next;
}
//如果其中一條鏈表未遍歷完荸镊,直接合并到臨時鏈表尾部
if (listNode1 != null) {
temp.next = listNode1;
} else if (listNode2 != null) {
temp.next = listNode2;
}
return mergedNode.next;
}
//輸出鏈表
private static void printListNode (ListNode node) {
if (node == null) {
return;
}
while (node != null) {
System.out.print( node.data );
node = node.next;
if (node != null) {
System.out.print( " -> " );
}
}
}
【測序7】打印出所有的 "水仙花數(shù) "躬存,所謂 "水仙花數(shù) "是指一個三位數(shù),其各位數(shù)字立方和等于該數(shù)本身岭洲。例如:153是一個 "水仙花數(shù) "盾剩,因為153=1的三次方+5的三次方+3的三次方。
public static void main(String[] args) {
int a; //百分位
int b; //十分位
int c; //個位
for (int i=101; i<1000; i++) {
a = i/100;
b = (i%100)/10;
c = (i%100)%10;
if (i == Math.pow( a, 3 ) + Math.pow( b, 3 ) + Math.pow( c,3 )) {
System.out.println( i );
}
}
}