こんなんでどぉぢゃろ
C++
1#include <algorithm>
2#include <iostream>
3#include <random>
4
5using namespace std;
6
7struct R {
8 int score;
9 int rank;
10 int id;
11};
12
13int main() {
14 const int N = 100;
15 R data[N];
16
17 mt19937 gen;
18 uniform_int_distribution<> dist(0,30);
19 auto rnd = [&]() { return dist(gen); };
20
21 // テキトーな乱数で埋める
22 for (int i = 0; i < N; ++i) {
23 data[i].id = i;
24 data[i].score = rnd();
25 }
26
27 // scoreでソートする
28 auto score_less = [](const R& x, const R& y) { return x.score > y.score; };
29 sort(begin(data), end(data), score_less);
30
31 // 順位づけ
32 for (int i = 0; i < N; ++i) {
33 data[i].rank = static_cast<int>(distance(begin(data), lower_bound(begin(data), end(data), data[i], score_less)))+1;
34 }
35
36 // できたかな?
37 cout << "id\trank\tscore\n";
38 for ( auto item : data ) {
39 cout << item.id << '\t' << item.rank << '\t' << item.score << endl;
40 }
41
42}
実行結果はこんな↓
id rank score
66 1 30
0 2 29
2 2 29
32 2 29
7 2 29
44 6 28
42 6 28
72 6 28
21 6 28
89 6 28
5 11 27
45 12 26
69 12 26
50 12 26
41 12 26
98 16 25
74 16 25
…
人数分、順番[プレイヤーID]に、自分のスコアより大きいスコアを持つ人数+1をセット
なら上記コードの順位づけの部分を
for_each(begin(data), end(data),
[&](R& item) {
item.rank = static_cast<int>(count_if(begin(data), end(data),
[&](const R& t) { return t.score > item.score; }));
});
この場合実行結果は:
id rank score
0 1 29
1 44 15
2 1 29
3 52 13
4 96 0
5 10 27
6 92 1
7 1 29
8 92 1
9 25 21
10 52 13
11 22 22
12 79 5
13 75 6
...
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/25 13:08
2017/03/25 13:17 編集
2017/03/27 08:33
2017/03/27 08:37
2017/03/27 15:43
2017/03/27 16:37