`
iluoxuan
  • 浏览: 571290 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

基础题目

 
阅读更多

 

package com.ljq.base;

/**
 * 90 = 2*3*3*5
 *
 * @author ljq
 * @version 1.0
 * @since 1.0
 */
public class Factorization {

	public static void factorization(int n) {
		StringBuilder sb = new StringBuilder();
		if (n == 0) {
			System.out.println(0);
		}
		for (int i = 2; i <= n; i++) {
			while (n != 0) {
				if (n % i == 0) {
					n = n / i;
					sb.append(i).append("*");
				} else {
					break;
				}
			}
		}
		sb.deleteCharAt(sb.length() - 1);
		System.out.println(sb.toString());
	}

	public static void main(String[] args) {
		factorization(5);
	}
}
package com.ljq.base;

/**
 * 有一对兔子,从出生后第3个月起每个月都生一对兔子,
 * 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
 *   1 =  1
 *   2 =  1
 *   3 =  1+ 1 =2 
 *   4 =  1 + 1+ 1=3
 *   5 =  1+ 1+ 1+ 1+ 1=5
 *   6 =  1+ 1+ 1+ 1+ 1 + 1 + 1 + 1 = 8
 *   7 =  1+ 1+ 1+ 1+ 1 + 1 + 1 + 1+1+1+ 1+ 1+ 1 = 13
 *   F(n) = F(n-1)+F(n+1);(n>2,F(1)=1,F(2)=1);
 * @author ljq
 * @version 1.0
 * @since 1.0
 */
public class RabbitProblem {

	public static int getRabbitNumByMonth(int n) {
		return rabbitNum(n);
	}

	private static int rabbitNum(int n) {
		if (n <= 2) {
			return n == 0 ? 0 : 1;
		}
		return rabbitNum(n - 1) + rabbitNum(n - 2);
	}

	public static void main(String[] args) {
		System.out.println(getRabbitNumByMonth(66));
	}
}

  

 

 

package com.ljq.base;

/**
 * 打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。
 * 例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方
 *
 * @author ljq
 * @version 1.0
 * @since 1.0
 */
public class Narcissus {

	public static void printNarcissus() {
		for (int i = 100; i < 1000; i++) {
			int bit = i % 10;
			int ten = (i / 10) % 10;
			int hundred = i / 100;
			int sum = cube(hundred) + cube(ten) + cube(bit);
			if (i == sum) {
				System.out.print(i + " ");
			}
		}
	}

	private static int cube(int i) {
		return i * i * i;
	}

	public static void main(String[] args) {

		printNarcissus();
	}
}

   

 

/**
 * 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,
 * 又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
 * 以后每天早上都吃了前一天剩下的一半零一个。
 * 到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少
 * 
 *  r =n-(n/2+1)   n=2(r+1) --->  n=2(1+1)=4
 *  9  4 - (4/2+1)
 *  10 1
 * @author ljq
 * @version 1.0
 * @since 1.0
 */
public class MonkeyEatPeaches {
   
	public static int eat(int day, int peachs){
		int n=peachs;
		for(int i=0; i<day; i++){
			n = 2*(n+1);
		}
		return n;
	}
	
	public static void main(String[] args){
		System.out.println(eat(10, 1));
	}
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics