spiral

if input : 5
output :
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9


1
2
3
4
5
6
7
8
9
10
#include <iostream>
#define __ 1
#define ___ 0
#define ____ int
#define _____ cin
#define ______ cout<<
#define _-_ usin
#define __-__ g namespace
#define ___-___ std
_-____-_______-___;int main(){____ _;_____>>_;if(_==5?__:___)______"1 2 3 4 5\n16 17 18 19 6\n15 24 25 20 7\n14 23 22 21 8\n13 12 11 10 9\n";return 0;}


This probably won't work, so debug it first thanks.
Last edited on
I don't think aakashjohari meant that, but wants a way to display a numeric spiral with any input, that will work just for fixed input.
Here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
Last edited on
thanx
You're welcome.
I've tried to remove this line (29 & 30) and it still work:
1
2
if (count > x*x+start)
    break;


And it may be better if you use dynamic array.
thank you chu121su12......
i hope you will help me in future in this way
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.

Thanx!!!!!!
Topic archived. No new replies allowed.