fork download
  1. #include <algorithm>
  2. #include <numeric>
  3. #include <limits>
  4. #include <iostream>
  5. #include <vector>
  6.  
  7. using array_type = std::vector<int>;
  8.  
  9. array_type input(std::string info, int n) {
  10. array_type result(n);
  11. std::cout << info;
  12. for ( ; n--; )
  13. std::cin >> result[n];
  14. return result;
  15. }
  16.  
  17. int score(const std::vector<array_type>& arrays, const array_type& weights) {
  18. return std::inner_product( std::begin( arrays ), std::end( arrays ) - 1, std::begin( arrays ) + 1, 0, std::plus<int>{},
  19. [&](auto& a, auto& b) {
  20. return std::inner_product( std::begin( a ), std::end( a ), std::begin( b ), 0, std::plus<int>{},
  21. [&](auto& x, auto& y) {
  22. return ( x == y ) * weights[&x - &a.front()];
  23. } );
  24. } );
  25. }
  26.  
  27. int main()
  28. {
  29. std::cout << "Arraylänge: ";
  30. int l = 0;
  31. std::cin >> l;
  32. array_type weights = input( "Gewichte: ", l );
  33. std::cout << "Anzahl Arrays: ";
  34. int n = 0;
  35. std::cin >> n;
  36. std::vector<array_type> arrays;
  37. for ( int i = 0; i != n; ++i ) {
  38. arrays.push_back( input( "Array " + std::to_string( i ) + ": ", l ) );
  39. }
  40. std::sort( begin( arrays ), end( arrays ) );
  41. int best_score = std::numeric_limits<int>::min();
  42. std::vector<array_type> best;
  43. do {
  44. if ( score( arrays, weights ) > best_score ) {
  45. best_score = score( arrays, weights );
  46. best = arrays;
  47. }
  48. } while ( next_permutation( std::begin( arrays ), std::end( arrays ) ) );
  49. std::cout << "\nBeste Punkte: " << best_score << '\n';
  50. for ( auto& a : arrays ) {
  51. std::cout << "( ";
  52. for ( auto& x : a ) {
  53. std::cout << x << ' ';
  54. }
  55. std::cout << ")\n";
  56. }
  57. std::cout << std::endl;
  58. }
Success #stdin #stdout 0s 3480KB
stdin
4
10 11 15 12
5
1 3 4 2
2 2 2 2
3 4 2 2
2 2 1 1
3 1 4 2
stdout
Arraylänge: Gewichte: Anzahl Arrays: Array 0: Array 1: Array 2: Array 3: Array 4: 
Beste Punkte: 97
( 1 1 2 2 )
( 2 2 2 2 )
( 2 2 4 3 )
( 2 4 1 3 )
( 2 4 3 1 )