标签:int _id margin ber style 表示 font 程序 lis
分析:第一个月兔子对数为1,第二个月兔子对数为1,第三个月兔子对数为2,第四个月兔子对数为3,第五个月兔子对数为5,....
该"兔子数列"(也即是斐波那契数列),从第二项开始,每一项都等于前两项之和,1,1,2,3,5,8,13,21,34,.... 需要注意的是第一个1代表的是第1项,第0项为0。
下面的Java代码只是根据输入的月份,来输出当月的兔子对数。
import java.util.Scanner; public class Java01 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入要查询的月份:"); int month = sc.nextInt(); System.out.println("第" + month + "个月兔子共有" + fib(month) + "对。"); } public static int fib(int month) { if (month == 1 || month == 2) { return 1; } else { return fib(month - 1) + fib(month - 2); } } }
import java.util.ArrayList; public class Java02 { public static void main(String[] args) { int m, n; int temp = 0; ArrayList<Integer> alist = new ArrayList<Integer>(); A: for (n = 101; n <= 200; n++) { // 采用了标签的形式 for (m = 2; m < n / 2; m++) { if (n % m == 0) { continue A; } } alist.add(n); } System.out.println("101-200共有"+alist.size()+"个素数。"); System.out.println("分别如下:"); for(int one:alist){ temp++; System.out.print(one+" "); if (temp % 5 == 0) { //每行输出5个 System.out.println(); } } } }
public class Java03 { public static void main(String[] args) { int m, n; int temp = 0; System.out.println("100以内的素数有:"); A: for (n = 2; n <= 100; n++) { // 采用了标签的形式 for (m = 2; m < n / 2; m++) { if (n % m == 0) { continue A; } } System.out.print(n + " "); temp++; if (temp % 5 == 0) { // 每行输出5个 System.out.println(); } } } }
import java.util.ArrayList; public class Java04 { public static void main(String[] args) { ArrayList<Integer> alist = new ArrayList<Integer>(); int temp = 0; int count = 0; for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { for (int k = 1; k <= 4; k++) { if (i != j && j != k && k != i) { temp = i * 100 + j * 10 + k; alist.add(temp); } } } } System.out.println("能组成"+alist.size()+"个互不相同且无重复数字的三位数。"); System.out.println("分别是:"); for(int one:alist){ count++; System.out.print(one+" "); if (count % 5 == 0) { //每行输出5个 System.out.println(); } } } }
import java.util.Scanner; public class Java05 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入一个正整数:"); int n = sc.nextInt(); System.out.println(n+"分式因解的结果如下:"); System.out.print(n + "="); solution(n); } public static void solution(int n) { for (int i = 2; i <= n; i++) { if (n == i) { System.out.println(i); return; } if (n > i && (n % i == 0)) { System.out.print(i + "*"); solution(n / i); break; } } } }
import java.util.Scanner; public class Java06 { public static void main(String[] args) { System.out.println("请输入一行字符:"); Scanner sc = new Scanner(System.in); String inStr = sc.nextLine(); System.out.println("统计结果如下:"); count(inStr); } public static void count(String inStr) { int letter = 0; int number = 0; int blank = 0; int other = 0; for (int i = 0; i < inStr.length(); i++) { char in = inStr.charAt(i); if (in >= '0' && in <= '9') number++; else if ((in >= 'a' & in <= 'z') || (in >= 'A' & in <= 'Z')) letter++; else if (in == ' ') blank++; else other++; } System.out.println("字母的个数为:" + letter + "\n数字的个数为:" + number + "\n空格的个数为:" + blank + "\n其他符号的个数为:" + other); } }
public class Java07 { public static void main(String[] args) { int sum = 0; int res = 1; for (int day = 9; day >= 1; day--) { sum = (res + 1) * 2; res = sum; } System.out.println("第一天共摘了" + sum + "个桃子。"); } }
标签:int _id margin ber style 表示 font 程序 lis
原文地址:http://blog.csdn.net/csulfy/article/details/52964570