fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <functional>
  4.  
  5. template<class RandIt, class Compare>
  6. bool next_k_permutation(RandIt first, RandIt mid, RandIt last, Compare comp)
  7. {
  8. std::sort(mid, last, [=](auto a, auto b){ return comp(b,a); });
  9. return std::next_permutation(first, last, comp);
  10. }
  11.  
  12. template<class RandIt>
  13. bool next_k_permutation(RandIt first, RandIt mid, RandIt last)
  14. {
  15. return next_k_permutation(first, mid, last,std::less<>());
  16. }
  17.  
  18. int main()
  19. {
  20. // B M W Z A O T
  21. int v[] = {0,1,2,3,4,5,6,7,8,9};
  22. do {
  23. if (((v[0]+v[3]-v[5])*10+v[1]+v[4]-v[6])*10+v[2]+v[3]-v[5] == 1000*v[1])
  24. std::cout
  25. << v[0] << v[1] << v[2] << "+"
  26. << v[3] << v[4] << v[3] << "="
  27. << v[1] << v[5] << v[6] << v[5] << "\n";
  28. } while(next_k_permutation(v,v+7,v+10));
  29. }
  30.  
Success #stdin #stdout 0.02s 5516KB
stdin
Standard input is empty
stdout
415+787=1202
516+787=1303
617+585=1202