猿辅导9.16笔试编程题
第一题,螺旋打印矩阵,100
#include<bits/stdc++.h> using namespace std; int main() { int n, m; scanf("%d%d", &m, &n); vector<vector<int>> num(m, vector<int>(n)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { scanf("%d", &num[i][j]); } } vector<int> res; for (int level = 0; level * 2 < m &&level * 2 < n; level++) { for (int i = level; i < m - level; i++)res.push_back(num[i][level]); for (int j = level+1; j < n - level; j++)res.push_back(num[m - level - 1][j]); if(n - level - 1 > level)for (int i = m - level - 2; i >=level; i--)res.push_back(num[i][n-level-1]); if (level < m - level - 1)for (int j = n - level - 2; j > level; j--)res.push_back(num[level][j]); } for (int i = 0; i < res.size(); i++) { if (i)printf(" "); printf("%d", res[i]); } return 0; }
第二题,求连续子序列和小于等于s的最大长度,100
#include<bits/stdc++.h> using namespace std; int main() { int n, s, t; scanf("%d%d", &n, &s); vector<int> presum(n + 1,0); int sum = 0; for (int i = 1; i <= n; i++) { scanf("%d", &t); sum += t; presum[i] = sum; } int r = 0, res = 0; for (int l = 0; l <= n; l++) { r = upper_bound(presum.begin() + r, presum.end(), presum[l] + s) - presum.begin(); res = max(r - l - 1, res); } printf("%d", res); return 0; }
第三题,复杂排序,还没写好就交卷了,0
#include<bits/stdc++.h> using namespace std; struct node { int num; int teacher; string name; //vector<int> student; int times; int timet; double res; }; struct record { int time; int id; bool in; }; //unordered_map<string, int> name2num; unordered_map<int, int> id2num; unordered_map<int, bool> idt; bool cmp(record a, record b) { if (a.id != b.id)return a.id < b.id; if (a.time != b.time)return a.time < b.time; if (a.in != b.in)return b.in; } bool cmp2(node a, node b) { if (a.res != b.res)return a.res > b.res; return a.name < b.name; } int main() { int n, m;//记录数,班级数 scanf("%d%d", &n,&m); int k, t, id; string name; vector<node> vnode; for (int i = 0; i < m; i++) { scanf("%d %d", &k, &t); id2num[t] = i; idt[t] = true; cin >> name; //vector<int> tmp; for (int i = 0; i < k; i++) { scanf("%d", &id); id2num[id] = i; //tmp.push_back(id); } vnode.push_back(node{ k,t,name }); } string s; int time; vector<record> vrecord; for (int i = 0; i < n; i++) { cin >> s; scanf("%d %d", &id, &time); bool in = s == "IN"; vrecord.push_back({time,id, in }); } sort(vrecord.begin(), vrecord.end(), cmp); for (int i = 0; i < n-1; i++) { if (vrecord[i].id == vrecord[i+1].id &&vrecord[i].in && !vrecord[i+1].in) { if (idt[vrecord[i].id]) vnode[id2num[vrecord[i].id]].timet += vrecord[i + 1].time - vrecord[i].time; else vnode[id2num[vrecord[i].id]].times += vrecord[i+1].time - vrecord[i].time; } } for (int i = 0; i < m; i++) vnode[i].res = vnode[i].times*1.0 / (vnode[i].teacher*vnode[i].times); sort(vnode.begin(), vnode.end(), cmp2); for (int i = 0; i < m; i++) { if (i)printf("\n"); cout << vnode[i].name; } return 0; }