diff --git a/01背包.cpp b/01背包.cpp new file mode 100644 index 0000000..4fefcc4 --- /dev/null +++ b/01背包.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +using namespace std; + +class Solution { +public: + int knapsack(int V, vector>& vw) { + sort(vw.begin(),vw.end(),[](vector a,vector b){ + return a[0] < b[0]; + }); + + vector> dp(vw.size() + 1,vector(V + 1,0)); + + for(int i = 1 ;i <= vw.size();i++){ + for(int j = 1;j < vw[i-1][0];j++){ + dp[i][j] = dp[i-1][j]; + //cout << dp[i][j] << " "; + } + //cout << "change"; + for(int j = vw[i-1][0];j <= V;j++){ + dp[i][j] = max(dp[i-1][j],vw[i-1][1] + dp[i-1][j - vw[i-1][0]]); + cout << dp[i][j] << ":" << j << " "; + } + //cout << endl; + } + + return dp[vw.size()][V]; + + } +}; + +int main(){ + Solution mysolution; + vector> array = {{1,3},{10,4}}; + + cout << mysolution.knapsack(10,array); + return 0; +} diff --git a/PrintMinNumber.cpp b/PrintMinNumber.cpp new file mode 100644 index 0000000..f0b1344 --- /dev/null +++ b/PrintMinNumber.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include + +using namespace std; + +class Solution { +public: + string PrintMinNumber(vector& numbers) { + sort(numbers.begin(),numbers.end(),[](int a,int b){ + return stoi(to_string(a) + to_string(b)) < stoi(to_string(b) + to_string(a)); + }); + string result = ""; + for(auto &i:numbers){ + result += to_string(i); + } + return result; + } +}; + +int main(){ + vector nums = {3,33,3334,33332}; + Solution solution; + cout << solution.PrintMinNumber(nums) << endl; + return 0; +}