2018年10月8日 星期一

C++魔方陣

魔方陣

成績: 20 / 倒扣: 0.1
魔方陣是大家所熟知的數學問題,其規則是方陣中每一行、每一列以及對角線三個數字的總和都要相等。

數字 1 填入第一列中間行的位置。

輸入說明:

第一列的數字 n 代表總共有 n 個題組

每個題組的第一列的數字 m 代表 m x m 的魔方陣, m >= 3,奇數。

第二列數字 r 代表第一列到第m列中的第 r 列,1<=r<=m。

第二列數字 c 代表第一行到第m行中的第 c 列行,1<=c<=m。

輸出說明:

每一題組輸出兩列數字,

第 r 列的 d1-d2+d3-d4+...+dm 的值和第 c 行 d1-d2+d3-d4+...+dm 的值。

範例:

輸入

1

3

2

3

輸出

5

1

奇數的魔方陣的解法為:
1.數字 1 填入第一列中間行的位置
2.以1為起點,往右上方延伸
3.超出列範圍的(<0),則填入該行最後列
4.超出行範圍的(>m),則填入該列第一行
5.遇到不為零或是超出列(<0)與行(>m),則填入目前的數下面一格
6.沒有上述狀況,則填入右上方格

3X3為

8 1 6
3 5 7
4 9 2


 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
69
70
71
#include<iostream>
using namespace std;

int main()
{
 int n, m, r, c, i, j, ti, tj, num, rsum, csum;
 int arr[100][100];
 int f[2][2] = { {-1,1},{1,0} };
 cin >> n;
 for (int k = 0; k < n; k++)
 {
  cin >> m;
  cin >> r;
  cin >> c;
  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 b = 0; b < m; b++)
  {
   if (b % 2 == 0)
   {
    rsum += arr[r - 1][b];
    csum += arr[b][c - 1];
   }
   else
   {
    rsum -= arr[r - 1][b];
    csum -= arr[b][c - 1];
   }
  }
  cout << rsum << endl;
  cout << csum << endl;
 }
 return 0;
}

itsa [C_AR62-中]矩陣相乘

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

int main()
{
 int m, n, t;
 int a[100][100], b[100][100], c[100][100];
 cin >> m >> n;
 for (int i = 0; i < m; i++)
  for (int j = 0; j < n; j++)
   cin >> a[i][j];
 cin >> n >> t;
 for (int i = 0; i < n; i++)
  for (int j = 0; j < t; j++)
   cin >> b[i][j];
 for (int i = 0; i < m; i++)
 {
  for (int j = 0; j < t; j++)
  {
   c[i][j] = 0;
   for (int k = 0; k < n; k++)
    c[i][j] = c[i][j] + a[i][k] * b[k][j];
  }
 }
 for (int i = 0; i < m; i++)
 {
  for (int j = 0; j < t; j++)
  {
   if (j > 0)
    cout << " ";
   cout << c[i][j];
  }
  cout << endl;
 }
 return 0;
}

2018年9月15日 星期六

itsa [DP40-中]Find the Sequence Pattern

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

int main()
{
 string s1, s2, lstr, sstr;
 string s[100];
 int a, b, x;
 while (getline(cin, s1))
 {
  bool ischeck = false;
  b = 0;
  x = 0;
  getline(cin, s2);
  lstr = s1;
  sstr = s2;
  if (s1.length() < s2.length())
  {
   lstr = s2;
   sstr = s1;
  }
  for (int j = sstr.length(); j > 1; j--)
  {
   a = 0;
   if (j != b && ischeck)
    break;
   for (int i = 0; i <= sstr.length() - j; i++)
   {
    if (string::npos != lstr.find(sstr.substr(i, j)))
    {
     for (int k = 0; k < x; k++)
      if (s[k].compare(sstr.substr(i, j)) == 0)
       a = 1;
     if (a == 0)
     {
      s[x++] = sstr.substr(i, j);
      b = j;
      ischeck = true;
     }
    }
   }
  }
  if (ischeck)
  {
   if (x > 1)
   {
    for (int i = 0; i < x - 1; i++)
    {
     for (int j = i + 1; j < x; j++)
     {
      if (s[i].compare(s[j]) > 0)
      {
       string tmp = s[i];
       s[i] = s[j];
       s[j] = tmp;
      }
     }
    }
   }
   for (int i = 0; i < x; i++)
    cout << s[i] << endl;
  }
  else
   cout << "No common sequence!" << endl;
 }
 return 0;
}

itsa66 5Find the Sequence Pattern

1.找最長共同子字串,用長度小的找長度長的,長度2以上
2.最長共同子字串可以有很多個,要去掉重複的
3.輸出要按字母順序排列

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

