아이템선택

2018. 12. 5. 01:18·Language/C C++
반응형
아이템 선택

아이템 선택

x
#include<iostream>
#include<vector>
#include<cstdlib>
#include<ctime>
#include<unordered_set>
​
using namespace std;
​
struct Table{
    struct Item {
        int id;
        int weight;
    };
​
    vector<Item> table;
};
​
int select(const Table& table, int w) {
    int randomNum = rand() % (w + 1);
​
    int sum = 0;
    for (auto it : table.table) {
        sum += it.weight;
        if (randomNum <= sum) {
            return it.id;
        }
    }
    return -1;//fail
}
​
vector<int> select2(const Table& table, int w, int k) {
    unordered_set<int> selected;
    while (selected.size() < k) {
        selected.insert(select(table, w));
    }
    
    return vector<int>(selected.begin(), selected.end());
}

일정 확률에 따라 아이템 인덱스를 반환

반응형
저작자표시 비영리 변경금지 (새창열림)
'Language/C C++' 카테고리의 다른 글
  • [C/C++] const keyword
  • [C++]optional
  • 중복 없는 난수 생성
  • 정렬
덴마크초코우유
덴마크초코우유
IT, 알고리즘, 프로그래밍 언어, 자료구조 등 정리
    반응형
  • 덴마크초코우유
    이것저것끄적
    덴마크초코우유
  • 전체
    오늘
    어제
    • 분류 전체보기 (124)
      • Spring Framework (10)
        • Spring (5)
        • JPA (3)
        • Spring Security (0)
      • Language (52) N
        • Java (12) N
        • Python (10)
        • JavaScript (5)
        • NUXT (2)
        • C C++ (15)
        • PHP (8)
      • DB (16)
        • MySQL (10)
        • Reids (3)
        • Memcached (2)
      • 개발 (3)
      • 프로젝트 (2)
      • Book (2)
      • PS (15)
        • 기타 (2)
        • 백준 (2)
        • 프로그래머스 (10)
      • 딥러닝 (8)
        • CUDA (0)
        • Pytorch (0)
        • 모델 (0)
        • 컴퓨터 비전 (4)
        • OpenCV (1)
      • 기타 (16)
        • 디자인패턴 (2)
        • UnrealEngine (8)
        • ubuntu (1)
        • node.js (1)
        • 블로그 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 미디어로그
    • 위치로그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    PS
    mscoco
    웹
    select
    Python
    프로그래머스
    CPP
    클래스
    알고리즘
    php
    게임
    JS
    언리얼엔진4
    게임 개발
    자바
    NUXT
    블루프린트
    MySQL
    딥러닝
    Unreal
    map
    pytorch
    FPS
    redis
    C
    JavaScript
    Unreal Engine
    파이썬
    memcached
    C++
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
덴마크초코우유
아이템선택
상단으로

티스토리툴바