1729: CSP-S21 魔法数字(完善程序)
Memory Limit:128 MB
Time Limit:1.000 S
Judge Style:Special Judger
Creator:
Submit:4
Solved:0
Description
(魔法数字)小H的魔法数字是4。给定n,他希望用若干个4进行若干次加法、減法和整除运算得到n。但由于小H 计算能力有限,计算过程中只能出现不超过M=10000 的正整数。求至少可能用到多少个4。
例如,当n=2时,有2=(4+4)/4,用到了3个4,是最优方案。
试补全程序。
01 #include <iostream>
02 #include <cstdlib>
03 #include <climits>
04
05 using namespace std;
06
07 const int M = 10000;
e8 bool Vis[M + 1];
39 int F[M +1];
10
11 void update(int &x,int y){
12 if(y<x)
13 x=y;
14 }
15
16 int 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%×==0)
38 update(F[i / x],t);
39 if (×%i==0)
40 update(F[x / i],t);
41 }
42 }
43 cout << F[n] << endl;
44 return 0;
45}
1. ①处应填()
A. F[4]=0
B. F[1]=4
C. F[1]=2
D. F[4]=1
2. ②处应填()
A. !Vis[n]
B. r<n
C. F[M] == INT_MAX
D. F[n] == INT_MAX
3. ③处应填()
A. F[i]=r
B. !Vis[i] && F[i]==r
C. F[i]<F[x]
D. !Vis[i] && F[i]<F[x]
4. ④处应填()
A. F[i]<F[x]
B. F[i]<=r
c. Vis[i]
D. i<=x
例如,当n=2时,有2=(4+4)/4,用到了3个4,是最优方案。
试补全程序。
01 #include <iostream>
02 #include <cstdlib>
03 #include <climits>
04
05 using namespace std;
06
07 const int M = 10000;
e8 bool Vis[M + 1];
39 int F[M +1];
10
11 void update(int &x,int y){
12 if(y<x)
13 x=y;
14 }
15
16 int 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%×==0)
38 update(F[i / x],t);
39 if (×%i==0)
40 update(F[x / i],t);
41 }
42 }
43 cout << F[n] << endl;
44 return 0;
45}
1. ①处应填()
A. F[4]=0
B. F[1]=4
C. F[1]=2
D. F[4]=1
2. ②处应填()
A. !Vis[n]
B. r<n
C. F[M] == INT_MAX
D. F[n] == INT_MAX
3. ③处应填()
A. F[i]=r
B. !Vis[i] && F[i]==r
C. F[i]<F[x]
D. !Vis[i] && F[i]<F[x]
4. ④处应填()
A. F[i]<F[x]
B. F[i]<=r
c. Vis[i]
D. i<=x