System.out.println(“第”+n+“个月兔子总数为”+fun(n));
}
private static int fun(int n){
if(n1 || n2)
return 1;
else
return fun(n-1)+fun(n-2);
}
}
【程序2】
=====
题目:判断101-200之间有多少个素数,并输出所有素数。
程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
public class Prog2{
public static void main(String[] args){
int m = 1;
int n = 1000;
int count = 0;
//统计素数个数
for(int i=m;i<n;i++){
if(isPrime(i)){
count++;
System.out.print(i+" ");
if(count%10==0){
System.out.println();
}
}
}
System.out.println();
System.out.println(“在”+m+“和”+n+“之间共有”+count+“个素数”);
}
//判断素数
private static boolean isPrime(int n){
boolean flag = true;
if(n==1)
flag = false;
else{
for(int i=2;i<=Math.sqrt(n);i++){
if((n%i)0 || n1){
flag = false;
break;
}
else
flag = true;
}
}
return flag;
}
}
【程序3】
=====
题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
public class Prog3{
public static void main(String[] args){
for(int i=100;i<1000;i++){
if(isLotus(i))
System.out.print(i+" ");
}
System.out.println();
}
//判断水仙花数
private static boolean isLotus(int lotus){
int m = 0;
int n = lotus;
int sum = 0;
m = n/100;
n -= m*100;
sum = mmm;
m = n/10;
n -= m*10;
sum += mmm + nnn;
if(sum==lotus)
return true;
else
return false;
}
}
【程序4】
=====
题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数n,重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
public class Prog4{
public static void main(String[] args){
int n = 13;
decompose(n);
}
private static void decompose(int n){
System.out.print(n+“=”);
for(int i=2;i<n+1;i++){
while(n%i==0 && n!=i){
n/=i;
System.out.print(i+“*”);
}
if(n==i){
System.out.println(i);
break;
}
}
}
}
【程序5】
=====
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
程序分析:(a>b)?a:b这是条件运算符的基本例子。
public class Prog5{
public static void main(String[] args){
int n = -1;
try{
n = Integer.parseInt(args[0]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println(“请输入成绩”);
return;
}
grade(n);
}
//成绩等级计算
private static void grade(int n){
if(n>100 || n<0)
System.out.println(“输入无效”);
else{
String str = (n>=90)?“分,属于A等”😦(n>60)?“分,属于B等”:“分,属于C等”);
System.out.println(n+str);
}
}
}
【程序6】
=====
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
程序分析:利用辗除法。
public class Prog6{
public static void main(String[] args){
int m,n;
try{
m = Integer.parseInt(args[0]);
n = Integer.parseInt(args[1]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println(“输入有误”);
return;
}
max_min(m,n);
}
//求最大公约数和最小公倍数
private static void max_min(int m, int n){
int temp = 1;
int yshu = 1;
int bshu = m*n;
if(n<m){
temp = n;
n = m;
m = temp;
}
while(m!=0){
temp = n%m;
n = m;
m = temp;
}
yshu = n;
bshu /= n;
System.out.println(m+“和”+n+“的最大公约数为”+yshu);
System.out.println(m+“和”+n+“的最小公倍数为”+bshu);
}
}
【程序7】
=====
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
程序分析:利用while语句,条件为输入的字符不为’\n’.
import java.util.Scanner;
public class Prog7_1{
public static void main(String[] args){
System.out.print(“请输入一串字符:”);
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();//将一行字符转化为字符串
scan.close();
count(str);
}
//统计输入的字符数
private static void count(String str){
String E1 = “[\u4e00-\u9fa5]”;//汉字
String E2 = “[a-zA-Z]”;
String E3 = “[0-9]”;
String E4 = “\s”;//空格
int countChinese = 0;
int countLetter = 0;
int countNumber = 0;
int countSpace = 0;
int countOther = 0;
char[] array_Char = str.toCharArray();//将字符串转化为字符数组
String[] array_String = new String[array_Char.length];//汉字只能作为字符串处理
for(int i=0;i<array_Char.length;i++)
array_String[i] = String.valueOf(array_Char[i]);
//遍历字符串数组中的元素
for(String s:array_String){
if(s.matches(E1))
countChinese++;
else if(s.matches(E2))
countLetter++;
else if(s.matches(E3))
countNumber++;
else if(s.matches(E4))
countSpace++;
else
countOther++;
}
System.out.println(“输入的汉字个数:”+countChinese);
System.out.println(“输入的字母个数:”+countLetter);
System.out.println(“输入的数字个数:”+countNumber);
System.out.println(“输入的空格个数:”+countSpace);
System.out.println(“输入的其它字符个数:”+countSpace);
}
}
import java.util.*;
public class Prog7_2{
public static void main(St