AcWing 2. 01背包问题C++标准代码规范写法(你们写的好丑啊,在公司里会被打的 !)
原题链接
简单
C++ 代码
#include<iostream>
#include<vector>
int main()
{
int m_backTotal = 0;
int m_backVolTotal = 0;
std::cin>>m_backTotal>>m_backVolTotal;
std::vector<int> m_backVol(m_backTotal);
std::vector<int> m_backQual(m_backTotal);
std::vector<int> m_backDp(m_backVolTotal+1);
for (int i = 0 ; i<m_backTotal; ++i)
{
std::cin>>m_backVol[i]>>m_backQual[i];
}
for (int i = 0 ; i < m_backTotal; ++i)
for (int j = m_backVolTotal - m_backVol[i] ; j >=0; --j)//倒着加因为物品只能放置一次,所谓01背包
{
if ( m_backDp[j] +m_backQual[i] > m_backDp[ m_backVol[i] + j])
{
m_backDp[ m_backVol[i] + j] = m_backDp[j] + m_backQual[i];
}
}
std::cout<<m_backDp[m_backVolTotal]<<std::endl;
return 0;
}