Мы можем наблюдать следующее.
До середины столбца значения являются значениями столбца, но не выше значений строки.
Над средним столбцом номера столбцов почитали. Что это значит? Если у вас есть 10 столбцов и вы хотите перевернуть их, вам нужно вычесть номер столбца из ширины 10. Так что
10-6 = 4
10-7 = 3
10-8 = 2
10-9 = 1
10-10 = 0;
То же самое относится и к строкам.
- Итак, для обычных столбцов (<среднее значение). номер столбца </li>
- Для номеров столбцов выше середины обратный номер столбца (см. выше)
- Но не больше, чем значение ограниченной строки
- Рассчитывается значение ограниченной строки согласно приведенной выше схеме
В моей демонстрационной программе я использую std::vector<std::vector<int>>
для хранения значений. Расчетные значения начинаются с 0. Поэтому я добавляю 1 к каждому значению, добавляем конец.
Пожалуйста, см .:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cmath>
void showRectangle(const size_t height, const size_t width) {
std::vector lines{ height, std::vector(width, 0U) };
// Calculate the vales in the middle
const size_t midHeight{ static_cast<size_t>(std::ceil(height / 2.0)) };
const size_t midWidth{ static_cast<size_t>(std::ceil(width / 2.0)) };
// Iterate over all rows and columns
for (size_t row{ 0U }; row < height; ++row) {
for (size_t column{ 0U }; column < width; ++column) {
// Calculate the vaule for row and column
lines[row][column] = (std::min((column >= midWidth) ? (width - column-1) : column,
(row >= midHeight) ? (height - row - 1) : row)) +1 ;
}
}
// Show output
for (const std::vector<size_t>& vi : lines) {
std::copy(vi.begin(), vi.end(), std::ostream_iterator<size_t>(std::cout));
std::cout << "\n";
}
}
int main() {
// Explain the user that he has to enter the height in rows
std::cout << "\nEnter the height of the rectangle in rows: ";
// Get the height. And check, if input worked
if (size_t height{}; std::cin >> height) {
// Explain the user that he has to enter the width in columns
std::cout << "Enter the width of the rectangle in columns: ";
if (size_t width{}; std::cin >> width) {
// SHow result
std::cout << "\n\nResulting rectangle:\n\n";
showRectangle(height, width);
}
else {
std::cerr << "\n*** Error: wrong input for width\n";
}
}
else {
std::cerr << "\n*** Error: wrong input for height\n";
}
}