复习的时候总要练一练才行,看看几道练习题吧。
1.有一对兔子,从出生后第3个月起每个月生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少? (输入月数,输出兔子的对数)。
2.求1~1000所有素数之和。
3.打印所有"水仙花数",所谓“水仙花数”是指一个三位数,其各位数字的立方等于该数本身。例如:153是一个“水仙花数”,因为153 = 1^3 +5^3+3^3。
4.一球从100米高度自由落下,每次落地后反弹到原来高度的一半,再落下,求它在第20次落地时,共经过多少米?
5.输出1,2,3,4四个数字所产生的不重复的所有3位数。
6.输入某年某月某日,判断这一天是这一年的第几天。
1.
大致思路:把兔子分成三个类别,分别是小兔子,中兔子,大兔子;
月份 | 小兔子只数 | 中兔子只数 | 大兔子只数 |
第1个月 | 1 | 0 | 0 |
第2个月 | 0 | 1 | 0 |
第3个月 | 1 | 0 | 1 |
第4个月 | 1 | 1 | 1 |
第5个月 | 2 | 1 | 2 |
我们发现,从第三个月开始,每个月兔子的个数都是前两个月的兔子个数之和。
package test;
/*有一对兔子,从出生后第3个月起每个月生一对兔子,小兔子长到第三个月后每个月又生一对
兔子,假如兔子都不死,问每个月的兔子对数为多少? (输入月数,输出兔子的对数)*/
import java.util.*;
public class test1
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("请输入你想要查看的月数:");
int month = scan.nextInt();//输入月份
for(int i = 1; i <= month; i++)
{
System.out.println("第"+i+"个月的兔子有"+rabbit(i)+"只");
}
}
public static int rabbit(int a)//兔子类,返回兔子的只数
{
if(a == 1||a == 2)
return 1;
else
return(rabbit(x - 1) + rabbit(x - 2));
}
}
2.
思路:设置标签,循环1000次,判断哪些是素数,更改标签,之后输出更改过标签的数字即可
package test;
//求1~1000所有素数之和
public class test2
{
public static void main(String[] args)
{
int count = 0;//计数器
int sum = 0;//和
for(int i = 2; i <= 1000; i++)
{
boolean flag = false;//建立一个标签
for (int j = 2; j <= i; j++)
{
if(i % j == 0 && j != i)//判断是否为素数
{
flag = true;//更改标签
break;
}
}
if(!flag)//对剩余标签求和
{
System.out.println(i + " ");
sum +=i;
count++;
}
}
System.out.println();
System.out.println("1-1000之间的素数一共有"+count+"个,其和为"+sum);
}
}
3.直接求解即可
package test;
/*打印所有"水仙花数",所谓“水仙花数”是指一个三位数,其各位数字的立方等于该数本身。例如
153是一个“水仙花数”,因为153 = 1^3 +5^3+3^3*/
public class test3
{
public static void main(String[] args)
{
System.out.println("所有的水仙花数分别为:");
for(int i = 100; i <= 999; i++)
{
int onesplace=i % 10;//个位数
int tensplace=i / 10 % 10;//十位数
int hundredsplace=i / 100 % 10; //百位数
if(Math.pow(onesplace, 3) + Math.pow(tensplace, 3) + Math.pow(hundredsplace, 3) == i)
{
System.out.println(i);
}
}
}
}
4.直接求解即可
package test;
/*一球从100米高度自由落下,每次落地后反弹到原来高度的一半,
再落下,求它在第20次落地时,共经过多少米*/
public class test4
{
public static void main(String[] args)
{
double sum=100,height=100;//设立初始高度
for(int i=1;i<=19;i++)
{
height=height/2;//计算高度
sum=sum+height*2;//高度求和
}
System.out.println("第二十次小球共经过:"+sum+"米");
}
}
5.直接求解即可
package test;
//输出1,2,3,4四个数字所产生的不重复的所有3位数
public class test5
{
public static void main(String[] args)
{
int i,j,k;
for(i = 1; i <= 4; i++)
{
for(j = 1; j<=4; j++)
{
for(k = 1; k <= 4; k++)
{
if(i != j && j != k && k != i)
System.out.printf("%d%d%d\n",i,j,k);
}
}
}
}
}
6.
思路:先判断是不是闰年,之后分别相加天数,可以用switch-case语句,也可以用if判断,这里给出if判断解法
package test;
//输入某年某月某日,判断这一天是这一年的第几天
import java.util.*;
public class test6
{
public static void main (String[] args)
{
//输入年月日
System.out.println("请输入某年某月某日");
Scanner scan = new Scanner(System.in);
System.out.println("年:");
int year = scan.nextInt();
System.out.println("月:");
int month = scan.nextInt();
System.out.println("日:");
int day = scan.nextInt();
int nonleapyear[] = {31,28,31,30,31,30,31,31,30,31,30,31};//非闰年
int leapyear[] = {31,29,31,30,31,30,31,31,30,31,30,31};//闰年
int yue = 0;
int tian = 0;
//判断输入是否争取
if(month > 12 || day > 31)
{
System.out.println("输入有误,无法查询!");
}
//闰年
else if(year % 100 != 0 && year %4 == 0 || year %400 == 0)
{
if(month < 2)
{
System.out.println(year+"年"+month+"月"+day+"日是"+year+"年的第"+day+"天。");
}
else
{
for(int i = 2;i <= month;i++)
{
yue += leapyear[i-2];
}
tian = yue + day;//计算天数
System.out.println(year+"年"+month+"月"+day+"日是"+year+"年的第"+tian+"天。");
}
}
//非闰年
else
{
if(month < 2)
{
System.out.println(year+"年"+month+"月"+day+"日是"+year+"年的第"+tian+"天。");
}
else
{
for(int i = 2;i <= month;i++)
{
yue += nonleapyear[i-2];
}
tian = yue + day;//计算天数
System.out.println(year+"年"+month+"月"+day+"日是"+year+"年的第"+tian+"天。");
}
}
}
}