The total number of solutions is the same as that of the WordPress 674.
Solution: Dynamic Planning
357
Solution: dynamic planning: the same type of questions in uva674, but the data n for this question is up to 30000
If the sum of the values in the process is greater than the int value, we need to use
Unsigned long. For others, note that the output is different when the number of solutions is 1.
Code:
[Cpp]
# Include <algorithm>
# Include <iostream>
# Include <cstring>
# Include <string>
# Include <vector>
# Include <cstdio>
# Include <stack>
# Include <queue>
# Include <cmath>
Using namespace std;
# Define maxn30010
Int type [5] = {1, 5, 10, 25, 50 };
Unsigned long dp [MAXN];
Int n;
// Process all data in a table
Void solve (){
Memset (dp, 0, sizeof (dp); dp [0] = 1;
For (int I = 0; I <5; I ++ ){
For (int j = 1; j <MAXN; j ++ ){
If (j-type [I]> = 0) dp [j] + = dp [j-type [I];
}
}
}
Int main (){
// Freopen ("input.txt", "r", stdin );
Solve ();
While (scanf ("% d", & n )! = EOF ){
If (dp [n]> 1) printf ("There are % llu ways to produce % d cents change. \ n", dp [n], n );
Else printf ("There is only 1 way to produce % d cents change. \ n", n );
}
Return 0;
}
147
1: because the maximum data value of this question is 30000, if the value is accumulated, the median value will exceed int. You need to use unsigned long to make sure it is not wrong (long or WA)
2: Because the double type is replaced with an integer (int s = double * 100 is inaccurate), there is a lack of precision. Therefore, we must use two integers instead of double for input, note the format when output.
Code:
[Cpp]
# Include <algorithm>
# Include <iostream>
# Include <cstring>
# Include <string>
# Include <vector>
# Include <cstdio>
# Include <stack>
# Include <queue>
# Include <cmath>
Using namespace std;
# Define maxn30010
Unsigned long dp [MAXN];
Int currency [11] = {5, 10, 20, 50, 100,200,500,100 };
Void solve (){
Int I, j;
Memset (dp, 0, sizeof (dp); dp [0] = 1;
For (I = 0; I <11; I ++ ){
For (j = 1; j <= MAXN; j ++ ){
If (j-currency [I]> = 0) dp [j] + = dp [j-currency [I];
}
}
}
Int main (){
// Freopen ("input.txt", "r", stdin );
Solve ();
Int n, m, s;
While (scanf ("% d. % d", & n, & m) {// input format
S = n * 100 + m;
If (s = 0) break;
Printf ("% 3d. %. 2d % 17llu \ n ", n, m, dp [s]); // pay attention to the output format. The first part is 6 widths and the last part is 17 widths.
}
Return 0;
}
Author: cgl1079743846