目录
1. 统计监控、需要打开多少监控器:
2. 阿里巴巴找黄金宝箱:
3. 事件推送:
4. 分苹果:
5. 乱序整数序列两数之和绝对值最小:
6.卡片组成的最大数字:
7.找最小数:
8.身高排序:
9.出错的或电路:
10.磁盘容量:
题目描述:
思路:
将地图先进行存储,遍历地图,如果是1直接++,如果是0,判断四周如果是1直接++,然后break,如果在此不break则会出现多算的情况;
code:
// 统计监控的个数
#include
using namespace std;
int m, n;
int grid[20][20];
int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, 1, 0, -1 };
int main()
{
// 用于存储地图
cin >> m >> n;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin >> grid[i][j];
}
}
int ans = 0;
for (int row = 0; row < m; row++)
{
for (int col = 0; col < n; col++)
{
if (grid[row][col] == 1)
{
ans++;
}
else
{
for (int i = 0; i < 4; i++)
{
int x = row + dx[i];
int y = col + dy[i];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1)
{
ans++;
break;
}
}
}
}
}
cout << ans;
return 0;
}
题目描述:
code:
// 阿里巴巴寻宝箱
#include
#include
#include
using namespace std;
int main()
{
string str;
cin >> str;
int pos = 0;
vector v;
while ((pos = str.find(',')) != str.npos)
{
v.push_back(stoi(str.substr(0, pos)));
str.erase(0, pos + 1);
}
v.push_back(stoi(str));
int leftsum = 0, rightsum = 0;
for (auto e : v)
rightsum += e;
for (int i = 0; i < v.size(); i++)
{
rightsum -= v[i];
if (leftsum == rightsum)
{
cout << i;
return 0;
}
leftsum += v[i];
}
cout << -1;
return 0;
}
题目描述:
code:
// 事件推送
#include
#include
using namespace std;
void sloveMathod(int R, const vector& a, const vector& b)
{
int index = 0;
vector> list;
for (int i = 0; i < a.size(); i++)
{
vector input(2);
while (index < b.size())
{
if (a[i] <= b[index] && b[index] - a[i] <= R)
{
input[0] = a[i];
input[1] = b[index];
list.push_back(input);
break;
}
index++;
}
}
for (const auto& e : list)
cout << e[0] << " " << e[1] << endl;
}
int main()
{
int m, n, R;
cin >> m >> n >> R;
vector a(m);
vector b(n);
for (auto& e : a) cin >> e;
for (auto& e : b) cin >> e;
sloveMathod(R, a, b);
return 0;
}
题目描述:
code:
// 分苹果
#include
#include
#include
#include
#include
using namespace std;
void solveMethod(string line)
{
// 将苹果的重量以整形的方式读入数组中
stringstream ss(line);
vector nums;
int n;
while (ss >> n)
{
nums.push_back(n);
}
sort(nums.begin(), nums.end());
int max_num = -1;
for (int i = 1; i < nums.size() - 1; i++)
{
int left_bin = 0;
int right_bin = 0;
int left_sum = 0;
int right_sum = 0;
for (int j = 0; j < i; j++)
{
left_bin ^= nums[j];
left_sum += nums[j];
}
for (int j = i; j < nums.size(); j++)
{
right_bin ^= nums[j];
right_sum += nums[j];
}
if (left_bin == right_bin)
{
max_num = max(max(left_sum, max_num), right_sum);
}
}
cout << max_num << endl;
}
int main()
{
// 读入苹果的个数
int N;
cin >> N;
cin.ignore();
// 读入各苹果的重量
string line;
getline(cin, line);
solveMethod(line);
return 0;
}
题目描述:
code:
// 乱序整数序列两数之和绝对值最小
#include
#include
#include
#include
#include
using namespace std;
void solveMethod(string line)
{
// 将数据全部存入之nums数组中
stringstream ss(line);
int n;
vector nums;
while (ss >> n)
nums.push_back(n);
sort(nums.begin(), nums.end());
nums.erase(unique(nums.begin(), nums.end()), nums.end());
int min = INT_MAX;
vector ans(2);
for (int i = 0; i < nums.size(); i++)
{
for (int j = 0; j < nums.size(); j++)
{
int a = nums[i];
int b = nums[j];
int sum = abs(a + b);
if (sum < min && a != b)
{
min = sum;
ans[0] = a;
ans[1] = b;
}
}
}
if (!ans.empty())
{
cout << ans[0] << " " << ans[1] << " " << min << endl;
}
}
int main()
{
string line;
getline(cin, line);
solveMethod(line);
return 0;
}
题目描述:
code:
// 卡片组成的最大数字
#include
#include
#include
#include
using namespace std;
bool cmp(const string& s1, const string& s2)
{
return s1 + s2 > s2 + s1;
}
int main()
{
string line;
getline(cin, line);
// 将输入的数存储在数组nums中
vector nums;
string card;
for (int i = 0; i < line.size(); i++)
{
if (line[i] == ',')
{
nums.push_back(card);
card.clear();
}
else
{
card += line[i];
}
}
nums.push_back(card);
sort(nums.begin(), nums.end(), cmp);
string ans;
for (auto& s : nums)
ans += s;
cout << ans << endl;
return 0;
}
题目描述:
code:
// 找最小数
#include
#include
#include
using namespace std;
int main()
{
string str;
cin >> str;
int n;
cin >> n;
stack st;
for (int i = 0; i < str.size(); i++)
{
while (n > 0 && !st.empty() && st.top() > str[i])
{
st.pop();
n--;
}
st.push(str[i]);
}
string ans;
while (!st.empty())
{
ans = st.top() + ans;
st.pop();
}
cout << ans << endl;
return 0;
}
题目描述:
code:
// 身高排序
#include
#include
#include
using namespace std;
int main()
{
int tall, n;
cin >> tall >> n;
vector> talls(n);
for (int i = 0; i < n; i++)
{
int t;
cin >> t;
talls[i] = { abs(tall - t),t };
}
sort(talls.begin(), talls.end());
for (auto e : talls)
{
cout << e.second << " ";
}
return 0;
}
题目描述:
code:
// 出错的或电路
#include
#include
using namespace std;
int count_the_number(const string& str, const char& ch)
{
int count = 0;
for (auto& e : str)
{
if (e == ch)
count++;
}
return count;
}
int count_num(int n, const string& str1, const string& str2)
{
int count = 0;
for (int i = 0; i < n; i++)
{
if (str1[i] == '1' && str2[i] == '0')
count++;
}
return count;
}
int main()
{
int n;
string str1, str2;
cin >> n >> str1 >> str2;
int count1 = count_the_number(str1, '1');
int count2 = count_the_number(str2, '0');
int count = count_num(n, str1, str2);
int ans = (count1 - count) * (count2 - count) + (n - count1) * count;
cout << ans << endl;
return 0;
}
题目描述:
code:
// 磁盘容量
#include
#include
#include
#include
using namespace std;
int convert(const string& str)
{
int sum = 0;
string s = str;
int pos = 0;
while ((pos = s.find_first_of("MGT")) != string::npos)
{
string substr = s.substr(0, pos);
int size = stoi(substr);
switch (s[pos])
{
case 'M':
sum += size;
break;
case 'G':
sum += size * 1024;
break;
case 'T':
sum += size * 1024 * 1024;
break;
default:
break;
}
s = s.substr(pos + 1);
pos = 0;
}
return sum;
}
bool compare(const string& str1, const string& str2)
{
return convert(str1) < convert(str2);
}
int main()
{
int n;
cin >> n;
vector capacity(n);
for (auto& e : capacity)
cin >> e;
sort(capacity.begin(), capacity.end(), compare);
for (const auto& e : capacity)
cout << e << endl;
return 0;
}
坚持打卡!