Created
July 27, 2021 09:39
-
-
Save AcrylicShrimp/8b276813f28bb19ad1f171434ef40e79 to your computer and use it in GitHub Desktop.
1347명이 있는 카톡 방에서 전체 인원의 95% 이상이 선물을 한번이라도 받을 때까지 무작위 세 명에게 선물하고 그 세 명중 한 사람이 다시 같은 행위를 반복하면 몇 회만에 종료될까
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use rand::seq::SliceRandom; | |
use rand::{thread_rng, Rng}; | |
fn main() { | |
let mut rng = thread_rng(); | |
for _ in 0..1000 { | |
let total = 1347; | |
let mut count_who_never_received = 1347; | |
let mut users = (0..total).collect::<Vec<_>>(); | |
let mut user_counts = vec![0usize; total]; | |
users.shuffle(&mut rng); | |
let mut run = |count_who_never_received: &mut usize| { | |
let santa = users.pop().unwrap(); | |
users.shuffle(&mut rng); | |
if user_counts[users[0]] == 0 { | |
*count_who_never_received -= 1; | |
} | |
if user_counts[users[1]] == 0 { | |
*count_who_never_received -= 1; | |
} | |
if user_counts[users[2]] == 0 { | |
*count_who_never_received -= 1; | |
} | |
user_counts[users[0]] += 1; | |
user_counts[users[1]] += 1; | |
user_counts[users[2]] += 1; | |
users.push(santa); | |
users.swap(rng.gen::<usize>() % 3, 1346); | |
}; | |
let mut count: usize = 0; | |
while 67 <= count_who_never_received { | |
run(&mut count_who_never_received); | |
count += 1; | |
} | |
println!("count: {}", count); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment