File tree Expand file tree Collapse file tree 1 file changed +115
-0
lines changed Expand file tree Collapse file tree 1 file changed +115
-0
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Linq ;
3
+ using System . Runtime . InteropServices ;
4
+
5
+ class Test
6
+ {
7
+ static void Main ( string [ ] args )
8
+ {
9
+ /*
10
+ if x is a odd number then f(x) = (-1) * ( 2 ^ x ) ,
11
+ Otherwise f(x) = 2 ^ x .
12
+ */
13
+
14
+ var N = Console . ReadLine ( ) . Split ( ' ' ) . Select ( int . Parse ) . ToArray ( ) ;
15
+
16
+ var a = TraverseSpiral ( N [ 0 ] , N [ 0 ] ) ;
17
+
18
+ for ( int i = 1 ; i <= N [ 1 ] ; i ++ )
19
+ {
20
+ int p = int . Parse ( Console . ReadLine ( ) ) ;
21
+ Console . WriteLine ( "{0}, {1}" , a [ p ] . _y + 1 , a [ p ] . _x + 1 ) ;
22
+
23
+ }
24
+
25
+ }
26
+
27
+
28
+ private static Point [ ] TraverseSpiral ( int width , int height )
29
+ {
30
+ int numElements = width * height + 1 ;
31
+ Point [ ] points = new Point [ numElements ] ;
32
+
33
+ int x = 0 ;
34
+ int y = 0 ;
35
+ int dx = 1 ;
36
+ int dy = 0 ;
37
+ int xLimit = width - 0 ;
38
+ int yLimit = height - 1 ;
39
+ int counter = 0 ;
40
+
41
+ int currentLength = 1 ;
42
+ while ( counter < numElements )
43
+ {
44
+ points [ counter ] = new Point ( x , y ) ;
45
+
46
+ x += dx ;
47
+ y += dy ;
48
+
49
+ currentLength ++ ;
50
+ if ( dx > 0 )
51
+ {
52
+ if ( currentLength >= xLimit )
53
+ {
54
+ dx = 0 ;
55
+ dy = 1 ;
56
+ xLimit -- ;
57
+ currentLength = 0 ;
58
+ }
59
+ }
60
+ else if ( dy > 0 )
61
+ {
62
+ if ( currentLength >= yLimit )
63
+ {
64
+ dx = - 1 ;
65
+ dy = 0 ;
66
+ yLimit -- ;
67
+ currentLength = 0 ;
68
+ }
69
+ }
70
+ else if ( dx < 0 )
71
+ {
72
+ if ( currentLength >= xLimit )
73
+ {
74
+ dx = 0 ;
75
+ dy = - 1 ;
76
+ xLimit -- ;
77
+ currentLength = 0 ;
78
+ }
79
+ }
80
+ else if ( dy < 0 )
81
+ {
82
+ if ( currentLength >= yLimit )
83
+ {
84
+ dx = 1 ;
85
+ dy = 0 ;
86
+ yLimit -- ;
87
+ currentLength = 0 ;
88
+ }
89
+ }
90
+
91
+ counter ++ ;
92
+ }
93
+
94
+ Array . Reverse ( points ) ;
95
+ return points ;
96
+ }
97
+
98
+ }
99
+
100
+ public class Point
101
+ {
102
+ public int _x ;
103
+
104
+ public int _y ;
105
+ public Point ( int x , int y )
106
+ {
107
+ _x = x ;
108
+ _y = y ;
109
+ }
110
+
111
+
112
+ }
113
+
114
+
115
+
You can’t perform that action at this time.
0 commit comments