洛谷P1428 小鱼比可爱 题解 枚举

题目链接:https://www.luogu.com.cn/problem/P1428

解题思路:
本题我们只需要枚举从 \(0\)\(n-1\) 枚举每一只小鱼 \(i\)(我这里数组坐标是从 \(0\) 开始的),然后再从 \(0\)\(i-1\) 去枚举每一只小鱼 \(j\) ,统计一下有多少只小鱼 \(j\) 没有小鱼 \(i\) 可爱就可以了。

示例代码:

#include <bits/stdc++.h>
using namespace std;
int n, a[100];
int main() {
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i];
    for (int i = 0; i < n; i ++) {  // 遍历每一只小鱼,对应编号为i
        int cnt = 0;    // cnt用于记录i前面没有小鱼i可爱的小鱼的数量
        for (int j = 0; j < i; j ++) {  // 遍历i前面的每一只小鱼j
            if (a[i] > a[j]) cnt ++;    // 找到一只没有小鱼i可爱的小鱼j,则cnt++
        }
        cout << cnt << " "; // 最终cnt记录的就是小鱼i前面没有小鱼i可爱的小鱼的数量
    }
    return 0;
}
posted @ 2020-04-16 21:59  quanjun  阅读(472)  评论(0编辑  收藏  举报