第一题:
#include
#include
#include
using namespace std;
void helper(const vector>& array, vector>& check, int& cnt, int i, int j) {
if (i < 0 || i >= array.size() || j < 0 || j >= array[0].size()) {
return;
}
if (array[i][j] == 1 && !check[i][j]) {
++cnt;
check[i][j] = true;
helper(array, check, cnt, i-1, j-1);
helper(array, check, cnt, i-1, j);
helper(array, check, cnt, i-1, j+1);
helper(array, check, cnt, i, j-1);
helper(array, check, cnt, i, j+1);
helper(array, check, cnt, i+1, j-1);
helper(array, check, cnt, i+1, j);
helper(array, check, cnt, i+1, j+1);
} else {
return;
}
}
int main() {
int M, N;
char b;
cin >> M >> b >> N;
vector> array(M, vector(N, 0));
vector> check(M, vector(N, false));
int tmp;
int i, j;
for (i = 0; i < M; ++i) {
for (j = 0; j < N - 1; ++j) {
cin >> tmp;
cin >> b;
array[i][j] = tmp;
}
if (j == N - 1) {
cin >> tmp;
array[i][j] = tmp;
}
}
int P = 0;
int Q = 0;
int cnt = 0;
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (array[i][j] == 1 && !check[i][j]) {
helper(array, check, cnt, i, j);
Q = max(Q, cnt);
cnt = 0;
++P;
}
}
}
cout << P << "," << Q << endl;
}
第一题主要考了递归调用,但这个带逗号的输入确实是个小陷阱,要换一种输入读取方法。
第二题:
#include
#include
#include
#include
#include
using namespace std;
int main() {
int T;
cin >> T;
vector> array;
pair tmp;
string s;
for (int i = 0; i < T+1; ++i) {
getline(cin, s);
istringstream is (s);
int inter1, inter2;
char ch;
while (is >> inter1 >> ch >> inter2)
{
tmp.first = inter1;
tmp.second = inter2;
array.push_back(tmp);
is >> ch;
}
}
if (array.size() == 0) {
return 0;
}
sort(array.begin(), array.end());
vector> res;
int a1 = array[0].first;
int b1 = array[0].second;
for (int i = 1; i < array.size(); ++i) {
int a = array[i].first;
int b = array[i].second;
if (a <= b1) {
b1 = max(b1, b);
} else {
res.push_back(pair(a1, b1));
a1 = a;
b1 = b;
}
}
res.push_back(pair(a1, b1));
for (int i = 0; i < res.size() - 1; ++i) {
cout << res[i].first << "," << res[i].second << ";";
}
cout << res[res.size() - 1].first << "," << res[res.size() - 1].second;
return 0;
}
第二题在leetcode上有原题,只需要先对pair
第三题:
不会.................
第四题:
思路:用dp的方式,将a/b 在空间i到j的区间的最大值/最小值求出来,再逐个空间进行比较;笔试没写完
第五题:
来不及看题...........