天天看點

《Java程式設計思想》第4章練習題

源碼位址:https://github.com/yangxian1229/ThinkingInJava

練習1:寫一個程式,列印從1到100的值

練習2:寫一個程式,産生25個int類型的随機數。對于每一個随機值,使用if-else語句來将其分類為大于,小于或等于緊随它而随機生成的值。

練習3:修改練習2,把代碼用一個while無限循環包括起來。然後運作它直至用鍵盤中斷其運作。

練習4:寫一個程式,使用兩個嵌套的for循環和取餘操作符(%)來探測和列印素數。

練習5:重複第3章中的練習10,不要用Integer.toBinaryString()方法,而是用三元操作符和按位操作符來顯示二進制的1和0.

package ch4;
import static net.mindview.util.Print.*;
public class E05 {
	static void toBinaryString(int a){
		char buffer[] = new char[32];
		int position = 32;
		do{
			buffer[--position] = 
					((a & 0x01) != 0) ? '1' :'0';
			a >>>= 1;
		}while(a!=0);
		for(int i=position;i<32;i++)
			printnb(buffer[i]);
		print();
	}
	public static void main(String[] args) {
		int i1 = 0xaaaaaaaa;
		int i2 = 0x55555555;
		
		printnb("i1 = "); toBinaryString(i1);
		printnb("i2 = "); toBinaryString(i2);
		printnb("~i1 = "); toBinaryString(~i1);
		printnb("~i2 = "); toBinaryString(~i2);
		printnb("i1 & i1 = "); toBinaryString(i1 & i1);
		printnb("i1 | i1 = "); toBinaryString(i1 | i1);
		printnb("i1 ^ i1 = "); toBinaryString(i1 ^ i1);
		printnb("i1 & i2 = "); toBinaryString(i1 & i2);
		printnb("i1 | i2 = "); toBinaryString(i1 | i2);
		printnb("i1 ^ i2 = "); toBinaryString(i1 ^ i2);
	}
}
           

練習6:修改前兩個程式的test()方法,讓它們接受兩個額外的參數begin和end,這樣在測試testval時判斷它是否在begin和end之間(包括begin和end)的範圍内。

練習7:修改本章練習1,通過使用break關鍵詞,使得程式在列印到99時退出。然後嘗試使用return來達到相同的目的。

練習8:略。

練習9:一個斐波那契數列是由數字1,1,2,3,5,8,13,21,34等等組成的,其中每一個數字(從第三個數字起)都是由前兩個數字的和。建立一個方法,接受一個整數參數,并顯示第一個元素開始總共由該參數指定的個數所構成的所有斐波那契數字。

package ch4;

public class E09 {
	static void fibonacci(int n){
		int f[] = new int[n];
		f[0]=f[1]=1;
		System.out.print(f[0]+" "+f[1]+" ");
		for(int i=2;i<n;i++){
			f[i]=f[i-1]+f[i-2];
			System.out.print(f[i]+" ");
		}
	}
	//遞歸
	static int fib(int n){
		if(n<2)
			return 1;
		else
			return fib(n-1)+fib(n-2);
	}

	public static void main(String[] args) {
		fibonacci(20);
		System.out.println();
		int n = 20;
		for(int i=0;i<n;i++){
			System.out.print(fib(i)+" ");
		}

	}

}/* Output:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 
*/
           

練習10:吸血鬼數字是指位數為偶數的數字,可以由一對數字相乘而得到,而這對數字各包含乘積的一半位數的數字,其中從最初數字中選取的數字可以任意排序。以兩個0結尾的數字是不允許的,例如,下列數字都是“吸血鬼”數字:

1395=1593

1260=2160

1827=2187

2187=2781

寫一個程式,找出4位數的所有吸血鬼數字。

package ch4;

public class E10 {
	public static void main(String[] args) {
		int startDigit[] = new int[4];
		int productDigit[] = new int[4];
		
		for(int num1=10;num1<=99;num1++){
			for(int num2=num1;num2<=99;num2++){
				// Pete Hartley's theoretical result:
				//If x·y is a vampire number then x·y == x+y (mod 9)
				if((num1*num2)%9 != (num1+num2)%9)
					continue;
				int product = num1*num2;
				startDigit[0] = num1/10;
				startDigit[1] = num1%10;
				startDigit[2] = num2/10;
				startDigit[3] = num2%10;
				productDigit[0] = product/1000;
				productDigit[1] = (product%1000)/100;
				productDigit[2] = product%1000%100/10;
				productDigit[3] = product%1000%100%10;
				int count=0;
				for(int i=0;i<4;i++){
					for(int j=0;j<4;j++){
						if(startDigit[i] == productDigit[j]){
							count++;
							startDigit[i] = -1;
							productDigit[j] = -2;
							if(count == 4){
								System.out.println(product+"="+num1+"*"+num2);
								break;
							}
						}
					}
				}
			}
		}
	

	}

}/* Output:
1395=15*93
1260=21*60
1827=21*87
2187=27*81
1530=30*51
1435=35*41
6880=80*86
*///:~