逆序数是大学高等代数的一个比较简单的概念,整体思路很简单,通过数组去接受一个n维的数据,遍历去获取后方比自己小的数,累加即可。
下方为完整代码
import java.util.Scanner;
public class Reverse_order_number {
public static void main(String[] args) {
int[] a = Way.input();
//Way.print(a);
System.out.println(" ");
int answer = Way.get1(a);
System.out.println("逆序数为" + answer);
}
}
class Way{
public static int[] input(){
Scanner scanner = new Scanner(System.in);
System.out.println("输入阶数");
int n = scanner.nextInt();
int[] var = new int[n];
for (int i = 0; i < var.length; i++) {
System.out.println("输入第"+(i+1)+"位数");
var[i] = scanner.nextInt();
}
return var;
}
public static int get1(int[] a){
int num = 0;
for (int i = 0; i < a.length; i++) {
//System.out.print(check1(a,a[i])+"+");
num+=check1(a,a[i]);
}
return num;
}
public static int check1(int[] a, int b){
int num = 0;
for (int i = check(a,b); i < a.length; i++) {
if(a[i]<b){
num++;
}
}
System.out.println(b+"后面之后有"+num+"个比"+b+"小的");
return num;
}
public static int check(int[] a ,int b){
for (int i = 0; i < a.length; i++) {
if (a[i] == b){
return i;
}
}
return -1;
}
}