int main () {
int x=5; // change this for the block size
int d[x][x];
int start = 1; // change this for starting number
int count = start;
for (int i=0; i<x; i++) {
// fill the top section
for (int j=i; j<x-i; j++)
d[i][j] = count++;
count--;
// fill the right side
for (int j=i; j<x-i; j++)
d[j][x-i-1] = count++;
count--;
// fill the bottom side
for (int j=x-i-1; j>=i; j--)
d[x-i-1][j] = count++;
count--;
// fill the left side
for (int j=x-i-1; j>i; j--)
d[j][i] = count++;
if (count > x*x+start)
break;
}
for (int i=0; i<x; i++) {
for (int j=0; j<x; j++) {
cout << d[i][j] << " ";
}
cout << endl;
}
return 0;
}
It's not optimized, tough..
Edit: add some comment, reverse variable i and j
I was just excited about this question. Usually, I got the the triangle stuff and I don't have any idea on any simple-complex advancement. Your question was interesting enough to make me think beside my other codes.
This should be my thank.
This is an another problem chu121su12, which i have sent in this forum.....
suppose input is a triangle as follows-
2
3 4
6 1 9
3 4 8 3
7 2 4 3 1
i am to choose a path from top to botttom so that the sum of the numbers in this path should be maximum. i can move only left or right of the number when coming to the next row. as i am at then i can move only to 3 or 4. if i move to 4 then in next chance i can move to 1 or 9. in this way i will go to bottom. this program only should answer the maximum sum.