《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=21
60
1827=2187
2187=27
81
写一个程序,找出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
*///:~

猜你喜欢

转载自blog.csdn.net/lanzijingshizi/article/details/83989814