java初学做的小题

/*
  1. 一对兔子生孩子
  2. 1234 组无重复三位数
  3.水仙花数
* 4.完数
* 5.一球从100米高度自由落下,每次落地后反跳回原高度的一半;
* 6.输入某年某月某日,判断这一天是这一年的第几天?.
* 7. 输出国际象棋棋盘的样式
* 8.猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,
* 9."鸡兔同笼"问题
* 10.蜘蛛,蜻蜓 蝉 三种动物,共十八只,共有腿118条,
	翅膀20对,请问有多少只蜻蜓?要求输出结果。
* */

import java.util.Scanner;

public class Examination {
    public static void main(String[] args) {
//        one();
//        two();
//        three();
//        four();
//        five();
//        six();
//        seven();
//        eight();
//        nine();
//        ten();
    }
//    有一对兔子,从出生后第3个月起每个月都生一对兔子,
//    这一对小兔子从出生后第3个月起每个月又生一对兔子,
//    假如兔子都不死,请问每个月的兔子总数为多少对?
//    要求:键盘输入截止月份,并输出各月的兔子总数。

    public static void one() {
        int xt_0 = 1;  //小兔子 0
        int xt_1 = 0;  //小兔子 1月
        int dt = 0;  //小兔子 2月 ==大兔
        int temple_1 = 0;
        int temple_dt = 0;
        int zt = 1; //一对
        int m = 0;   //要经过的月份
        Scanner s = new Scanner(System.in);
        System.out.println("输入经过的月份");
        m = s.nextInt();
        for (int i = 0; i <= m; i++) {
            System.out.println(i + "月共有" + zt + "对兔子");
            if (xt_0 > 0) {
                temple_1 += xt_0; //临时储存1阶段
                xt_0 = 0; //长大了 应该都长了,不是一个长大了
            }
            if (xt_1 > 0) {//上个阶段得有才加
                temple_dt += xt_1; //临时储存2阶段
                xt_1 = 0;
            }
            //每个阶段都长应该长的岁数后,判断完了以后再赋值  赋值应该是加上阶段,不是覆盖掉
            xt_1 = xt_1 + temple_1;
            dt = dt + temple_dt;
            temple_1 = 0;
            temple_dt = 0;
            xt_0 = xt_0 + dt;  //生孩子
            zt = xt_0 + xt_1 + dt;
        }
    }

    public static void two() {
        int one = 1;
        int two = 2;
        int three = 3;
        int four = 4;

        int n[] = {1, 2, 3, 4}; //6x4
        for (int j = 0; j <= 3; j++) {
            for (int k = 0; k <= 3; k++) {
                if (j == k) {  //自身不重复
                    continue;
                }
                for (int l = 0; l <= 3; l++) {
                    if (k == l || j == l) {  //自身不重复
                        continue;
                    }
                    System.out.print(n[j]);
                    System.out.print(n[k]);
                    System.out.print(n[l]);
                    System.out.println();
                }
            }
        }
    }

    /*所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。
	例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
			要求:打印出所有的“水仙花数”。*/
    public static void three() {
        int geWei = 0;
        int shiWei = 0;
        int baiWei = 0;
        for (int i = 100; i <= 999; i++) {
            geWei = (i % 100) % 10;
            shiWei = (i / 10) % 10;
            baiWei = i / 100;
            int g = geWei * geWei * geWei;
            int s = shiWei * shiWei * shiWei;
            int b = baiWei * baiWei * baiWei;
            int gsb = g + s + b;
            if (gsb == i) {
                System.out.println(i + "是水仙花数");
            }
        }
    }


    /*一个数如果恰好等于它的因子之和,这个数就称为“完数”。
    例如6=1+2+3。编程找出1000以内的所有完数。*/
    public static void four() {
        for (int i = 1; i <= 1000; i++) {
            int yinZi = 0;
            for (int j = 1; j <= i; j++) {
                if (i % j == 0 && i != j) {//如果比 i 这个数小的数除 i没有余数,则为因子
                    yinZi = yinZi + j;//求因子的和
                }
            }
            if (yinZi == i) {  //判断因子的和等于他本身
                System.out.println(i + "是完数");
            }
        }
    }

