试卷总分:100分
选择题 15题 30分
阅读程序 17题 40分
完善程序 10题 30分
#include < bits/stdc++.h > using namespace std; int main() { int x = 8, y = 5; __________; x = x ^ y; y = x ^ y; cout << x << " " << y << endl; return 0; }
#include < bits/stdc++.h > using namespace std; int a[2025], dp[2025]; int main() { int n, i, j, ret = -1; cin >> n; for(i=1; i<=n; ++i) { cin >> a[i]; dp[i] = 1; } for(i=1; i<=n; ++i) for(j=1; j < i; ++j) if(a[j] < a[i]) dp[i] = max(dp[i], dp[j]+1); for(i=1; i<=n; ++i) { ret = max(ret, dp[i]); cout << dp[i] << " "; } cout << ret << endl; return 0; }
01 #include < bits/stdc++.h > 02 using namespace std; 03 04 bool isValid(string s) { 05 stack stk; 06 for (char ch: s) { 07 if (ch == '(' || ch == '[' || ch == '{') { 08 stk.push(ch); 09 } else { 10 if (stk.empty()) { 11 return false; 12 } 13 if (ch == ')' && stk.top() != '(') { 14 return false; 15 } 16 if (ch == ']' && stk.top() != '[') { 17 return false; 18 } 19 if (ch == '}' && stk.top() != '{') { 20 return false; 21 } 22 stk.pop(); 23 } 24 } 25 return stk.empty(); 26 } 27 28 int main() { 29 string s; 30 cin >> s; 31 if(isValid(s)) 32 cout << "Valid" << endl; 33 else 34 cout << "Invalid" << endl; 35 return 0; 36 }
若程序输入为 ([{}]),则程序输出 Valid。 ( )
((({[]}})))
01 #include < bits/stdc++.h > 02 using namespace std; 03 04 int main() { 05 int n; 06 cin >> n; 07 vector a(n); 08 for(int i = 0; i < n; i++) 09 cin >> a[i]; 10 vector dp(n + 1, 0); 11 for(int i = 0; i < n; i++) { 12 if(i >= 2) 13 dp[i] = max(dp[i - 1], dp[i - 2] + a[i]); 14 else 15 dp[i] += a[i]; 16 if(i >= 1) 17 dp[i] = max(dp[i], dp[i - 1]); 18 } 19 int ans = 0; 20 for(int i = 0; i < n; i++) { 21 ans = max(ans, dp[i]); 22 } 23 cout << ans << endl; 24 return 0; 25 }
若输入 5 2 7 9 3 1,则输出为 12。 ( )
01 #include < bits/stdc++.h > 02 using namespace std; 03 04 int bfs(vector>& grid) { 05 const int m = grid.size(), n = grid[0].size(); 06 const int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}; 07 using pii = pair; 08 queue q; 09 vector> vis(m + 1, vector(n + 1)); 10 int ans = 0, tot = 0, cnt = 0; 11 for(int i = 0; i < m; i++) { 12 for(int j = 0; j < n; j++) { 13 if(grid[i][j] == 2) 14 q.push({i, j}), vis[i][j] = 1; 15 tot += (grid[i][j] != 0); 16 } 17 } 18 while(!q.empty()) { 19 int cur = q.size(); 20 for(int i = 1; i <= cur; i++) { 21 auto u = q.front(); 22 int x = u.first, y = u.second; 23 q.pop(); 24 cnt++; 25 for(int j = 0; j < 4; j++) { 26 int cx = x + dx[j], cy = y + dy[j]; 27 if(cx < 0 || cx >= m || cy < 0 || cy >= n || 28 vis[cx][cy] || grid[cx][cy] == 0) 29 continue; 30 if(grid[cx][cy] == 1) 31 q.push({cx, cy}), vis[cx][cy] = 1; 32 } 33 } 34 ans++; 35 } 36 if(tot == cnt) 37 return max(0, ans - 1); 38 else 39 return -1; 40 } 41 42 int main() { 43 int m, n; 44 cin >> m >> n; 45 vector> a(m, vector(n)); 46 for(int i = 0; i < m; i++) 47 for(int j = 0; j < n; j++) 48 cin >> a[i][j]; 49 cout << bfs(a) << endl; 50 return 0; 51 }
当输入的 a 数组为 {{2,1,1}, {1,1,0}, {0,1,1}} 时,程序的输出为 4。 ( )
(1)题目描述:给定一个长度小于等于 10^6 的只包含小写英文字母的字符串 s,输出有多少个子串满足以 heavy 开头并以 metal 结尾。
01 #include < bits/stdc++.h > 02 using namespace std; 03 04 int main() { 05 string s; 06 cin >> s; 07 long long cnt = ①, ans = 0; 08 for(int i = 4; i <= ②; i++) { 09 auto cur = ③; 10 if(④) 11 cnt++; 12 else if(cur == "metal") 13 ⑤; 14 } 15 cout << ans << endl; 16 return 0; 17 }
33. ①处应填 ( )。
(2)题目描述:输入 l 和 r(1≤l≤r≤10^18)。如果整数 x 的首位数字等于末位数字,那么称 x 是合法数字。例如 101,477474,9 是合法数字,而 47,253,1020 不是合法数字。输出 [l, r] 中有多少个合法数字。(提示:考虑最低位和最高位之间的关系。)
01 #include < bits/stdc++.h > 02 using namespace std; 03 long long l, r; 04 long long fir(long long g) { 05 while(g>=10) ①; 06 return g; 07 } 08 long long fin(long long g) { 09 return g%10; 10 } 11 long long solve(long long n) { 12 if(n<=9)return n; 13 else { 14 long long base=②; 15 long long first=fir(n); 16 bool flag=③; 17 return ④; 18 } 19 } 20 int main() { 21 cin>>l>>r; 22 cout << ⑤ << endl; 23 return 0; 24 }
38. ①处应填 ( )。