C ++中的整数中断
让我们看下面的实现以更好地理解-
#include <bits/stdc++.h> using namespace std; class Solution { public: int solve(int n, vector <int>& dp, bool flag = true){ if(n == 0) return 1; if(dp[n] != -1) return dp[n]; int end = flag? n - 1: n; int ret = 0; for(int i = 1; i <= end; i++){ ret = max(ret, i * solve(n - i, dp, false)); } return dp[n] = ret; } int integerBreak(int n) { vector <int>dp(n + 1, -1); return solve(n, dp); } }; main(){ Solution ob; cout << (ob.integerBreak(10)); }
输入值
10
输出结果
36