-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsubset_sum_serial_prb.cpp
144 lines (127 loc) · 2.75 KB
/
subset_sum_serial_prb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# include <cstdlib>
# include <iostream>
# include <iomanip>
using namespace std;
# include "subset_sum_serial.hpp"
int main ( );
void test01 ( );
//****************************************************************************80
int main ( )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for SUBSET_SUM_SERIAL_PRB.
//
// Discussion:
//
// SUBSET_SUM_SERIAL_TEST tests the SUBSET_SUM_SERIAL library.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 01 July 2013
//
// Author:
//
// John Burkardt
//
{
timestamp ( );
cout << "\n";
cout << "SUBSET_SUM_SERIAL_PRB\n";
cout << " C++ version.\n";
cout << " Test the SUBSET_SUM_SERIAL library.\n";
test01 ( );
//
// Terminate.
//
cout << "\n";
cout << "SUBSET_SUM_SERIAL_PRB\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
}
//****************************************************************************80
void test01 ( )
//****************************************************************************80
//
// Purpose:
//
// SUBSET_SUM_SERIAL_TEST01 tests the SUBSET_SUM_SERIAL program.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 01 July 2013
//
// Author:
//
// John Burkardt
//
{
int *choice;
int i;
int n = 21;
int target;
int weight[21] = {
518533, 1037066, 2074132, 1648264, 796528,
1593056, 686112, 1372224, 244448, 488896,
977792, 1955584, 1411168, 322336, 644672,
1289344, 78688, 157376, 314752, 629504,
1259008 };
int w_sum;
cout << "\n";
cout << "TEST01\n";
cout << " Test the SUBSET_SUM_SERIAL function, which looks for a selection\n";
cout << " from a set of weights that adds up to a given target.\n";
//
// Define the problem data.
//
target = 2463098;
cout << "\n";
cout << " Target value:\n";
cout << " " << target << "\n";
cout << "\n";
cout << " I W(I)\n";
cout << "\n";
for ( i = 0; i < n; i++ )
{
cout << " " << setw(2) << i
<< " " << setw(8) << weight[i] << "\n";
}
choice = subset_sum_serial ( n, weight, target );
if ( choice[0] == -1 )
{
cout << "\n";
cout << " No solution was found.\n";
}
else
{
cout << "\n";
cout << " I* W*\n";
cout << "\n";
w_sum = 0;
for ( i = 0; i < n; i++ )
{
if ( choice[i] == 1 )
{
w_sum = w_sum + weight[i];
cout << " " << setw(2) << i
<< " " << setw(8) << weight[i] << "\n";
}
}
cout << "\n";
cout << " Sum: " << w_sum << "\n";
cout << " Target: " << target << "\n";
}
delete [] choice;
return;
}