0

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.

  • Are you supposed to have a space at the end of each line? Probably not, so you'll have to figure out a way around that. Perhaps print the space before each value except the first on each line. – Retired Ninja Nov 19 '20 at 05:09
  • Oh thats a good idea i'll give it a shot thanks –  Nov 19 '20 at 05:11
  • 2
    I hope you're aware of [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Nov 19 '20 at 05:14
  • Yeah I am aware, just decided to use it for strictly competitions. In project work I take note of that. Thanks for the heads up still. –  Nov 19 '20 at 05:45

1 Answers1

1

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];
      }
}
Kostas
  • 4,061
  • 1
  • 14
  • 32