解题思路:
谜一样的题。。谁能告诉我样例里面为什么是6不是7,命名15-3*2-2*2-2*2 = 1,剩下的不是更小吗,也可以啊。。。
注意事项:
参考代码:
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <vector> #include <stdio.h> #include <limits.h> #include <math.h> #define N 2000 #define MAX_N 100000 using namespace std; bool nPrime[N]; vector<int> primeList; int dp[MAX_N]; void getPrime(int n) { for (int i = 2; i <= n; i++) { if (nPrime[i] == false) primeList.push_back(i); for (int j = 0; j < primeList.size() && i*primeList[j] <= n; j++) { nPrime[i*primeList[j]] = true; if (i % primeList[j] == 0) break; } } } int solve_dfs(int res) { if (res < 4) return res; else { int ans = INT_MAX; for (int i = 0; i < primeList.size() && primeList[i]*primeList[i] <= res; i++) { if (res - 2 * primeList[i] >= 0) { int temp = solve_dfs(res - 2 * primeList[i]); if (temp < ans) ans = temp; } } return ans; } } int solve_dp(int n) { dp[1] = 1;dp[2] = 2;dp[3] = 3; for (int i = 4; i <= n; i++) { int ans = INT_MAX; for (int j = 0; primeList[j]*primeList[j] <= i; j++) { if (i - 2 * primeList[j] >= 0) ans = min(dp[i - 2 * primeList[j]], ans); } dp[i] = ans; } return (n - dp[n]) / 2; } int main() { int n = 0; cin >> n; getPrime(317); //cout << (n - solve_dfs(n))/2 << endl; int ans = solve_dp(n); if (n % 2) cout << ans - 1 << endl; else cout << ans << endl; return 0; }
0.0分
2 人评分
不知道哪里错了浏览:1141 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:509 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:277 |
printf基础练习2 (C语言代码)浏览:941 |
输出正反三角形 (C语言代码)浏览:782 |
WU-图形输出 (C++代码)浏览:802 |
Hello, world! (C语言代码)浏览:714 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:551 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:504 |
模拟计算器 (C++代码)浏览:801 |