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;
}

2018年11月21日 星期三

itsa68 4算數移位相加

移位 例:000123 => 123000 可看成 (向左移*10 向右移*0.1)
原 1*10^2+2*10^1+3*10^0
果 1*10^5+2*10^4+3*10^3 = (1*10^2+2*10^1+3*10^0)*10^3
現在有一個數n,4位數,向右移3次後相加,可寫成
S = n+n*0.1+n*0.01+n*0.001
= n*(1+0.1+0.01+0.001)
= n*1.111
n = S / 1.111
算出來會是除了個位數的其他位數
因為有誤差所以要加回去(進位等)
跑0~20,加完後,判斷移位相加後的結果與S是否相同,即為所求a0


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

bool check(unsigned long long n, unsigned long long s, unsigned long long len)
{
 unsigned long long sum = 0;
 for (unsigned long long i = 0; i < len; i++)
 {
  sum += n;
  n /= 10;
 }
 if (sum - s == 0)
  return true;
 return false;
}

int main()
{
 int found;
 unsigned long long n, len, res;
 double div, fr;
 while (cin >> n)
 {
  len = (unsigned long long) (log10(n)) + 1;
  div = 1;
  fr = 0.1;
  found = 0;
  for (int i = 2; i <= len; i++)
  {
   div += fr;
   fr *= 0.1;
  }
  res = (unsigned long long)(n / div);
  for (int j = 0; j <= 20; j++)
  {
   if (check(res + j, n, len))
   {
    cout << res + j << endl;
    found = 1;
    break;
   }
  }
  if (found == 0)
   cout << -1 << endl;
 }
 return 0;
}

2018年11月5日 星期一

itsa [C_SL27-中]狀態機 (State Machine)

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

int main()
{
 string str;
 char arr[30][30];
 int n, s, x, y;
 while (cin >> n >> s)
 {
  cin >> str;
  for (int i = 1; i <= n; i++)
   for (int j = 1; j <= n; j++)
    cin >> arr[i][j];
  x = 0;
  y = s;
  cout << s;
  for (int i = 0; i < str.length(); i++)
  {
   for (int j = 1; j <= n; j++)
   {
    if (arr[y][j] == str[i])
    {
     cout << j;
     y = j;
     break;
    }
   }
  }
  cout << endl;
 }
 return 0;
}

itsa67 4狀態機

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

int main()
{
 string str;
 char arr[30][30];
 int n, s, x, y;
 while (cin >> n >> s)
 {
  cin >> str;
  for (int i = 1; i <= n; i++)
   for (int j = 1; j <= n; j++)
    cin >> arr[i][j];
  x = 0;
  y = s;
  cout << s;
  for (int i = 0; i < str.length(); i++)
  {
   for (int j = 1; j <= n; j++)
   {
    if (arr[y][j] == str[i])
    {
     cout << j;
     y = j;
     break;
    }
   }
  }
  cout << endl;
 }
 return 0;
}

2018年10月21日 星期日

itsa [C_GD03-易]線段切割

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;

int main()
{
 int n, sum, max;
 while (cin >> n)
 {
  sum = 0;
  max = 0;
  for (int i = 1; i <= n; i++)
  {
   if (sum + i > n)
    break;
   sum += i;
   max++;
  }
  cout << max << endl;
 }
 return 0;
}

itsa67 2線段切割

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;

int main()
{
 int n, sum, max;
 while (cin >> n)
 {
  sum = 0;
  max = 0;
  for (int i = 1; i <= n; i++)
  {
   if (sum + i > n)
    break;
   sum += i;
   max++;
  }
  cout << max << endl;
 }
 return 0;
}