Day31---学习Java第三弹

2021-08-10

Java经典例题(九)

29、对10个数进行排序

选择法:

package test2;

public class tset28 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] m = { 1, 5, 4, 6, 7, 4, 5, 8, 56, 45 };
        for (int i = 0; i < m.length; i++) {
            int k = i;
            for (int j = i + 1; j < m.length; j++) {
                if (m[j] < m[k]) {
                    k = j;
                    int temp = m[j];
                    m[j] = m[i];
                    m[i] = temp;
                }
            }
        }
        for (int i = 0; i < m.length; i++) {
            System.out.print(m[i] + " ");
        }
    }

}

 

冒泡法:

package test2;

public class test28too {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] m = { 1, 5, 4, 6, 7, 4, 5, 8, 56, 45 };
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m.length - 1 - i; j++) {
                if (m[j] > m[j + 1]) {
                    int temp = m[j];
                    m[j] = m[j + 1];
                    m[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < m.length; i++) {
            System.out.print(m[i] + " ");
        }
    }

}

 

 

30、求一个3*3矩阵对角线元素之和

package test2;

import java.util.Scanner;

public class test29 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        int[][] m = new int[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                m[i][j] = sc.nextInt();
            }
        }
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (i == j) {
                    System.out.println(m[i][j]);
                }
            }
        }
    }

}

分析:用双层for循环,如果i==j就输出

需要注意的点:无

 

31、有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

package test2;

import java.util.Scanner;

public class test30 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int[] m = { 1, 2, 6, 9, 12, 34, 56, 78, 90 };
        int[] n = new int[m.length + 1];
        int mark = 0;
        for (int i = 0; i < m.length; i++) {
            if (x > m[i])
                mark = i + 1;
        }
        n[mark] = x;
        for (int i = 0; i < mark; i++) {
            n[i] = m[i];
        }
        for (int i = mark + 1; i < n.length; i++) {
            n[i] = m[i - 1];
        }
        for (int i = 0; i < n.length; i++) {
            System.out.print(n[i] + " ");
        }
    }
}

分析:关键是判断出输入的数应该在那个位置插入

需要注意的点:注意下标

 

32、取一个整数a从右端开始的4~7位。

package test2;

import java.util.Scanner;

public class test32 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        char[] ch = s.toCharArray();
        if (ch.length < 7) {
            System.out.println("输入的数字不符合标准");
            System.exit(-1);
        }

        for (int i = 0; i < ch.length; i++) {
            if (i >= ch.length - 7 && i <= ch.length - 4) {
                System.out.print(ch[i]);
            }
        }
    }

}

分析:用字符数组解决,简单直接

需要注意的点:最好判断一下输入的数字是否符合要求

 

 

33、打印出杨辉三角形(要求打印出10行如下图)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

package test2;

public class test33 {
    public static void main(String[] args) {
        int[][] a = new int[10][10];
        for (int i = 0; i < 10; i++) {
            a[i][i] = 1;
            a[i][0] = 1;
        }
        for (int i = 2; i < 10; i++) {
            for (int j = 1; j < i; j++) {
                a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
            }
        }
        for (int i = 0; i < 10; i++) {
            for (int k = 0; k < 2 * (a.length - i) - 1; k++) {
                System.out.print(" ");
            }
            for (int j = 0; j <= i; j++) {
                System.out.print(a[i][j] + "   ");
            }
            System.out.println();
        }
    }
}

分析:除了第一行和每行第一个元素之外,其余的每行的第x个元素都等于上一行的的第x-1个元素和第x个元素之和

需要注意的点:需要注意最后两个for循环是如何输出的,输出了几个

--------------------------------------------------------------------------------------------------------------

明天继续输入输出

posted @ 2021-08-10 21:01  zrswheart  阅读(30)  评论(0编辑  收藏  举报