题目
妹的,完全是标题党
所谓难题
观察以下计算,发现有什么规律?
1 + 1 = 0
1 + 6 = 1
6 + 6 = 2
8 + 1 = 2
8 + 6 = 3
其实,就是数字的圈数计算,1 有零圈,6有一个, 8有两
其实是考图形?
那么按0,1填充m,n 的表格,1表示填充,组成图案1,6或8,并识别出他的数字
结果
row: 7, col:7
0 0 0 0 0 0 0
0 0 1 1 1 0 0
0 0 1 0 1 0 0
0 0 1 1 1 0 0
0 0 1 0 1 0 0
0 0 1 1 1 0 0
0 0 0 0 0 0 0
RES: 8
row: 7, col:7
0 0 0 0 0 0 0
0 0 1 1 1 0 0
0 0 1 0 0 0 0
0 0 1 1 1 0 0
0 0 1 0 1 0 0
0 0 1 1 1 0 0
0 0 0 0 0 0 0
RES: 6
row: 7, col:7
0 0 0 0 0 0 0
0 0 1 0 0 0 0
0 0 1 0 0 0 0
0 0 1 1 0 0 0
0 0 1 0 0 0 0
0 0 1 0 0 0 0
0 0 0 0 0 0 0
RES: 1
代码
#include <iostream>
#include <map>
const static std::map<int, int> HOLE_COUNT_2_NUMS = {
std::make_pair(0, 1),
std::make_pair(1, 6),
std::make_pair(2, 8)
};
int _cal_num_type(const bool* content, int row, int col) {
int hole_count = 0;
for (int i = 0; i < row; ++i) {
int left_index = -1;
int start_index = i * col;
for (int j = 0; j < col; ++j) {
const int cur_index = start_index + j;
if (left_index == -1) {
if (content[cur_index]) {
left_index = cur_index;
}
}
else {
if (content[cur_index]) {
if (cur_index - left_index > 1) {
++hole_count;
}
left_index = cur_index;
}
}
}
}
const auto res = HOLE_COUNT_2_NUMS.find(hole_count);
if (res != HOLE_COUNT_2_NUMS.end()) {
return res->second;
}
return -1;
}
void output_res(const bool* content, const int row, const int col, const int res) {
std::cout << "row: " << row << ", col:" << col<< std::endl;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
std::cout << " " << content[i * col + j];
}
std::cout << std::endl;
}
std::cout << "RES: " << res<< std::endl;
}
void cal_num_type() {
{
const int n = 7, m = 7;
const bool content[] = {
0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0,
0, 0, 1, 0, 1, 0, 0,
0, 0, 1, 1, 1, 0, 0,
0, 0, 1, 0, 1, 0, 0,
0, 0, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0,
};
const int res = _cal_num_type(content, n, m);
output_res(content, m, n, res);
}
{
const int n = 7, m = 7;
const bool content[] = {
0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0,
0, 0, 1, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0,
0, 0, 1, 0, 1, 0, 0,
0, 0, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0,
};
const int res = _cal_num_type(content, n, m);
output_res(content, m, n, res);
}
{
const int n = 7, m = 7;
const bool content[] = {
0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
};
const int res = _cal_num_type(content, n, m);
output_res(content, m, n, res);
}
}