# include # include # include # include using namespace std; # include "subset_sum_swap.hpp" int main ( ); void subset_sum_swap_tests ( ); void subset_sum_swap_try ( int n, int w[], int t ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // subset_sum_swap_test() tests subset_sum_swap(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 15 June 2024 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "subset_sum_swap_test():\n"; cout << " C++ version\n"; cout << " Test subset_sum_swap().\n"; subset_sum_swap_tests ( ); // // Terminate. // cout << "\n"; cout << "subset_sum_swap_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return EXIT_SUCCESS; } //****************************************************************************80 void subset_sum_swap_tests ( ) //****************************************************************************80 // // Purpose: // // subset_sum_swap_tests() tests subset_sum_swap(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 15 June 2024 // // Author: // // John Burkardt // { int n; int t; static int w01[8] = { 15, 22, 14, 26, 32, 9, 16, 8 }; static int w02[8] = { 15, 22, 14, 26, 32, 9, 16, 8 }; static int w03[8] = { 15, 22, 14, 26, 32, 9, 16, 8 }; static int w04[10] = { 267, 493, 869, 961, 1000, 1153, 1246, 1598, 1766, 1922 }; static int w05[21] = {518533, 1037066, 2074132, 1648264, 796528, 1593056, 686112, 1372224, 244448, 488896, 977792, 1955584, 1411168, 322336, 644672, 1289344, 78688, 157376, 314752, 629504, 1259008}; static int w06[10] = { 41, 34, 21, 20, 8, 7, 7, 4, 3, 3 }; static int w07[9] = { 81, 80, 43, 40, 30, 26, 12, 11, 9 }; static int w08[6] = { 1, 2, 4, 8, 16, 32 }; static int w09[10] = { 25, 27, 3, 12, 6, 15, 9, 30, 21, 19 }; n = 8; t = 53; subset_sum_swap_try ( n, w01, t ); n = 8; t = 53; subset_sum_swap_try ( n, w02, t ); n = 8; t = 53; subset_sum_swap_try ( n, w03, t ); n = 10; t = 5842; subset_sum_swap_try ( n, w04, t ); n = 21; t = 2463098; subset_sum_swap_try ( n, w05, t ); n = 10; t = 50; subset_sum_swap_try ( n, w06, t ); n = 9; t = 100; subset_sum_swap_try ( n, w07, t ); n = 6; t = 22; subset_sum_swap_try ( n, w08, t ); n = 10; t = 50; subset_sum_swap_try ( n, w09, t ); return; } //****************************************************************************80 void subset_sum_swap_try ( int n, int w[], int t ) //****************************************************************************80 // // Purpose: // // subset_sum_swap_try() tries the swap code for a given subset_sum problem. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 15 June 2024 // // Author: // // John Burkardt // { int i; int *index; int sum_achieved; cout << "\n"; cout << " Target value: " << t << "\n"; cout << "\n"; cout << " Available weights:\n"; cout << "\n"; for ( i = 0; i < n; i++ ) { cout << " " << w[i] << "\n"; } index = new int[n]; sum_achieved = subset_sum_swap ( n, w, t, index ); cout << "\n"; cout << " Selected weights:\n"; cout << "\n"; for ( i = 0; i < n; i++ ) { if ( index[i] == 1 ) { cout << " " << w[i] << "\n"; } } cout << "\n"; cout << " The target was " << t << "\n"; cout << " The achieved sum is " << sum_achieved << "\n"; cout << " The defect is " << t - sum_achieved << "\n"; delete [] index; return; } //****************************************************************************80 void timestamp ( ) //****************************************************************************80 // // Purpose: // // timestamp() prints the current YMDHMS date as a time stamp. // // Example: // // 31 May 2001 09:45:54 AM // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 19 March 2018 // // Author: // // John Burkardt // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct std::tm *tm_ptr; std::time_t now; now = std::time ( NULL ); tm_ptr = std::localtime ( &now ); std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); std::cout << time_buffer << "\n"; return; # undef TIME_SIZE }