JAVA基础编程题

https://blog.csdn.net/wenzhi20102321/article/details/52274976
【程序1】
题目:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子的对数是多少?(这个就是找了多年的树杈问题:假设有一棵树没有树杈,长出新树杈三个月之后变成老树杈,老树杈每个月可长出一个新树杈)

/**
 *	月份	1	2	3	4	5	6
 *	--	-	-	-	-	-	-	
 *	幼崽	1	0	1	1	2	3
 *	成年	0	1	1	2	3	5
 *	总数	1	1	2	3	5	8
 *	规律:f(n) = f(n-1) + f(n-2)
 */
public class RabbitAndTree {
	public static void main(String[] args) {
		int month = 1;
		while (month < 36) {
			System.out.println("第" + month + "月的兔子数量为: " + f(month));
			month++;
		}
	}
	static int f(int month){
		if (month <= 2) {
			return 1;
		}else {
			return f(month - 1) + f(month - 2);
		}
	}
}

输出结果

第1月的兔子数量为: 1
第2月的兔子数量为: 1
第3月的兔子数量为: 2
第4月的兔子数量为: 3
第5月的兔子数量为: 5
第6月的兔子数量为: 8
第7月的兔子数量为: 13
第8月的兔子数量为: 21
第9月的兔子数量为: 34
第10月的兔子数量为: 55

【程序2】
题目:判断101-200之间有多少个素数,并输出所有素数

public class PrimaryNum {
	public static void main(String[] args) {
		int count = 0;
		for (int i = 101; i <= 200; i++) {
			if (isPri(i)) {
				System.out.print(i + " ");
				count++;
			}
		}
		System.out.println();
		System.out.println("一共有" + count + "个素数");
	}
	
	//传入一个数,判断是否为素数(质数)
	public static boolean isPri(int n){
		boolean a = true;
		//如果还有其他两个数相乘等于n,则n不是素数
		for (int i = 2; i < n; i++) {
			for (int j = n - 1; j >= i; j--) {
				if (i*j == n) {
					a = false;
				}
			}
		}
		return a;
	}
}

程序输出

101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 
一共有21个素数

【程序3】
题目:打印出所有的“水仙花数”,即一个三位数,其各位数字立方和等于该数本身。例如:153是一个”水仙花数”,因为153=1的三次方+5的三次方+3的三次方。

public class NarcissusNum {

	public static void main(String[] args) {
		for (int i = 100; i < 1000; i++) {
			if (isNar(i)) {
				System.out.println(i+" ");
			}
		}
	}

	public static boolean isNar(int n) {
		int geweishu = n % 10;
		int shiweishu = (n - (n / 100) * 100) / 10;
		int baiweishu = n / 100;
		int newnumber = geweishu * geweishu * geweishu + shiweishu * shiweishu
				* shiweishu + baiweishu * baiweishu * baiweishu;
		if (n == newnumber) {
			return true;
		}
		return false;
	}
}

运行结果

153 
370 
371 
407 

【程序4】
题目:将一个正整数分解质因数。例如:输入90,打印90=233*5。
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n!=k,但n能被k整除(n%k == 0),则应打印出k的值,并用n除以k的商,做为新的正整数n,重复执行第一步。
(3)如果n不能被k整除,则用k+1做为k的值,重复执行第一步。

public class PrimeNum {
	public static void main(String[] args) {
		int n;
		Scanner in = new Scanner(System.in);
		System.out.print("请输入一个整数:");
		n = in.nextInt();
		System.out.print(n + " = ");
		f(n);
	}
	
	public static int f(int n){
		for (int k = 2; k < n; k++) {
			if (n%k == 0 && n != k) {
				System.out.print(k+" * ");
				return f(n/k);
			}
		}
		System.out.print(n);
		return n;
	}
}

结果

请输入一个整数:80
80 = 2 * 2 * 2 * 2 * 5

