zoukankan      html  css  js  c++  java
  • Java日志第52天 2020.8.28

    5.1 数组元素的引用

    public class Demo5_1 {
        public static void main(String[] args) {
            int[] a = new int[10];
            for (int i = 0; i <= 9; i++) {
                a[i] = i;
            }
            for (int i = 9; i >= 0; i--) {
                System.out.print(a[i]+" ");
            }
            System.out.println();
        }
    }

     

     

    5.2 用数组来处理求Fibonacci数列问题

    public class Demo5_2 {
        public static void main(String[] args) {
            int[] f = new int[20];
            f[0] = 1;
            f[1] = 1;

            for (int i = 2; i < 20; i++) {
                f[i] = f[i-2]+f[i-1];

            }

            for (int i = 0; i < 20; i++) {
                if (i%5 == 0) {
                    System.out.println();
                }
                System.out.print(f[i]+" ");
            }
        }
    }

     

     

    5.3 编写程序,用气泡法对10个数排序

    import java.util.Scanner;

    public class Demo5_3 {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int temp;
            int[] a = new int[11];
            System.out.print("Input 10 numbers:");
            for (int i = 1; i < 11; i++) {
                a[i] = sc.nextInt();
            }
            for (int j = 1; j <= 9; j++) {
                for (int i = 1; i <= 10-j; i++) {
                    if (a[i] > a[i+1]) {
                        temp = a[i];
                        a[i] = a[i+1];
                        a[i+1] = temp;
                    }
                }
            }

            System.out.println("The sorted numbers:");
            for (int i = 1; i < 11; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println();
        }
    }

     

     

    5.4 将一个二维数组行和列互换,存在另一个二维数组中。

    public class Demo5_4 {
        public static void main(String[] args) {
            int a[][] = new int[][] {{1, 2, 3}, {4, 5, 6}};
            int[][] b = new int[3][2];

            System.out.println("Array a:");

            for (int i = 0; i <= 1; i++) {
                for (int j = 0; j <= 2; j++) {
                    System.out.print(a[i][j]+" ");
                    b[j][i] = a[i][j];
                }
                System.out.println();
            }

            System.out.println("Array b:");
            for (int i = 0; i <= 2; i++) {
                for (int j = 0; j <= 1; j++) {
                    System.out.print(b[i][j]+" ");
                }
                System.out.println();
            }
        }
    }

     

     

     

    5.5 有一个3×4的矩阵,要求编程序求出其中值最大的那个元素的值,以及其所在的行号和列号。

    public class Demo5_5 {
        public static void main(String[] args) {
            int row =0, colum=0, max;
            int[][] a = new int[][] {{5,12,23,56}, {19,28,37,46}, {-12,-34,6,8}};
            max = a[0][0];

            for (int i = 0; i <= 2; i++) {
                for (int j = 0; j <= 3; j++) {
                    if (a[i][j]>max) {
                        max = a[i][j];
                        row = i+1;
                        colum = j+1;
                    }
                }
            }
            System.out.println("max = "+max+", row = "+row+", colum = "+colum);
        }
    }

     

  • 相关阅读:
    Hapoop 搭建 (五)搭建zookeeper集群环境
    Hapoop 搭建 (四)搭建后测试
    Hapoop 搭建 (二)设置虚拟机SSH无密钥登陆
    Hapoop 搭建 (一)设置虚拟机与主机同一个网段
    windows上vmware 虚拟机,局域网其他主机访问
    Hadoop HDFS命令行操作
    jenkins配置
    WebAPI 实现前后端分离的示例
    Merge join、Hash join、Nested loop join对比分析
    SQL Server nested loop join 效率试验
  • 原文地址:https://www.cnblogs.com/Gazikel/p/13579779.html
Copyright © 2011-2022 走看看