Java实例程序安利006~010

006 计算阶乘(1+1/2!+1/3!+…+1/5!的和)

package com.company;

import java.math.BigDecimal;

/**
 * @author 过儿
 * @date 2022/5/9 0009 18:52
 * @description 1+1/2!+1/3!+...+1/5!的和
 */
public class 计算阶乘006 {
    
    
    public static void main(String[] args) {
    
    

        //和
        BigDecimal sum = new BigDecimal(0.0);

        //阶乘项的计算结果
        BigDecimal factorial = new BigDecimal(1.0);
        //循环增量
        int i = 1;
        while (i <= 5) {
    
    
            //累加各项阶乘的和
            sum = sum.add(factorial);
            //i+1
            ++i;
            //计算阶乘项
            factorial = factorial.multiply(new BigDecimal(1.0 / i));
        }
        //输出计算结果
        System.out.println("1+1/2!+1/3!+...+1/5!的和 计算结果等于:\n" + sum);
    }
}

在这里插入图片描述

007 空心菱形

使用两个双层for 循环分别输出菱形的上半部分与下半部分

package com.company;

/**
 * @author 过儿
 * @date 2022/5/10 0010 13:50
 * @description
 */
public class 空心菱形 {
    
    
    public static void main(String[] args) {
    
    
        printHollowRhombus(8);
    }

    public static void printHollowRhombus(int size) {
    
    
        if (size % 2 == 0) {
    
    
            //计算菱形大小
            size++;
        }

        for (int i = 0; i < size / 2 + 1; i++) {
    
    
            for (int j = size / 2 + 1; j > i + 1; j--) {
    
    
                //输出左上角位置的空白
                System.out.print(" ");
            }

            for (int j = 0; j < 2 * i + 1; j++) {
    
    
                if (j == 0 || j == 2 * i) {
    
    
                    //输出菱形上半部边缘
                    System.out.print("*");
                } else {
    
    
                    //输出菱形上半部空心
                    System.out.print(" ");
                }
            }
            System.out.println(" ");
        }
        for (int i = size / 2 + 1; i < size; i++) {
    
    
            for (int j = 0; j < i - size / 2; j++) {
    
    
                //输出菱形左下角空白
                System.out.print(" ");
            }

            for (int j = 0; j < 2 * size - 1 - 2 * i; j++) {
    
    
                if (j == 0 || j == 2 * (size - i - 1)) {
    
    
                    //输出菱形下半部边缘
                    System.out.print("*");
                } else {
    
    
                    //输出菱形下半部空心
                    System.out.print(" ");
                }
            }
            System.out.println(" ");
        }
    }
}

在这里插入图片描述

008 获取一维数组最小值

package com.company;

/**
 * @author 过儿
 * @date 2022/5/10 0010 17:24
 * @description
 */
public class 获取一维数组最小值  {
    
    

    public static void main(String[] args) {
    
    
        int[] numbers = {
    
    2,5,123,34,66,12,1,24};
        int min = numbers[0];
        for (int tem:numbers) {
    
    
            if (tem < min) {
    
    
                min = tem;
            }
        }
        System.out.println(min);
    }
}

009 二维数组行列互换

利用双层for循环遍历数组时,把新数组与原数组的行列索引交换进行元素赋值

package com.company;

/**
 * @author 过儿
 * @date 2022/5/11 0011 14:11
 * @description
 */
public class 二维数组行列互换009 {
    
    

    public static void main(String[] args) {
    
    
        //创建二维数组
        int[][] arr = new int[][]{
    
    {
    
    1, 2, 3}, {
    
    4, 5, 6}, {
    
    7, 8, 9}};
        System.out.println("行列互调前:");
        //输出二维数组
        printArray(arr);

        int[][] arr2 = new int[arr.length][arr.length];
        //调整数组行列数据
        for (int i = 0; i < arr.length; i++) {
    
    
            for (int j = 0; j < arr[i].length; j++) {
    
    
                arr2[i][j] = arr[j][i];
            }
        }
        System.out.println("行列互调后:");
        //输出二维数组
        printArray(arr2);
    }
    private static void printArray(int[][]arr){
    
    
        //遍历数组
        for(int i=0;i<arr.length;i++){
    
    
            for(int j=0;j<arr.length;j++){
    
    
                //输出数组元素
                System.out.print(arr[i][j]+"");
            }
            System.out.println();
        }
    }
}

在这里插入图片描述

010 选择排序

选择排序的基本思想是,每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列最后,直到全部待排序的数据元素排完。下面是一个数组排序过程的例子。
初始数组资源【63 4 24 1 3 15】
第1趟排序后【15 4 24 1 3】63
第2趟排序后【15 4 3 1】24 63
第3趟排序后【1 4 3】15 24 63
第4趟排序后【1 3】 4 15 24 63
第5趟排序后【1】3 4 15 24 63

如果数组有重复值,应使用选择排序。
选择排序法从数组中挑选最大直并放在数组最后,而遇到重复的相等值不会做任何处理,所以如果程序允许数组有重复值的情况,建议使用适选择排序法,因为它的数据交换次数较少,速度也会略微提升,但这取决于数组中重复值的数量。

package com.company;

/**
 * @author 过儿
 * @date 2022/5/11 0011 16:21
 * @description
 */
public class 选择排序010 {
    
    

    public static void main(String[] args) {
    
    
        int[] numbers = {
    
    63,4,24,1,3,15};
        int index;
        for(int i=1;i<numbers.length;i++){
    
    
            index=0;
            for(int j=1;j<=numbers.length-i;j++){
    
    
                if(numbers[j] > numbers[index]){
    
    
                    //查找最大值
                    index=j;
                }
            }
            //交换在numbers.length-i和index(最大值)位置上的两个数
            int temp = numbers[numbers.length-i];
            numbers[numbers.length-i]=numbers[index];
            numbers[index]=temp;
        }
        for (int number : numbers) {
    
    
            System.out.println(number);
        }
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_40247570/article/details/124688260