2019年9月4日 星期三

itsa [C_AR031-中]一維矩陣表示二維平面空間

 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
42
#include<iostream>
#include<string>
using namespace std;

int main()
{
 string sarr[25][25];
 int f[8][2] = { {-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1} };
 int N, index, x, y;
 cin >> N;
 cin >> index;
 for (int i = 0; i < 25; i++)
  for (int j = 0; j < 25; j++)
   sarr[i][j] = "";
 for (int i = 1; i <= N; i++)
 {
  for (int j = 1; j <= N; j++)
  {
   sarr[i][j] = 'A' + i - 1;
   sarr[i][j] += j + '0';
  }
 }
 x = index / N + 1;
 y = index % N + 1;
 for (int i = 0; i < 8; i++)
 {
  if (sarr[x + f[i][0]][y + f[i][1]] != "")
   cout << (x + f[i][0] - 1) * N + (y + f[i][1]) - 1 << " ";
  else
   cout << "-1 ";
 }
 cout << endl;
 for (int i = 0; i < 8; i++)
 {
  if (sarr[x + f[i][0]][y + f[i][1]] != "")
   cout << sarr[x + f[i][0]][y + f[i][1]] << " ";
  else
   cout << "-1 ";
 }
 cout << endl;
 return 0;
}

2019年8月31日 星期六

itsa [C_AR220-易]奇數魔方陣

 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
using namespace std;

int main()
{
 int n, m, i, j, ti, tj, num, rsum, csum;
 int arr1[10], arr[100][100];
 int f[2][2] = { {-1,1},{1,0} };
 cin >> n;
 for (int k = 0; k < n; k++)
  cin >> arr1[k];
 for (int k = 0; k < n; k++)
 {
  m = arr1[k];
  for (int a = 0; a < m; a++)
   for (int b = 0; b < m; b++)
    arr[a][b] = 0;
  i = 0;
  j = m / 2;
  num = 2;
  rsum = 0;
  csum = 0;
  arr[i][j] = 1;
  for (int a = 1; a < m*m; a++)
  {
   ti = i + f[0][0];
   tj = j + f[0][1];
   if ((ti < 0 && tj > m - 1) || (ti >= 0 && tj <= m - 1 && arr[ti][tj] != 0))
   {
    arr[i + f[1][0]][j + f[1][1]] = num;
    i += f[1][0];
    j += f[1][1];
   }
   else if (ti < 0 && tj <= m - 1)
   {
    arr[m - 1][tj] = num;
    i = m - 1;
    j = tj;
   }
   else if (ti >= 0 && tj > m - 1)
   {
    arr[ti][0] = num;
    i = ti;
    j = 0;
   }
   else
   {
    arr[ti][tj] = num;
    i = ti;
    j = tj;
   }
   num++;
  }
  for (int a = 0; a < arr1[k]; a++)
  {
   for (int b = 0; b < arr1[k]; b++)
   {
    if (b > 0)
     cout << " ";
    cout << arr[a][b];
   }
   cout << endl;
  }
  if (k != n - 1)
   cout << '-' << endl;
 }
 return 0;
}