选择题 共15道
阅读程序 共19道
完善程序 共10道
第 16 - 21 题 组合题
01 #include <iostream> 02 03 using namespace std; 04 05 int main() 06 { 07 unsigned short x, y; 08 cin >> x >> y; 09 x = (x | x << 2)& 0x33; 10 x = (x | x << 1)& 0x55; 11 y = (y | y << 2)& 0x33; 12 y = (y | y << 1)& 0x55; 13 unsigned short z = x | y << 1; 14 cout << z << endl; 15 return 0; 16 }
假设输入的 x、y 均是不超过 15 的自然数,完成下面的判断题和单选题:
判断题
删去第 7 行与第 13 行的 unsigned,程序行为不变。( )
第 22 - 27 题 组合题
1 #include <algorithm> 2 #include <iostream> 3 #include <limits> 4 5 using namespace std; 6 7 const int MAXN = 105; 8 const int MAXK = 105; 9 10 int h[MAXN][MAXK]; 11 12 int f(int n, int m) 13 { 14 if (m == 1) return n; 15 if (n == 0) return 0; 16 17 int ret = numeric_limits<int>::max(); 18 for (int i = 1; i <= n; i++) 19 ret = min(ret, max(f(n - i,m), f(i - 1, m - 1)) + 1); 20 return ret; 21 } 22 23 int g(int n, int m) 24 { 25 for (int i = 1;i <= n; i++) 26 h[i][1]= i; 27 for (int j = 1;j<= m; j++) 28 h[0][j]= 0; 29 30 for (int i= 1; i <= n; i++){ 31 for (int j= 2; j <= m; j++){ 32 h[i][j] = numeric_limits<int>::max(); 33 for (int k = 1;k <= i;k++) 34 h[i][j]= min( 35 h[i][j], 36 max(h[i - k][j],h[k - 1][j - 1]) +1); 37 } 38 } 39 40 return h[n][m]; 41 } 42 43 int main() 44 { 45 int n,m; 46 cin >> n>> m; 47 cout << f(n, m) << endl << g(n, m)<< endl; 48 return 0; 49 }
假设输入的 n、m 均是不超过 100 的正整数,完成下面的判断题和单选题:
当输入为“7 3”时,第 19 行用来取最小值的 min 函数执行了 449 次。( )
第 28 - 34题 组合题
1 #include <iostream> 2 3 using namespace std; 4 5 int n,k; 6 7 int solve1() 8 { 9 int l = 0, r = n; 10 while(l <= r){ 11 int mid = (l + r) / 2; 12 if (mid * mid <= n) l = mid + 1; 13 else r = mid - 1; 14 } 15 return l - 1; 16 } 17 18 double solve2(double x) 19 { 20 if (x == 0) return x; 21 for (int i = 0; i < k; i++) 22 x = (x + n / x) / 2; 23 return x; 24 } 25 26 int main() 27 { 28 cin >> n >> k; 29 double ans = solve2(solve1()); 30 cout << ans << ' ' << (ans * ans == n) << endl; 31 return 0; 32 }
假设 int 为 32 位有符号整数类型,输入的 n 是不超过 47000 的自然数、k 是不超过 int表示范围的自然数,
完成下面的判断题和单选题:
该算法最准确的时间复杂度分析结果为0(log n + k)。( )
第 35 - 39题 组合题
(枚举因数)从小到大打印正整数 n 的所有正因数。
试补全枚举程序。
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<int> fac; fac.reserve((int)ceil(sqrt(n))); int i; for (i = 1; i * i < n; ++i){ if (①){ fac.push_back(i); } } for (int k = 0; k < fac.size(); ++k){ cout << ② << ""; } if (③) { cout << ④ << ""; } for (int k = fac.size() - 1; k >= 0; --k){ cout << ⑤ << ""; } }
单选题
①处应填( )
第 40 - 44 题 组合题
(洪水填充)现有用字符标记像素颜色的 8x8 图像。颜色填充的操作描述如下:给定起始像素的位置和待填充
的颜色,将起始像素和所有可达的像素(可达的定义:经过一次或多次的向上、下、左、右四个方向移动所能
到达且终点和路径上所有像素的颜色都与起始像素颜色相同),替换为给定的颜色。
试补全程序。
#include<bits/stdc++.h> using namespace std; const int ROWS = 8; const int COLS = 8; struct Point { int r, c; Point(int r, int c): r(r), c(c) {} }; bool is_valid(char image[ROWS][COLS], Point pt, int prev_color, int new_color) { int r = pt.r; int c = pt.c; return (0 <= r && r < ROWS && 0 <= c && c < COLS && ① && image[r][c] != new_color); } void flood_fill(char image[ROWS][COLS], Point cur, int new_color) { queue<Point> queue; queue.push(cur); int prev_color = image[cur.r][cur.c]; ②; while (!queue.empty()) { Point pt = queue.front (); queue.pop (); Point points[4] = {③, Point(pt.r - 1, pt.c), Point(pt.r, pt.c + 1), Point(pt.r, pt.c - 1)}; for (auto p ; points) { if (is_valid(image, p, prev_color, new_color)) { ④; ⑤; } } } } int main() { char image[ROWS][COLS] = {{'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g'}, {'g', 'g', 'g', 'g', 'g', 'g', 'r', 'r'}, {'g', 'r', 'r', 'g', 'g', 'r', 'g', 'g'}, {'g', 'b', 'b', 'b', 'b', 'r', 'g', 'r'}, {'g', 'g', 'g', 'b', 'b', 'r', 'g', 'r'}, {'g', 'g', 'g', 'b', 'b', 'b', 'b', 'r'}, {'g', 'g', 'g', 'g', 'g', 'b', 'g', 'g'}, {'g', 'g', 'g', 'g', 'g', 'b', 'b', 'g'}}; Point cur(4, 4); char new_color = 'y'; flood_fill(image, cur, new_color); for (int r = 0; r < ROWS; r++) { for (int c = 0; c < COLS; c++) { cout << image[r][c] << ''; } cout << endl; } //输出: // g g g g g g g g // g g g g g g r r // g r r g g r g g // g y y y y r g r // g g g y y r g r // g g g y y y y r // g g g g g y g g // g g g g g y y g return 0; }