int main()
{
 string s1, s2, lstr, sstr;
 string s[100];
 int a, b, x;
 while (getline(cin, s1))
 {
  bool ischeck = false;
  b = 0;
  x = 0;
  getline(cin, s2);
  lstr = s1;
  sstr = s2;
  if (s1.length() < s2.length())
  {
   lstr = s2;
   sstr = s1;
  }
  for (int j = sstr.length(); j > 1; j--)
  {
   a = 0;
   if (j != b && ischeck)
    break;
   for (int i = 0; i <= sstr.length() - j; i++)
   {
    if (string::npos != lstr.find(sstr.substr(i, j)))
    {
     for (int k = 0; k < x; k++)
      if (s[k].compare(sstr.substr(i, j)) == 0)
       a = 1;
     if (a == 0)
     {
      s[x++] = sstr.substr(i, j);
      b = j;
      ischeck = true;
     }
    }
   }
  }
  if (ischeck)
  {
   if (x > 1)
   {
    for (int i = 0; i < x - 1; i++)
    {
     for (int j = i + 1; j < x; j++)
     {
      if (s[i].compare(s[j]) > 0)
      {
       string tmp = s[i];
       s[i] = s[j];
       s[j] = tmp;
      }
     }
    }
   }
   for (int i = 0; i < x; i++)
    cout << s[i] << endl;
  }
  else
   cout << "No common sequence!" << endl;
 }
 return 0;
}

2018年9月13日 星期四

itsa [C_ST125-易]字串切割

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

void replaceD(string &s)
{
 int i = 0;
 while (i < s.length())
 {
  if (s[i] == ':' || s[i] == ',' || s[i] == ';')
   s[i] = ' ';
  i++;
 }
}

int main()
{
 int N, x = 0;
 string s, str[100];
 cin >> N;
 getline(cin, s);
 for (int i = 0; i < N; i++)
 {
  x = 0;
  getline(cin, s);
  cout << "Original string: " << s << endl;
  replaceD(s);
  stringstream ss(s);
  while (ss >> str[x])
   x++;
  cout << "Tokens found:\n";
  for (int j = 0; j < x; j++)
   cout << str[j] << endl;
 }
 return 0;
}

itsa [C_MM359-易]未知數

#include<iostream>
#include<string>
#include<cmath>
#include<cstdlib>
#include<iomanip>
using namespace std;

int main()
{
double ans;
string s1, s2, s3, s4, s5;
while (cin >> s1 >> s2 >> s3 >> s4 >> s5)
{
ans = 0;
if (s1 == "x")
{
if (s2 == "+")
ans = atof(s5.c_str()) - atof(s3.c_str());
else if (s2 == "-")
ans = atof(s5.c_str()) + atof(s3.c_str());
else if (s2 == "*")
ans = atof(s5.c_str()) / atof(s3.c_str());
else
ans = atof(s5.c_str()) * atof(s3.c_str());
}
else if (s3 == "x")
{
if (s2 == "+")
ans = atof(s5.c_str()) - atof(s1.c_str());
else if (s2 == "-")
ans = atof(s1.c_str()) - atof(s5.c_str());
else if (s2 == "*")
ans = atof(s5.c_str()) / atof(s1.c_str());
else
ans = atof(s1.c_str()) / atof(s5.c_str());
}
else
{
if (s2 == "+")
ans = atof(s1.c_str()) + atof(s3.c_str());
else if (s2 == "-")
ans = atof(s1.c_str()) - atof(s3.c_str());
else if (s2 == "*")
ans = atof(s1.c_str()) * atof(s3.c_str());
else
ans = atof(s1.c_str()) / atof(s3.c_str());
}
ans = floor(ans * 10) / 10;
cout << fixed << setprecision(1) << ans << endl;
}
return 0;
}

itsa [C_AR69-中]羅馬數字系統

#include<iostream>
#include<string>
using namespace std;

int main()
{
string one[4] = { "I","X","C","M" };
string five[3] = { "V","L","D" };
string s;
int n;
cin >> n;
getline(cin, s);
for (int k = 0; k < n; k++)
{
getline(cin, s);
for (int i = 0; i < s.length(); i++)
{
int subNum = s[i] - '0';
int num = 0;
if (subNum < 5)
num = subNum % 4;
else if (subNum > 5)
num = (subNum - 5) % 4;
if (subNum >= 1 && subNum <= 4)
{
if (num == 0)
{
cout << one[s.length() - i - 1];
cout << five[s.length() - i - 1];
}
else
{
for (int j = 0; j < num; j++)
cout << one[s.length() - i - 1];
}
}
else if (subNum == 5)
cout << five[s.length() - i - 1];
else if (subNum >= 6 && subNum <= 9)
{
if (num == 0)
{
cout << one[s.length() - i - 1];
cout << one[s.length() - i];
}
else
{
cout << five[s.length() - i - 1];
for (int j = 0; j < num; j++)
cout << one[s.length() - i - 1];
}
}
}
cout << endl;
}
return 0;
}