欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

洛谷P1116题解(Java语言描述)

程序员文章站 2022-07-13 11:20:08
...

题目要求

洛谷P1116题解(Java语言描述)

分析

这题,属实下饭嗷,hh……
题出的挺玄学,其实就一个冒泡排序,笑死个人~~
洛谷P1116题解(Java语言描述)

直接上AC代码就完事

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt(scanner.nextLine());
        int[] arrInt = new int[num];
        for (int i = 0; i < num; i++) {
            arrInt[i] = scanner.nextInt();
        }
        System.out.println(bubbleSort(num, arrInt));
        scanner.close();
    }

    public static int bubbleSort(int length, int[] record) {
        int count = 0;
        int exchange = length - 1;
        int position, bound, temp;
        while (exchange != 0) {
            bound = exchange;
            exchange = 0;
            for (position = 0; position < bound; position++) {
                if (record[position] > record[position+1]) {
                    temp = record[position];
                    record[position] = record[position+1];
                    record[position+1] = temp;
                    exchange = position;
                    count++;
                }
            }
        }
        return count;
    }

}

以前写的,发一下,蛤蛤蛤

相关标签: # 菜鸡逛洛谷