This is the problem. https://dunjudge.me/analysis/problems/838/ . Essentially its to create a square with layers that keep increasing like so.
1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 4 3 2 1
1 2 3 3 3 2 1
1 2 2 2 2 2 1
1 1 1 1 1 1 1
I have already figured out the logic and have implemented it myself. I can replicate the testcase and tried to work out some examples.
#include <bits/stdc++.h>
using namespace std;
int f(int n)
{
return -1 + 2 * n;
}
int main()
{
int n = 0;
cin >> n;
for (int i = 1; i < n + 1; i++)
{
for (int k = 1; k < i; k++)
{
cout << k << ' ';
}
if (i == 1)
{
for (int j = 0; j < 2 * n - 1; j++)
{
cout << i << ' ';
}
}
else if (i > 1)
{
for (int j = 0; j < 2 * n - f(i); j++)
{
cout << i << ' ';
}
}
for (int k = i - 1; k > 0; k--)
{
if (k == i - 1) {
cout << i << ' ';
} else {
cout << ' ' << i;
}
}
cout << endl;
}
for (int w = n - 1; w > 0; w--)
{
for (int k = 1; k < w; k++)
{
cout << k << ' ';
}
if (w == 1)
{
for (int s = 2 * n - 1; s > 0; s--)
{
cout << w << ' ';
}
}
else if (w > 1)
{
for (int j = 0; j < 2 * n - f(w); j++)
{
cout << w << ' ';
}
}
for (int k = w - 1; k > 0; k--)
{
if (k == w - 1) {
cout << w << ' ';
} else {
cout << ' ' << w;
}
}
cout << endl;
}
return 0;
}
However it is always giving me a so called presentation error whilst I can match the sample case. Hence I am not sure how to fix my answer.
This might be a simpler way to go about this
void printSq(const int N) {
for (int r = 0; r < N; ++r)
for (int c = 0; c < N; ++c) {
std::cout << std::min({r+1, c+1, N-r, N-c}) << " \n"[c == N-1];
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.