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

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

itsa66 4羅馬數字系統

1.將羅馬數字存到陣列,區分五跟十的倍數。
2.以五為分界,前四做前輸出,後四做後輸出。
3.以迴圈做對應輸出。
注意:陣列存的方式會改變輸出時的陣列編號。

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

itsa66 3字串切割

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

itsa66 2未知數

1.無條件捨去,引用<cmath>的floor。
2.字串轉整數或小數,要先轉字元, "c_str()"可以將字串轉字元,但要看情況使用。
例:1111 2222 333
s[   0     1    2  ]
s[0][1] = 1  這時有[][]就不能用s[0][1].c_str() 要不然會錯喔!
注意:可以為小數,運算要正確。

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

itsa66 1過半元素

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

int main()
{
int arr[30];
int N, count, a;
string str;
while (getline(cin, str))
{
a = 0;
N = 0;
stringstream ss(str);
while (ss >> arr[N])
N++;
N--;
for (int i = 0; i < N; i++)
{
count = 0;
for (int j = 0; j < N; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count > N / 2)
{
a = 1;
cout << arr[i] << endl;
break;
}
}
if (count <= N / 2 && a == 0)
cout << "NO" << endl;
}
return 0;
}

2018年9月8日 星期六

itsa [C_AR030-中]文字頻率分析

註: tUp英文大寫次數 tLow英文小寫次數 tw字次數

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

int main()
{
int c, w, count = 0, x = 0, y = 0, z = 0, g = 0, a = 0, b = 0, check = 0;
int tUp[26] = { 0 }, tLow[26] = { 0 }, tw[100] = { 0 };
string prefix, suffix, s, str[100], pre[100], suf[100], wo[100];
cin >> prefix;
cin >> suffix;
cin >> c;
cin >> w;
getline(cin, s);
getline(cin, s);
stringstream ss(s);
while (ss >> str[x])
x++;
for (int i = 0; i < x; i++)
{
check = 0;
count = 0;
if (str[i].length() >= prefix.length())
{
for (int j = 0; j < prefix.length(); j++)
{
if (str[i][j] == prefix[j])
count++;
else
break;
}
if (count == prefix.length())
{
for (int k = 0; k < y; k++)
if (pre[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
pre[y++] = str[i];
}
}
}
for (int i = 0; i < x; i++)
{
a = 0;
count = 0;
check = 0;
if (str[i].length() >= suffix.length())
{
for (int j = str[i].length() - suffix.length(); j < str[i].length(); j++)
{
if (str[i][j] == suffix[a++])
count++;
else
break;
}
if (count == suffix.length())
{
for (int k = 0; k < z; k++)
if (suf[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
suf[z++] = str[i];
}
}
}
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < s.length(); j++)
{
if (s[i] == s[j])
{
if (s[i] >= 97 && s[i] <= 122)
tLow[s[i] - 'a']++;
else if (s[i] >= 65 && s[i] <= 90)
tUp[s[i] - 'A']++;
s[j] = ' ';
}
}
}
for (int i = 0; i < x; i++)
{
b = 0;
check = 0;
for (int j = 0; j < x; j++)
{
count = 0;
if (str[i].length() == str[j].length())
{
for (int k = 0; k < str[i].length(); k++)
{
if (str[i][k] == str[j][k])
count++;
else
break;
}
}
if (count == str[i].length())
b++;
}
if (b >= w)
{
for (int k = 0; k < g; k++)
if (wo[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
{
wo[g] = str[i];
tw[g] = b;
g++;
}
}
}
for (int i = 0; i < g - 1; i++)
{
for (int j = i + 1; j < g; j++)
{
if (wo[i].compare(wo[j]) == 1)
{
string tmp = wo[i];
wo[i] = wo[j];
wo[j] = tmp;
int temp = tw[i];
tw[i] = tw[j];
tw[j] = temp;
}
}
}
cout << "prefix of " << prefix << ":" << endl;
for (int i = 0; i < y; i++)
cout << pre[i] << endl;
cout << "suffix of " << suffix << ":" << endl;
for (int i = 0; i < z; i++)
cout << suf[i] << endl;
cout << "character frequency over " << c << ":" << endl;
for (int i = 0; i < 26; i++)
{
if (tUp[i] >= c)
cout << (char)(i + 'A') << ',' << tUp[i] << endl;
}
for (int i = 0; i < 26; i++)
{
if (tLow[i] >= c)
cout << (char)(i + 'a') << ',' << tLow[i] << endl;
}
cout << "word frequency over " << w << ":" << endl;
for (int i = 0; i < g; i++)
cout << wo[i] << ',' << tw[i] << endl;
return 0;
}

2018年9月2日 星期日

itsa [C_AR029-難]文字編碼

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

int main()
{
int m = 0, x = 0, check = 0;
char arr[16][16];
for (int i = 0; i < 16; i++)
for (int j = 0; j < 16; j++)
arr[i][j] = ' ';
string s;
getline(cin, s);
for (int i = 1; i * i <= s.length(); i++)
m = i;
if (m * m < s.length())
m++;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
arr[i][j] = s[x++];
if (x == s.length())
break;
}
if (x == s.length())
break;
}
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
cout << arr[j][i];
cout << endl;
return 0;
}