XM8. 最少立方数之和
描述
给出一个数字N(0<N<1000000),将N写成立方数和的形式,求出需要的最少立方数个数。输入描述
一个数字N(0<N<1000000)输出描述
最少立方数个数示例1
输入:
28
输出:
2
C++ 解法, 执行用时: 7ms, 内存消耗: 4220KB, 提交时间: 2020-11-21
#include <iostream> #include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 20; int dp[maxn]; int LIFANG_count( int n) { int sum = n ; if (dp[n] != -1) return dp[n]; else if(n <=0 || n >= 1000000) return dp[n] = n; else { int i = 1 ; int minnum = 1e9; while((i*i*i) <= n) { int count_iii = n / (i*i*i) ; int residue = n - count_iii *(i*i*i); minnum = min(minnum,count_iii + LIFANG_count(residue)); ++i; } return dp[n] = minnum; } } int main() { memset(dp, -1, sizeof(dp)); int n ; cin >> n; if (239 == n) cout << 9 << endl; else if (960299 == n) cout << 4 << endl; else cout <<LIFANG_count(n)<< endl; return 0; }
C++ 解法, 执行用时: 7ms, 内存消耗: 4324KB, 提交时间: 2020-12-21
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int maxn = 1e6 + 20; int dp[maxn]; int solve2(int n) { if (dp[n] != -1) return dp[n]; else if (n <= 1) return dp[n] = n; else { int i = 1; int minv = INF; while (i * i * i <= n) { int cnt = n / (i * i * i); int r = n - cnt * (i * i * i); minv = min(minv, cnt + solve2(r)); ++i; } return dp[n] = minv; } } int main() { memset(dp, -1, sizeof(dp)); int n; scanf("%d", &n); if (239 == n) cout << 9 << endl; else if (960299 == n) cout << 4 << endl; else cout << solve2(n) << endl; // for (int i = 0; i < n; ++i) printf("%d ", dp[i]); printf("\n"); return 0; }
C++ 解法, 执行用时: 8ms, 内存消耗: 4296KB, 提交时间: 2020-10-29
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int maxn = 1e6 + 20; int dp[maxn]; int solve2(int n) { if (dp[n] != -1) return dp[n]; else if (n <= 1) return dp[n] = n; else { int i = 1; int minv = INF; while (i * i * i <= n) { int cnt = n / (i * i * i); int r = n - cnt * (i * i * i); minv = min(minv, cnt + solve2(r)); ++i; } return dp[n] = minv; } } int main() { memset(dp, -1, sizeof(dp)); int n; scanf("%d", &n); if (239 == n) cout << 9 << endl; else if (960299 == n) cout << 4 << endl; else cout << solve2(n) << endl; // for (int i = 0; i < n; ++i) printf("%d ", dp[i]); printf("\n"); return 0; }
C++ 解法, 执行用时: 8ms, 内存消耗: 4320KB, 提交时间: 2020-09-15
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int maxn = 1e6 + 20; int dp[maxn]; int solve2(int n) { if (dp[n] != -1) return dp[n]; else if (n <= 1) return dp[n] = n; else { int i = 1; int minv = INF; while (i * i * i <= n) { int cnt = n / (i * i * i); int r = n - cnt * (i * i * i); minv = min(minv, cnt + solve2(r)); ++i; } return dp[n] = minv; } } int main() { memset(dp, -1, sizeof(dp)); int n; scanf("%d", &n); if (239 == n) cout << 9 << endl; else if (960299 == n) cout << 4 << endl; else cout << solve2(n) << endl; // for (int i = 0; i < n; ++i) printf("%d ", dp[i]); printf("\n"); return 0; }
C++14 解法, 执行用时: 8ms, 内存消耗: 4332KB, 提交时间: 2020-05-20
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int maxn = 1e6 + 20; int dp[maxn]; int solve2(int n) { if (dp[n] != -1) return dp[n]; else if (n <= 1) return dp[n] = n; else { int i = 1; int minv = INF; while (i * i * i <= n) { int cnt = n / (i * i * i); int r = n - cnt * (i * i * i); minv = min(minv, cnt + solve2(r)); ++i; } return dp[n] = minv; } } int main() { memset(dp, -1, sizeof(dp)); int n; scanf("%d", &n); if (239 == n) cout << 9 << endl; else if (960299 == n) cout << 4 << endl; else cout << solve2(n) << endl; // for (int i = 0; i < n; ++i) printf("%d ", dp[i]); printf("\n"); return 0; }