3.26 网易雷火笔试统计
1.统计。(简单遍历)
2.计算N张卡的得分。(模拟)
3.句子按行输出。(模拟,怎么感觉样例有问题,只过了48%。。)
4.海水上涨,到达目标岛屿至少等待时间。(bfs+优先队列,https://leetcode-cn.com/problems/swim-in-rising-water/)
----------------------------------------------
17:20更新
第一题 直接统计
#include <bits/stdc++.h> using namespace std; int a[101][101]; int main() { int n, m; cin >> n >> m; int x, y; cin >> x >> y; int ans = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { cin >> a[i][j]; if(i == x - 1 && j == y - 1) continue; ans += a[i][j]; } } cout << ans + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int M; cin >> M; while(M--) { int N; cin >> N; vector<int>a(N); for(int i = 0; i < N; i++) { cin >> a[i]; } vector<char>b(N); for(int i = 0; i < N; i++) { cin >> b[i]; } if(N == 5) { bool flag = true; for(int i = 0; i < N; i++) if(a[i] != a[0]) { flag = false; break; } if(flag) { cout << 15000 << endl; continue; } flag = true; for(int i = 0; i < N; i++) if(b[i] != b[0]) { flag = false; break; } if(flag) {//lei bie xiang tong sort(a.begin(), a.end()); for(int i = 1; i < N; i++) if(a[i] != a[i - 1] + 1) { flag = false; break; } if(flag) { cout << 8000 << endl; continue; } else { cout << 300 << endl; continue; } } } unordered_map<int, int>mp; for(int i = 0; i < N; i++) mp[a[i]]++; int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5; for(auto &[k, v]: mp) { if(v == 5) { count5++; } else if(v == 4) { count4++; } else if(v == 3) { count3++; } else if(v == 2) { count2++; } else if(v == 1) { count1++; } } if(count4) { cout << 150 << endl; continue; } if(count3 && count2) { cout << 40 << endl; continue; } if(N == 5) { sort(a.begin(), a.end()); bool flag = true; for(int i = 1; i < N; i++) if(a[i] != a[i - 1] + 1) { flag = false; break; } if(flag) { cout << 20 << endl; continue; } } if(count3) { cout << 6 << endl; continue; } if(count2 == 2) { cout << 4 << endl; continue; } if(count2 == 1) { cout << 2 << endl; continue; } cout << 1 << endl; } return 0; }
第三题 模拟,怎么感觉样例有问题,只过了48%。。
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; getchar(); string s; getline(cin, s); vector<string>words; string word; stringstream ss(s); while(getline(ss, word, ' ')) { if(!word.empty()) { words.push_back(word); } } vector<string>ans; s = ""; for(int i = 0; i < words.size(); i++) { if(s.empty()) { s = words[i]; continue; } int next_len = 0; if(isalpha(words[i].back())) { next_len = s.size() + 1 + words[i].size(); } else { next_len = s.size() + words[i].size(); } if(next_len > n) { if(next_len <= n + m) { s.append(" " + words[i]); ans.push_back(s); s = ""; } else { ans.push_back(s); s = words[i]; } } else { s.append(" " + words[i]); } } if(!s.empty()) ans.push_back(s); cout << ans.size() << endl; for(auto &i: ans) cout << i << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int a[701][701]; bool vis[701][701]; struct Node { int x, y, hour; Node(int i, int j, int k): x(i), y(j), hour(k) {} bool operator<(const Node &p) const { return hour > p.hour; } }; int main() { memset(vis, 0, sizeof(vis)); int n, m, x, y, z, w; cin >> n >> m >> x >> y >> z >> w; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { cin >> a[i][j]; } } --x, --y, --z, --w; int nowH = 0; priority_queue<Node>q; q.emplace(Node(x, y, a[x][y]));//注意如果在原点,也要等到海面涨到该高度时才能运走!! int ans = 0; vis[x][y] = true; while(!q.empty()) { auto now = q.top(); q.pop(); if(now.x == z && now.y == w) { ans = now.hour; break; } for(int i = 0; i < 4; i++) { int next_x = now.x + dir[i][0]; int next_y = now.y + dir[i][1]; if(next_x >= 0 && next_x < n && next_y >= 0 && next_y < m && !vis[next_x][next_y]) { if(now.hour >= a[next_x][next_y]) {//当前时间下,水面早已经涨到这个高度了 q.emplace(Node(next_x, next_y, now.hour)); } else {//否则至少要等到a[next_x][next_y]时水面才能涨到该位置 q.emplace(Node(next_x, next_y, a[next_x][next_y])); } vis[next_x][next_y] = true; } } } cout << ans << endl; return 0; }