摘要: 题目:输出1~100以内前5个可以被3整除的数。思路:算一个能被3整除的数,输出直到5个后不再计算和输出。代码: 1 public class DivByThree { 2 public static void main(String[] args) { 3 int count = 0; 4 5 for (int i=1; i<100; i++) 6 { 7 if (i % 3 == 0) 8 { 9 System.out.println(i... 阅读全文
posted @ 2012-04-05 22:39 水芊芊 阅读(226) 评论(0) 推荐(0)
摘要: 题目:求1!+2!+3!+……+10!思路:先求每个数字的阶乘n!=num,再将num相加。代码: 1 public class Factorial { 2 public static void main(String[] args) { 3 int sum = 0; 4 int num = 1; 5 for (int i=1; i<=10; i++) 6 { 7 num = num * i; 8 sum = sum + num; 9 }10 ... 阅读全文
posted @ 2012-04-05 21:50 水芊芊 阅读(211) 评论(0) 推荐(0)
摘要: 题目:求Fibonacci数列:1,1,2,3,5,8,……第n个数的值(假设n=40)。思路:Fibonacci数列满足递推公式:F(1)=1,F(2)=1,F(n)=F(n-1) + F(n-2)解法1:递归 1 public class Fibonacci { 2 public static void main(String[] args) { 3 System.out.println(f(40)); 4 } 5 6 public static int f(int n) { 7 if (n == 1 || n ==... 阅读全文
posted @ 2012-04-05 20:52 水芊芊 阅读(226) 评论(0) 推荐(0)