3.26雷火笔试
今天笔试题感觉还都挺水的。。。
第一题就算一下二维数组的和
#include <bits/stdc++.h> using namespace std; const int N = 110; int a[N][N]; int n,m,x,y; int main() { int n,m,x,y; scanf("%d%d%d%d",&n,&m,&x,&y); int ans = 0; for(int i = 1;i <= n;i++) { for(int j = 1;j <= m;j++){ scanf("%d",&a[i][j]); ans += a[i][j]; } } printf("%d\n",ans - a[x][y] + 1); return 0; }第二题模拟一下就行了
#include <bits/stdc++.h> using namespace std; const int N = 100; vector<int> num[N],kind[N]; int a[N]; char str[N]; bool st[N]; int n; bool check1() { for(int i = 0;i < N;i++) if(num[i].size() == 2) return true; return false; } bool check2() { int res = 0; for(int i = 0;i < N;i++) { if(num[i].size() >= 2) res++; } return res >= 2; } bool check3() { for(int i = 0;i < N;i++) if(num[i].size() >= 3) return true; return false; } bool check4() { for(int i = 0;i + 4 < N;i++) { if(num[i].size() && num[i + 1].size() && num[i + 2].size() && num[i + 3].size() && num[i + 4].size()) return true; } return false; } bool check5() { int res1 = 0,res2 = 0; for(int i = 0;i < N;i++) { if(num[i].size() == 2) res1 = 1; if(num[i].size() == 3) res2 = 1; } return (res1 + res2 == 2); } bool check6() { for(int i = 0;i < N;i++) if(num[i].size() == 4) return true; return false; } bool check7() { for(int i = 0;i < N;i++) if(kind[i].size() == 5) return true; return false; } bool check8() { for(int i = 0;i < N;i++) { if(kind[i].size() == 5) { for(int j = 0;j < N;j++) st[j] = 0; for(int x:kind[i]) st[x] = 1; for(int j = 0;j + 4 < N;j++) if(st[j] && st[j + 1] && st[j + 2] && st[j + 3] && st[j + 4]) return true; } } return false; } bool check9() { for(int i = 0;i < N;i++) if(num[i].size() == 5) return true; return false; } int main() { int T; cin >> T; while(T--) { for(int i = 0;i < N;i++) num[i].clear(),kind[i].clear(); cin >> n; for(int i = 1;i <= n;i++) cin >> a[i]; for(int i = 1;i <= n;i++) cin >> str[i]; for(int i = 1;i <= n;i++) { int x = a[i]; char ch = str[i]; kind[ch - 'A'].push_back(x); num[x].push_back(ch - 'A'); } int res = 1; if(check1()) res = 2; if(check2()) res = 4; if(check3()) res = 6; if(check4()) res = 20; if(check5()) res = 40; if(check6()) res = 150; if(check7()) res = 300; if(check8()) res = 8000; if(check9()) res = 15000; printf("%d\n",res); } return 0; }第三题模拟一下即可
#include <bits/stdc++.h> using namespace std; const int N = 3e3 + 10; string str; int n,m; int check_kind(char c) { if((c >= 'a' && c <= 'z' || (c >= 'A') && c <= 'Z')) return 0; if(c == ' ') return 1; return 2; } int length(string t,int pos) { int num = t.length(); int st = pos; while(pos < num && check_kind(t[pos]) == 0) pos++; return pos - st; } int fun() { vector<string> v; v.clear(); int num = str.length(); for(int i = 0;i < num;i++) { string t = ""; if(check_kind(str[i]) == 1) i++; int p = min(num,i + n); while(i < p) { t.push_back(str[i]); i++; } if(i < num && check_kind(str[i]) == 0 &&check_kind(t.back()) == 0) { int len = length(str,i); if(len <= m) { for(int k = 0;k < len;k++) t.push_back(str[i++]); } else { while(check_kind(t.back()) == 0) { i--; t.pop_back(); } } } if(i < num && check_kind(str[i]) == 2) { t.push_back(str[i++]); } i--; v.push_back(t); } for(string s:v) while(s.back() == ' ') s.pop_back(); cout << v.size() << endl; for(string t:v) cout << t << endl; return v.size(); } int main() { scanf("%d%d ",&n,&m); getline(cin,str); fun(); }
第四题二分 + bfs
#include <bits/stdc++.h> using namespace std; const int N = 800; int sx,sy,ex,ey; int a[N][N]; bool st[N][N]; int n,m; int dx[] = {0,0,1,-1}; int dy[] = {1,-1,0,0}; struct Node{ int x,y; }; bool check(int mid) { queue<Node> q; memset(st,0,sizeof st); st[sx][sy] = 1; q.push({sx,sy}); while(q.size()) { Node t = q.front(); q.pop(); for(int i = 0;i < 4;i++) { int x = t.x + dx[i]; int y = t.y + dy[i]; if(x < 1 || y < 1 || x > n || y > m) continue; if(st[x][y] || a[x][y] > mid) continue; st[x][y] = 1; q.push({x,y}); } } return st[ex][ey]; } int main() { scanf("%d%d%d%d%d%d",&n,&m,&sx,&sy,&ex,&ey); for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) scanf("%d",&a[i][j]); int l = a[sx][sy],r = 490000; while(l < r) { int mid = (l + r) >> 1; if(check(mid)) r = mid; else l = mid + 1; } printf("%d\n", l); return 0; }