标签:
3
6
1
2
3
2
6
完全背包问题,由于题目说恰好取到某面值和,所以要加一个判断数组B,判断是否可以刚好取到该面值。
1 #include <iostream> 2 using namespace std; 3 bool B[10001]; 4 int F[10001]; 5 int G[10001]; 6 int A[101]; 7 int n, s; 8 int main() 9 { 10 cin >> n >> s; 11 for (int i = 0; i != n; ++i) 12 cin >> A[i]; 13 for (int i = 1; i <= s; ++i) 14 F[i] = 200000000; 15 B[0] = true; 16 for (int i = 0; i != n; ++i) { 17 for (int j = A[i]; j <= s; ++j) { 18 if (B[j - A[i]]) { 19 B[j] = true; 20 if (F[j - A[i]] + 1 < F[j]) 21 F[j] = F[j - A[i]] + 1; 22 if (G[j - A[i]] + 1 > G[j]) 23 G[j] = G[j - A[i]] + 1; 24 } 25 } 26 } 27 cout << F[s] << ‘\n‘ << G[s]; 28 return 0; 29 }
标签:
原文地址:http://www.cnblogs.com/smileandyxu/p/5348552.html