java练习题

   //创建一个 int 类型的数组, 元素个数为 100, 并把每个元素依次设置为 1 - 100

    public  static void setNum(int[] arry){
        for(int i = 0; i < 100;i++){
            arry[i] = i+1;
        }
    }

    public static void main(String[] args) {
        int[] arry = new int[100];
        setNum(arry);
        System.out.println(Arrays.toString(arry));
    }


    //实现一个方法 printArray, 以数组为参数, 循环访问数组中的每个元素, 打印每个元素的值.

 /*   public static void printArray(int[] arry){
        for (int i = 0; i < arry.length; i++) {
            System.out.println(arry[i]);
        }
    }

    public static void main(String[] args) {
        int[] arry = {1,2,3,4,5};
        printArray(arry);
    }*/


    //实现一个方法 transform, 以数组为参数, 循环将数组中的每个元素乘以2 , 并设置到对应的数组元素上. 例如 原数组为 {1, 2, 3}, 修改之后为 {2, 4, 6}
 

  /*  public static void transform(int[] arry){
        for (int i = 0; i < arry.length; i++) {
            arry[i] *= 2;
        }
    }

    public static void main(String[] args) {
        int[] arry = {1,2,3};
        transform(arry);
        System.out.println(Arrays.toString(arry));
    }*/


    //实现一个方法 sum, 以数组为参数, 求数组所有元素之和.

//    public  static  int sum(int[] arry){
//        int sum = 0;
//        for (int i = 0; i < arry.length; i++) {
//            sum += i;
//        }
//        return sum;
//    }
//
//    public static void main(String[] args) {
//       int[] arry = {1,2,3,4,5};
//        System.out.println(sum(arry));
//    }


    //实现一个方法 avg, 以数组为参数, 求数组中所有元素的平均值(注意方法的返回值类型).

 /*   public static  double avg(int[] arry){
        double sum = 0.0;
        for (int i = 0; i < arry.length; i++) {
            sum += arry[i];
        }
        double avg  = sum / arry.length;
        return avg;
    }

    public static void main(String[] args) {
        int[] arry = {1,2,3,4,5};
        System.out.println(avg(arry));
    }*/

    //递归求 N 的阶乘

//    public  static  int getNum(int n){
//        if(n == 1){
//            return 1;
//        }
//        return getNum(n-1)*n;
//    }
//
//    public static void main(String[] args) {
//        Scanner scan = new Scanner(System.in);
//        while(scan.hasNext()){
//            int n = scan.nextInt();
//            System.out.println(getNum(n));
//        }
//    }

    //递归求 1 + 2 + 3 + ... + 10

/*    public  static  int sum(int n){
        if(n == 1){
            return 1;
        }
        return n+sum(n - 1);
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            int n = scan.nextInt();
            System.out.println(sum(n));
        }
    }*/

    //按顺序打印一个数字的每一位(例如 1234 打印出 1 2 3 4) (递归)

/*
    public  static  void print(int n){
        if(n > 9){
            print(n / 10);
        }
        System.out.print(n % 10+" ");
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            int n = scan.nextInt();
            print(n);

        }
    }
*/


    //写一个递归方法,输入一个非负整数,返回组成它的数字之和

   /* public static int getSum(int n){
        if(n < 10){
            return n;
        }
        return n % 10 + getSum(n / 10);
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            int n = scan.nextInt();
            System.out.println(getSum(n));
        }
    }
*/

    //递归求解fib

   /* public  static  int fib(int n){
        if(n == 1 || n == 2){
            return 1;
        }
        return fib(n-1)+fib(n - 2);
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            int n = scan.nextInt();
            System.out.println(fib(n));
        }
    }*/

   /* public static int fib(int n){
        int a = 1;
        int b = 1;
        int c = 1;
        for(int i = 3;i <= n;i++){
            c = a + b;
            a = b;
            b = c;
        }
        return c;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            int n = scan.nextInt();
            System.out.println(fib(n));
        }
    }*/


//  /*//汉诺塔问题

//    public  static void hanio(int n,char pos1,char pos2,char pos3){
//        if(n == 1){
//            move(pos1, pos3);
//        }else{
//            hanio(n -1,pos1,pos3,pos2);
//            move(pos1,pos3);
//            hanio(n -1,pos2,pos1,pos3);
//        }
//    }
//    public static  void move(char pos1,char pos2){
//        System.out.print(pos1+"->"+pos2+" ");
//    }
//    public static void main(String[] args) {
//        hanio(1,'A','B','C');
//        System.out.println("");
//        hanio(2,'A','B','C');
//        System.out.println("");
//        hanio(3,'A','B','C');
//    }
//
    public static void hanio(int n,char pos1,char pos2,char pos3){
        if(n == 1){
            move(pos1,pos3);
        }else{
            hanio(n-1,pos1,pos3,pos2);
            move(pos1,pos3);
            hanio(n-1,pos2,pos1,pos3);
        }

    }
    public static void move(char pos1,char pos2){
        System.out.print(pos1+"->"+pos2+" ");
    }

    public static void main(String[] args) {
        hanio(1,'A','B','C');
        System.out.println(" ");
        hanio(2,'A','B','C');
        System.out.println(" ");
        hanio(3,'A','B','C');
    }*/

    //青蛙跳台阶问题
    //一只青蛙一次可以跳上 1 级台阶,也可以跳上2 级。求该青蛙跳上一个n 级的台阶总共有多少种跳法

    //1:1种
    //2: 【1,1】 【2】2种
    //3: 【1,1,1】 【1 2】 【2,1】 3种
    //4:【1,1,1,,1】 【1 1 2】 【1,2,1】 【2,1,1】 【2,2】 5种

//    public static int count(int n){
//        if(n == 1){
//            return 1;
//        }
//        if(n == 2){
//            return 2;
//        }
//        return count(n-1)+count(n-2);
//    }
//
//    public static void main(String[] args) {
//        Scanner scan = new Scanner(System.in);
//        while(scan.hasNext()){
//            int n = scan.nextInt();
//            System.out.println(count(n));
//        }
//    }
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值