练习

Java练习代码
题目:有一对兔子,从出生后第 3 个月起每个月都生一对兔子, 小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

public  class   tuzi{   
public  static  void    main(String args[]){
int i=0;                
for(i=1;i <=20;i++)     
System.out.println(f(i));   
}                       
public  static  int f(int  x)   
{                       
if(x==1 ||  x==2)       
return  1;              
else                        
return  f(x-1)+f(x-2);  
}                       
}                       
public  class   tuzi{   
public  static  void    main(String args[]){
int i=0;                
math  mymath  =  new  math();   
for(i=1;i <=20;i++)     
System.out.println(mymath.f(i));    
}                       
}                       
class  math             
{                       
public  int f(int  x)   
{                       
if(x==1 ||  x==2)       
return  1;              
else                        
return  f(x-1)+f(x-2);  
}                       
}                       

题目:判断 101-200 之间有多少个素数,并输出所有素数。
分析:判断素数的方法:用一个数分别去除 2 到 sqrt(这个数),如果能被整除, 则表明此数不是素数,反之是素数。

public  class  shu{
public  static  void  main(String  args[]){
int  i=0;
shu  mymath  =  new  shu();
for(i=101;i <=200;i += 2)
if(mymath.iszhishu(i)==true)
System.out.println(i);

}
public  boolean  iszhishu(int  x)
{
for(int  i=2;i <=x/2;i++)
if  (x  %  i==0  )
return  false;
return  true;
}

}

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

public class hua{
public static void main(String args[]){
int i=0;
math mymath = new math();
for(i=100;i <=999;i++)
if(mymath.shuixianhua(i)==true)
System.out.println(i);

}           
}

class  math 
{           
public  int f(int   x)
{           
if(x==1 ||  x==2)
return  1;      
else            
return  f(x-1)+f(x-2);
}   
public  boolean iszhishu(int  x)
{
for(int i=2;i <=x/2;i++)
if  (x  %  2==0  )  
return  false;  
return  true;   
}
public  boolean shuixianhua(int  x)
{           
int i=0,j=0,k=0;
i=x /  100; 
j=(x    %   100)    /10;
k=x %   10; 
if(x==i*i*i+j*j*j+k*k*k)
return  true;   
else            
return  false;  
}
}

题目:输入某年某月某日,判断这一天是这一年的第几天?

import java.util.*;
public class test {
    public static void main (String[]args){
        int day=0;
        int month=0;
        int year=0;
        int sum=0;
        int leap;
        System.out.print("请输入年,月,日\n");
        Scanner input = new Scanner(System.in);
        year=input.nextInt();
        month=input.nextInt();
        day=input.nextInt();
        switch(month) /*先计算某月以前月份的总天数*/
        {
            case 1:
                sum=0;
                break;
            case 2:
                sum=31;
                break;
            case 3:
                sum=59;
                break;
            case 4:
                sum=90;
                break;
            case 5:
                sum=120;
                break;
            case 6:
                sum=151;
                break;
            case 7:
                sum=181;
                break;
            case 8:
                sum=212;
                break;
            case 9:
                sum=243;
                break;
            case 10:
                sum=273;
                break;
            case 11:
                sum=304;
                break;
            case 12:
                sum=334;
                break;
            default:
                System.out.println("data error");
                break;
        }
        sum=sum+day;
        /*再加上某天的天数*/
        if(year%400==0||(year%4==0&&year%100!=0))
        /*判断是不是闰年*/
            leap=1;
        else
            leap=0;
        if(leap==1 && month>2)
        /*如果是闰年且月份大于 2,总天数应该加一天*/
            sum++;
        System.out.println("It is the the day:"+sum);
    }
}

题目:两个乒乓球队进行比赛,各出三人。甲队为 a,b,c 三人,乙队为 x,y,z 三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a 说他不和 x 比,c 说他不和 x,z 比,请编程序找出三队赛手的名单。

import java.util.ArrayList;
public class pingpang {
    String a,b,c;
    public static void main(String[] args) {
        String[] op = { "x", "y", "z" };
        ArrayList<pingpang> arrayList=new ArrayList<pingpang>();
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                for (int k = 0; k < 3; k++) {
                    pingpang a=new pingpang(op[i],op[j],op[k]);
                    if(!a.a.equals(a.b)&&!a.b.equals(a.c)&&!a.a.equals("x")&&!a.c.equals("x")&&!a.c.equals("z")){
                        arrayList.add(a);
                    }
                }
        for(Object a:arrayList){
            System.out.println(a);
        }
    }
    public pingpang(String a, String b, String c) {
        super();
        this.a = a;
        this.b = b;
        this.c = c;
    }
    @Override
    public String toString() {
    // TODO Auto-generated method stub
    return "a 的对手是"+a+","+"b的对手是"+b+","+"c 的对手是"+c+"\n";
    }
}   

题目:一个 5 位数,判断它是不是回文数。即 12321 是回文数,个位与万位 相同,十位与千位相同。

import java.util.Scanner;
public class Shu {
    static int[] a = new int[5];
    static int[] b = new int[5];
    public static void main(String[] args) {
        boolean is =false;
        Scanner s = new Scanner(System.in);
        long l = s.nextLong();
        if (l > 99999 || l < 10000) {
            System.out.println("Input error, please input again!");
            l = s.nextLong();
        }
        for (int i = 4; i >= 0; i--) {
            a[i] = (int) (l / (long) Math.pow(10, i));
            l =(l % ( long) Math.pow(10, i));
        }
        System.out.println();
        for(int i=0,j=0; i<5; i++, j++) {
            b[j] = a[i];
        }
        for(int i=0,j=4; i<5; i++, j--) {
            if(a[i] != b[j]) {
                is = false;
                break;
            }
            else
            {
                is = true;
            }
        }
        if(is == false)
        {
            System.out.println("is not a Palindrom!");
        }
        else if(is == true)
        {
            System.out.println("is a Palindrom!");
        }
    }
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值