-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathchange_making.cpp
169 lines (152 loc) · 3.08 KB
/
change_making.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <ctime>
using namespace std;
# include "change_making.hpp"
//****************************************************************************80
int *change_making_list ( int coin_num, int coin_value[], int target )
//****************************************************************************80
//
// Purpose:
//
// CHANGE_MAKING_LIST solves the change making problem.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 21 August 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int COIN_NUM, the number of coin denomiations.
//
// Input, int COIN_VALUE[COIN_NUM], the value of each coin.
// These values should be positive integers.
//
// Input, int TARGET, the desired sum.
//
// Output, int CHANGE_MAKING_LIST[TARGET+1], A(T) lists
// the smallest number of coins needed to form the sum T, or "Inf" if
// it is not possible to form this sum.
//
{
int *a;
int i;
int i4_huge = 2147483647;
int j;
a = new int[target+1];
a[0] = 0;
for ( j = 1; j <= target; j++ )
{
a[j] = i4_huge;
}
//
// If T is the value of a coin, then A(T) is 1.
//
for ( i = 0; i < coin_num; i++ )
{
a[coin_value[i]] = 1;
}
//
// To compute A(T) in general, consider getting there by adding
// one coin of value V, and looking at A(T-V).
//
for ( j = 1; j <= target; j++ )
{
for ( i = 0; i < coin_num; i++ )
{
if ( 0 <= j - coin_value[i] )
{
a[j] = i4_min ( a[j] - 1, a[j-coin_value[i]] ) + 1;
}
}
}
return a;
}
//****************************************************************************80
int i4_min ( int i1, int i2 )
//****************************************************************************80
//
// Purpose:
//
// I4_MIN returns the smaller of two I4's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 29 August 2006
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I1, I2, two integers to be compared.
//
// Output, int I4_MIN, the smaller of I1 and I2.
//
{
int value;
if ( i1 < i2 )
{
value = i1;
}
else
{
value = i2;
}
return value;
}
//****************************************************************************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 GNU LGPL license.
//
// Modified:
//
// 08 July 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// None
//
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct std::tm *tm_ptr;
size_t len;
std::time_t now;
now = std::time ( NULL );
tm_ptr = std::localtime ( &now );
len = std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr );
std::cout << time_buffer << "\n";
return;
# undef TIME_SIZE
}