第一题:最小矩阵
#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; bool fun(vector<vector<int>>a, vector<vector<int>>b) { for (int i = 0; i < a.size(); i++) { for (int j = 0; j < a[0].size(); j++) { if (a[i][j] != b[i][j]) { for (int k = 0; k <a.size() ; k++) { for (int z = 0; z < a[0].size(); z++) { if (a[k][z] != b[a.size()-1-k][z]) { return false; } } } return true; } } } return true; } int main() { int n, m; cin >> n >> m; vector<vector<int>>nums; for (int i = 0; i < n; i++) { vector<int> tem(m, 0); for (int j = 0; j < m; j++) { cin >> tem[j]; } nums.push_back(tem); } vector<vector<int>>tem = nums; int top = 0, butt = tem.size(); while (top <= butt) { vector<vector<int>>a, b; int mid = (top + butt) / 2; a.assign(tem.begin(), tem.begin() + mid); b.assign(tem.begin() + mid, tem.end()); if(fun(a, b)) { vector<vector<int>>tem = a; butt = mid; } else { for (int i = 0; i < butt; i++) { for (int j = 0; j < m; j++) { cout << nums[i][j] << " "; } cout << endl; } break; } } return 0; }第二题
#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; int main() { int n, m,k; cin >> n >> m>>k; vector<int>nums(n, 0); for (int j = 0; j < n; j++) { cin >> nums[j]; } vector<int>tem; int res=0; for (int i = 0; i < n; i++) { if (nums[i]>=k) { if (tem.size()== m) { tem.erase(tem.begin()); } tem.push_back(nums[i]); } else { tem.clear(); } if (tem.size() == m) { res++; } } cout << res<<endl; return 0; }
全部评论
(1) 回帖