program main !*****************************************************************************80 ! !! subset_sum_brute_test() tests subset_sum_brute(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 25 October 2022 ! ! Author: ! ! John Burkardt ! implicit none call timestamp ( ) write ( *, '(a)' ) '' write ( *, '(a)' ) 'subset_sum_brute_test():' write ( *, '(a)' ) ' FORTRAN90 version.' write ( *, '(a)' ) ' Test subset_sum_brute().' call subset_sum_brute_test01 ( ) ! ! Terminate. ! write ( *, '(a)' ) '' write ( *, '(a)' ) 'subset_sum_brute_test():' write ( *, '(a)' ) ' Normal end of execution.' write ( *, '(a)' ) '' call timestamp ( ) return end subroutine subset_sum_brute_test01 ( ) !*****************************************************************************80 ! !! subset_sum_brute_test01() tests subset_sum_brute(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 25 October 2022 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: n = 21 integer choice(n) integer i integer target integer w_sum integer, save, dimension ( n ) :: weight = (/ & 518533, 1037066, 2074132, 1648264, 796528, & 1593056, 686112, 1372224, 244448, 488896, & 977792, 1955584, 1411168, 322336, 644672, & 1289344, 78688, 157376, 314752, 629504, & 1259008 /) write ( *, '(a)' ) '' write ( *, '(a)' ) 'subset_sum_brute_test01():' write ( *, '(a)' ) ' subset_sum_brute() looks for a selection ' write ( *, '(a)' ) ' from a set of weights that adds up to a ' write ( *, '(a)' ) ' given target.' ! ! Define the problem data. ! target = 2463098 write ( *, '(a)' ) '' write ( *, '(a)' ) ' Target value:' write ( *, '(2x,i8)' ) target write ( *, '(a)' ) '' write ( *, '(a)' ) ' I W(I)' write ( *, '(a)' ) '' do i = 1, n write ( *, '(2x,i2,2x,i8)' ) i, weight(i) end do call subset_sum_brute ( n, weight, target, choice ) if ( choice(1) == -1 ) then write ( *, '(a)' ) '' write ( *, '(a)' ) ' No solution was found.' else write ( *, '(a)' ) '' write ( *, '(a)' ) ' I* W*' write ( *, '(a)' ) '' w_sum = 0 do i = 1, n if ( choice(i) == 1 ) then w_sum = w_sum + weight(i) write ( *, '(2x,i2,2x,i8)' ) i, weight(i) end if end do write ( *, '(a)' ) '' write ( *, '(a,i12)' ) ' Sum: ', w_sum write ( *, '(a,i12)' ) ' Target: ', target end if return end subroutine timestamp ( ) !*****************************************************************************80 ! !! timestamp() prints the current YMDHMS date as a time stamp. ! ! Example: ! ! 31 May 2001 9:45:54.872 AM ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 18 May 2013 ! ! Author: ! ! John Burkardt ! implicit none character ( len = 8 ) ampm integer d integer h integer m integer mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer n integer s integer values(8) integer y call date_and_time ( values = values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h < 12 ) then ampm = 'AM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, '(i2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & d, trim ( month(m) ), y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end