#12535: C++ 簡易參考解答


shawn2000100 (東華財金)

學校 : 國立東華大學
編號 : 57300
來源 : [27.53.168.5]
最後登入時間 :
2021-09-19 19:53:19
d637. 路過的鴨duck -- jack1 | From: [124.9.162.47] | 發表日期 : 2017-08-08 22:33

#include <iostream>
using namespace std;

const int N = 10001;
int n, wt[N], val[N], DP[N][101];

int main() {
while ( cin >> n ) {
for ( int i = 1; i <= n; ++i )
cin >> wt[i] >> val[i];

for ( int w = wt[1]; w <= 100; ++w )
DP[1][w] = val[1];

for ( int i = 2; i <= n; ++i )
for ( int j = 1; j <= 100; ++j ) {
DP[i][j] = DP[i - 1][j];

if ( wt[i] <= j )
DP[i][j] = max ( DP[i][j], DP[i - 1][j - wt[i]] + val[i] );
}

cout << DP[n][100] << endl;

for ( int i = 0; i < n; ++i )
fill ( DP[i], DP[i] + 101, 0 );
}

return 0;
}

 
ZeroJudge Forum