题解 | 左侧严格小于计数

左侧严格小于计数

https://www.nowcoder.com/practice/c5922c6cdd1445749bd42f586c422435

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int a[] = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = in.nextInt();
        }
        int b[] = new int[n];
        for(int i = 0; i < n; i++){
            int count = 0; //这里的count不能放在for外面,否则会变成前向累加
            for(int j = 0; j < i; j++){
                if(a[j] < a[i]){
                    count++;
                }
            }
            b[i] = count;
        }
        for(int i = 0; i < n; i++){
            System.out.print(b[i] + " ");
        }
    }
    
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务