1.3000米长的绳子,每天减一半,问,需要多少天,绳子会小于5米?
int rope = 3000;
int day = 0 ;
while(rope >=5) {
rope = rope / 2;
day++;
System.out.printf("第%d天结束,绳子还剩%d米\n", day,rope);
}
System.out.println("绳子小于5米需要"+ day + "天");
2.显示从101到2100期间所有的闰年,每行显示10个,之间用制表符分割
int year = 101;
for(int flag = 0 ; year<=2100 ; year++) {
if((year % 4 ==0 && year % 100 !=0) ||( year % 400 == 0)) {
if(flag>=10) {
flag=0;
System.out.println();
}
flag++;
System.out.printf( "%d | ",year);
}
}
3.猴子吃桃问题。猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第十天早上在想吃时,就只剩一个桃子了。求第一天共摘了多少个桃子?
int peach = 1;
for(int i = 0 ; i<10 ; i++) {
peach++;
peach = peach * 2;
}
System.out.printf("第一天摘了%d个",(peach/2+1));
4.养老无忧计划:以目前的生活水平想要无忧养老需要存够600万养老金,假设你每年可以存N万,年利率为3%,问需要做牛做马多少年?
public final static int N = 8;
public static void main(String[] args) {
int year = 0;
double total_money = 0;
while(total_money <600) {
total_money = total_money+ (total_money * 0.03) + N ;
year++;
System.out.printf("第%d年,现有存款%.2f万\n",year,total_money);
}
System.out.printf("存到600万需要%d年",year);
}
5.鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?
int cook_price = 5;
int hen_price = 3;
int chick_price = 1;
int money = 100;
for(int cook = 0 ; cook<=(money/cook_price) ; cook++) {
for(int hen = 0 ; hen<=((money-(cook*cook_price)) / hen_price ) ; hen++){
for(int chick = 0 ; chick<=((money-(cook*cook_price) - (hen*hen_price)) / chick_price ) ; chick++) {
if((cook+hen+(chick*3))==100 && ((cook*cook_price)+(hen*hen_price)+(chick*chick_price))==100) {
System.out.printf("公鸡%d只,母鸡%d只,小鸡%d只\n",cook,hen,(chick*3));
}
}
}
6.鸡兔同笼:今有雉(鸡)兔同笼,上有三十五头,下有九十四足。问雉兔各几何
int head = 35;
int foot = 94;
for(int rabbit,chicken = 0; chicken<=head ; chicken++) {
rabbit = head - chicken;
if((chicken*2)+(rabbit*4) == 94){
System.out.printf("兔子%d只,鸡%d只\n",rabbit,chicken);
}
}
7.打印水仙花数
int hundred , ten , unit;
for(int i = 100 ; i <=999 ; i++) {
hundred = i/100;
ten = (i - (hundred*100))/10;
unit = i-(hundred*100) - (ten*10);
if(((hundred*hundred*hundred)+(ten*ten*ten)+(unit*unit*unit))==i) {
System.out.println(i+ "是水仙花数");
}
}
8.打印2到10000所有的素数(只能被1和它自身整除的数),每行显示8个
loop: for(int number =1,flag=0; number<=10000 ; number++) {
for(int i =2 ; i<=(number)/2 ; i++) {
if((number%i)==0) {
continue loop;
}
}
if(flag>=8) {
System.out.println();
flag=0;
}
flag++;
System.out.printf("%d ",number);
}
9.有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
int age = 10;
for(int i = 0 ; i<4 ; i++) {
age = age + 2;
}
System.out.printf("第五个人%d岁",age);
10.有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
int[] number = {1,2,3,4};
for(int i =1 ; i<=4 ; i++) {
for(int j = 1 ; j<=4 ; j++) {
if(i==j) {
continue;
}
for(int k = 1 ; k<=4 ; k++) {
if(k==i||k==j) {
continue;
}
System.out.println(((i*100)+(j*10)+k));
}
}
}
11.一个数恰好等于它的因子之和,这个数就被称为“完数”。例如,6的因子为1、2、3,而6=1+2+3,因此6是“完数”。编程找出1~n之内的所有完数。
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数:");
int n = input.nextInt();
int number;
for(int i = 1 ; i<=n ; i++) {
number = 0;
for(int j =1 ; j<i ; j++) {
if(i%j==0) {
number = number + j;
}
}
if(number == i) {
System.out.println( i + "是完数");
}
12.有一对兔子,从出生第三个月起每个月都生一对兔子,小兔子长到第三个月后,每个月又生一对兔子,假如兔子都不死,每个月的兔子总数。(斐波那契)
public static int function(int n) {
if(n == 0){
return 0;
}else if (n == 1){
return 1;
}else {
return function(n - 1) + function(n - 2);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入月数");
int mounth = input.nextInt();
function(mounth);
System.out.printf("%d个月后有%d只兔子",mounth,function(mounth));
}
13.一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?
double hight =100;
double total_metre =0;
for(int i =0 ; i<10 ; i++) {
total_metre += hight;
hight /= 2;
}
System.out.printf("第十次反弹的高度为%.5f米,第10次落地时经过了%.5fd米",hight,total_metre+100);
14.啤酒瓶。买了100瓶啤酒,每三个空瓶可以兑换一瓶啤酒,问最多可以喝多少瓶啤酒
int beer =100;
int empty = 0;
int total_beer = 0;
while(beer>0) {
total_beer +=beer;
empty +=beer;
beer = empty /3;
empty =empty%3;
}
System.out.printf("最终可以喝到%d瓶啤酒",total_beer);