	/*一球从100米高度自由落下,每次落地后反跳回原高度的一半;
	再落下,求它在第n次落地时,共经过多少米?第n次反弹多高?
	要求:键盘输入落地次数n,并输出相应结果。*/

    public static void five() {
        int n = 0;
        double h = 100.0;   //height
        Scanner s = new Scanner(System.in);
        System.out.println("输入()次落地");
        n = s.nextInt();
        for (int i = 1; i <= n; i++) {
            h = h / 2.0;
        }
        System.out.println("第n次反弹:" + h + "m");

    }

    /*输入某年某月某日,判断这一天是这一年的第几天?
    要求:键盘输入某年某月某日,并输出相应结果。*/
    public static void six() {
        Scanner s = new Scanner(System.in);
        System.out.println("输入年份");
        int year = s.nextInt();
        System.out.println("输入月份");
        int month = s.nextInt();
        System.out.println("输入日");
        int day = s.nextInt();

        int all_day = 0;
        int d = 0;
        for (int i = 1; i <= month; i++) {  //月
            if (i <= 7) { //判断天数  分割月数
                if (i % 2 != 0) {
                    d = 31;
                } else if (i == 2) {
                    if (year % 4 == 0 && year % 100 != 0) {//闰年
                        d = 29;
                    } else {
                        d = 28;
                    }
                } else if (i % 2 == 0) {
                    d = 30;
                }
                for (int j = 1; j <= d; j++) {
                    if (i == month) { //另外加输入的天数
                        break;
                    }
                    all_day++;
                }
            } else { //7月以后的
                if (i % 2 != 0) {
                    d = 30;
                } else if (i % 2 == 0) {
                    d = 31;
                }
                for (int j = 1; j <= d; j++) {
                    if (i == month) { //另外加输入的天数
                        break;
                    }
                    all_day++;
                }
            }
        }
        System.out.println("今天是第" + (all_day + day) + "天");
    }

    //要求输出国际象棋棋盘的样式。(根据所学知识,打印出大体样式即可)
    public static void seven() {
        for (int i = 1; i <= 8; i++) {
            for (int j = 1; j <= 8; j++) {
                if (j % 2 != 0) {
                    if (i % 2 != 0) {
                        System.out.print("白");
                    } else {
                        System.out.print("黑");
                    }
                } else {
                    if (i % 2 != 0) {
                        System.out.print("黑");
                    } else {
                        System.out.print("白");
                    }
                }

            }
            System.out.println();
        }
    }

    /*猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,
    又多吃了一个;第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
    以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,
    见只剩下一个桃子了。求第一天共摘了多少。*/
    public static void eight() {
        int n = 0;
        int peach = 1;
        for (int i = 10; i >= 1; i--) {
            System.out.println("第" + i + "天有:" + peach + "桃");
            peach = (peach + 1) * 2;
        }
    }

    //	"鸡兔同笼"问题:有若干只鸡兔同在一个笼子里,从上面数,有35个头;
//	从下面数,有94只脚。问笼中各有几只鸡和兔?要求输出结果。
//	设
    /* j + t = 35
     *
     * 2j + 4t = 94
     *  */
    public static void nine() {
        int j = 0;
        int t = 0;
        for (int i = 0; i <= 35; i++) {
            j = i;
            t = 35 - j;
            if (2 * j + 4 * t == 94) {
                System.out.println("鸡:" + j);
                System.out.println("兔:" + t);
            }
        }
    }

    //	蜘蛛,蜻蜓 蝉 三种动物,共十八只,共有腿118条,
//	翅膀20对,请问有多少只蜻蜓?要求输出结果。
    /* qt : 6t  2f       t:腿    f: fly 翅膀
     * zz : 8t  0f
     * c : 6t 1f
     * qt + zz + c = 18
     *   */
    public static void ten() {
        int zz = 0;
        int qt = 0;
        int c = 0;

        for (int i = 1; i <= 18; i++) {
            for (int j = 1; j <= i; j++) {
                qt = i;
                c = j;
                zz = 18 - qt - c;
                int t = qt * 6 + zz * 8 + c * 6;
                int f = qt * 2 + c;
                if (t == 118 && f == 20) {
                    System.out.println("蜻蜓:" + qt + "只");
                }
            }
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值