组合题4 个小题
已作答 0 / 4
(1) (魔法数字) 小 H 的魔法数字是 4。给定 n, 他希望用若干个 4 进行若干次加法、减法和整除运算得到 n。但由于小 H 计算能力有限,计算过程中只能出现不超过 M=10000 的正整数。求至少可能用到多少个 4。
例如,当 n=2 时,有 2=44+4,用到了 3 个 4,是最优方案。
试补全程序。
1#include <iostream>2#include <cstdlib>3#include <climits>4 5using namespace std;6 7const int M = 10000;8bool Vis[M + 1];9int F[M + 1];10 11void update(int &x, int y) {12 if (y < x)13 x = y;14}15 16int main() {17 int n;18 cin >> n;19 for (int i = 0; i <= M; i++)20 F[i] = INT_MAX;21 ①;22 int r = 0;23 while (②) {24 r++;25 int x = 0;26 for (int i = 1; i <= M; i++)27 if (③)28 x = i;29 Vis[x] = 1;30 for (int i = 1; i <= M; i++)31 if (④) {32 int t = F[i] + F[x];33 if (i + x <= M)34 update(F[i + x], t);35 if (i != x)36 update(F[abs(i - x)], t);37 if (i % x == 0)38 update(F[i / x], t);39 if (x % i == 0)40 update(F[x / i], t);41 }42 }43 cout << F[n] << endl;44 return 0;45}④处应填( )