fork download
  1. // author: Leonardone @ NEETSDKASU
  2.  
  3. struct Comb {
  4. size: usize,
  5. x: Vec<usize>,
  6. init: bool,
  7. }
  8.  
  9. impl Comb {
  10. fn new(size: usize, sel: usize) -> Self {
  11. Self {
  12. size,
  13. x: vec![0; sel],
  14. init: false,
  15. }
  16. }
  17. fn next(&mut self) -> Option<&[usize]> {
  18. if !self.init {
  19. self.init = true;
  20. self.x.iter_mut().enumerate().for_each(|(i, e)| *e = i);
  21. return Some(&self.x);
  22. }
  23. let mut i = self.x.len() - 1;
  24. let mut k = self.size - 1;
  25. loop {
  26. self.x[i] += 1;
  27. if self.x[i] > k {
  28. if i == 0 {
  29. self.init = false;
  30. return None;
  31. }
  32. i -= 1;
  33. k -= 1;
  34. } else {
  35. while i + 1 < self.x.len() {
  36. self.x[i + 1] = self.x[i] + 1;
  37. i += 1;
  38. }
  39. return Some(&self.x);
  40. }
  41. }
  42. }
  43. }
  44.  
  45. fn main() {
  46. let mut c = Comb::new(5, 3);
  47. while let Some(x) = c.next() {
  48. println!("{:?}", x);
  49. }
  50. }
  51.  
Success #stdin #stdout 0s 4424KB
stdin
Standard input is empty
stdout
[0, 1, 2]
[0, 1, 3]
[0, 1, 4]
[0, 2, 3]
[0, 2, 4]
[0, 3, 4]
[1, 2, 3]
[1, 2, 4]
[1, 3, 4]
[2, 3, 4]