【程序5】
题目:一球从100米高度自由落下,每次落地后反弹回原高度的一半;再落下,求第10次落地时,共经过多少米?第10次反弹多高?

public class Ballfall {
	private static int times = 1;
	private static float s;
	private static float h;
	
	public static void main(String[] args) {
		//路程
		s = 0;
		//弹起高度
		h = 100;
		while (times <= 10) {
			//每次从新的最高点落下都是新的开始,一次完整的过程是落下、弹起
			//每次落下的距离h
			s = s + h;
			//每次弹起的高度
			h = h/2;
			//每次落下后弹起
			s = s + h;
			System.out.println("第"+times+"次返回的高度:"+h);
			//最后的s多算了最后一次弹起的高度
			System.out.println("第"+times+"次落地一共走过的路程:"+(s-h));
			times++;
		}
	}
}

结果

第1次返回的高度:50.0
第1次落地一共走过的路程:100.0
第2次返回的高度:25.0
第2次落地一共走过的路程:200.0
第3次返回的高度:12.5
第3次落地一共走过的路程:250.0
第4次返回的高度:6.25
第4次落地一共走过的路程:275.0
第5次返回的高度:3.125
第5次落地一共走过的路程:287.5
第6次返回的高度:1.5625
第6次落地一共走过的路程:293.75
第7次返回的高度:0.78125
第7次落地一共走过的路程:296.875
第8次返回的高度:0.390625
第8次落地一共走过的路程:298.4375
第9次返回的高度:0.1953125
第9次落地一共走过的路程:299.21875
第10次返回的高度:0.09765625
第10次落地一共走过的路程:299.60938

【程序6】
题目:猴子吃桃的问题,猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,有多吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天的一半+一个。当第10天早上想再吃时,见只有一个桃子。求第一天共摘了多少?

/**
 * 
 * 天数	1	2	3	4
 * 
 * 桃子	1	4	10	22
 *
 *	所以桃子的计算方法:前一天的桃子数*2+2
 */
public class MonkeyAndPeach {
	public static void main(String[] args) {
		int sum = 1;
		for (int i = 2; i < 10; i++) {
			sum = sum * 2 + 2;
		}
		System.out.println("猴子摘的桃子数为: " + sum);
	}
}

结果

猴子摘的桃子数为: 766

【程序7】
题目:求1!+ 2! + 3! +…+20!的和

public class Factorial {
	
	public static void main(String[] args) {
		int sum = 0;
		for (int i = 1; i <= 20; i++) {
			sum = sum + factorial(i);
		}
		System.out.println(" " + sum);
	}
	
	private static int factorial(int i){
		//求阶乘
		int mult = 1;
		for (int j = 1; j <= i; j++) {
			mult = mult * j;
		}
		return mult;
	}
}

结果

268040729

【程序】
题目:对于一个字符串,请设计一个算法,只在字符串的单词间做逆序调整,也就是说,字符串由一些由空格分隔的部分组成,需要将这些部分逆序。例,输入"I am a boy!", 输出"boy! a am I"

public class ReverseString {
	public static String Reverse(String string){

		String[] words = null;
		words = string.split(" ");
		StringBuffer sb = new StringBuffer();
		for (int i = words.length - 1; i >= 0; i--) {
			sb.append(words[i] + " ");
		}	
		return new String(sb);
	}
	public static void main(String[] args) {
		String string = "I am a boy!";
		System.out.println(Reverse(string));
	}
}

结果

boy! a am I 

【程序7】
题目:1到9999这个数组中有多少个3?例如数字303包含两个3

public class ContainsThrees {
	public static void main(String[] args) {
		int[] array = new int [10000];
		for (int i = 0; i < array.length; i++) {
			array[i] = i;
		}
		
		int count = 0;
		for (int i = 0; i < array.length; i++) {
			String string = String.valueOf(array[i]);
			char[] strings = string.toCharArray();
			for (char c : strings) {
				if (c == '3') {
					count++;
				}
			}
		}
		System.out.println(count);
	}
}

结果

4000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值