For n = 18 my code takes more than 0.5s on a 1GHz machine. I think this is due to the fact that I am using a recursive function, yet I don't really knwo how to optimise this code because it actually just "prints" numbers... Hence maybe the problem comes from the fact that I am using recursive function.
Here is my code :
#include<iostream>
void singleSquareRemove (int s)
{
if (s == 1)
{
std::cout << 1 << std::endl;
return;
}
else
{
for (int i = s-1; i >=1; --i)
singleSquareRemove(i);
std::cout << s << std::endl;
}
}
void whenSquareisFull (int v)
{
if (v == 1)
{
std::cout << 1 << std::endl;
return;
}
else if (v == 2)
{
std::cout << 2 << std::endl;
return;
}
else if (v == 3)
{
std::cout << 1 << std::endl;
std::cout << 3 << std::endl;
return;
}
else
{
whenSquareisFull(v-2);
for (int i = v-3; i > 0; --i)
singleSquareRemove(i);
std::cout << v << std::endl;
}
}
int main()
{
unsigned int n {0};
std::cin >> n;
whenSquareisFull(n);
for (int i = n-1; i > 0; --i)
{
singleSquareRemove(i);
}
}