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

2018年8月23日 星期四

itsa [RU22-中]Dice throwing

#include<iostream>
using namespace std;

long long gcd(long long x, long long y)
{
if (x == 0)
return y;
if (y == 0)
return x;
while (y != 0)
{
long long tmp = y;
y = x % y;
x = tmp;
}
return x;
}

int main()
{
int N, X;
long long a, b, GCD;
long long diceP[11][71], total[11] = { 0 };
for (int i = 0; i < 11; i++)
for (int j = 0; j < 71; j++)
diceP[i][j] = 0;
diceP[0][0] = 1;
total[0] = 1;
for (int i = 1; i <= 10; i++)
{
for (int j = 6 * i; j >= i; j--)
{
for (int k = 1; k <= 6 && j - k >= 0; k++)
diceP[i][j] += diceP[i - 1][j - k];
}
total[i] = total[i - 1] * 6;
}
for (int i = 1; i <= 10; i++)
{
for (int j = 6 * i; j >= 0; j--)
diceP[i][j] += diceP[i][j + 1];
}
while (cin >> N >> X)
{
if (N == 0 && X == 0)
break;
a = diceP[N][X];
b = total[N];
GCD = gcd(a, b);
a /= GCD;
b /= GCD;
if (b == 1)
cout << a << endl;
else
cout << a << "/" << b << endl;
}
return 0;
}

2018年8月21日 星期二

itsa [C_MM170-易]計算Pi

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

int main()
{
int n;
double sum, k;
while (true)
{
sum = 0, k = 1;
cin >> n;
if (n == 0)
break;
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
sum += 4 / k;
else
sum -= 4 / k;
k += 2;
}
cout << fixed << setprecision(5) << sum << endl;
}
return 0;
}

itsa [C_DT44-中]圓桌報數遊戲

#include<iostream>
using namespace std;

int main()
{
int n, a, b, count, num, out, x, ans;
while (cin >> n >> a >> b)
{
out = 0;
x = a;
int *people = new int[n];
for (int i = 0; i < n; i++)
people[i] = 1;
if (x != 0)
num = x - 2;
else
num = x - 1;
while (out != n)
{
for (int i = 0; i < n; i++)
{
ans = 0;
if (out > 0)
num = x - 1;
count = 0;
while (count != b + 1)
{
if (out == n - 1 && ans == 1)
break;
if (num < n - 1)
num++;
else
num = 0;
if (people[num] != 0)
count++;
if (count == b && ans == 0)
{
if (out > 0)
cout << " ";
cout << num + 1;
people[num] = 0;
ans = 1;
}
}
x = num;
out++;
}
}
cout << endl;
}
return 0;
}

itsa [C_AR40-易]螺旋矩陣

#include<iostream>
using namespace std;

int main()
{
int size, direction, x, y, tx, ty, num, finish;
char c;
int arr[32][32];
int cw[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
int cc[4][2] = { { 1,0 },{ 0,1 },{ -1,0 },{ 0,-1 } };
while (cin >> size >> c >> direction)
{
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
arr[i][j] = 0;
x = y = tx = ty = finish = 0;
num = 2;
arr[0][0] = 1;
if (direction == 1)
{
for (int i = 0; i <= size / 2; i++)
{
for (int j = 0; j < 4; j++)
{
if (!finish)
{
for (int k = 1; k < size; k++)
{
if (arr[x + cw[j][0] * k][y + cw[j][1] * k] == 0)
{
arr[x + cw[j][0] * k][y + cw[j][1] * k] = num;
tx = x + cw[j][0] * k;
ty = y + cw[j][1] * k;
if (num == size * size)
{
finish = 1;
break;
}
num++;
}
else
break;
}
}
x = tx;
y = ty;
}
}
}
else
{
for (int i = 0; i <= size / 2; i++)
{
for (int j = 0; j < 4; j++)
{
if (!finish)
{
for (int k = 1; k < size; k++)
{
if (arr[x + cc[j][0] * k][y + cc[j][1] * k] == 0)
{
arr[x + cc[j][0] * k][y + cc[j][1] * k] = num;
tx = x + cc[j][0] * k;
ty = y + cc[j][1] * k;
if (num == size * size)
{
finish = 1;
break;
}
num++;
}
else
break;
}
}
x = tx;
y = ty;
}
}
}
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (j > 0)
cout << ",";
if (arr[i][j] < 10)
cout << "00" << arr[i][j];
else if (arr[i][j] < 100)
cout << "0" << arr[i][j];
else
cout << arr[i][j];
}
cout << endl;
}
}
return 0;
}

itsa65 4圓桌報數遊戲

#include<iostream>
using namespace std;

int main()
{
int n, a, b, count, num, out, x, ans;
while (cin >> n >> a >> b)
{
out = 0;
x = a;
int *people = new int[n];
for (int i = 0; i < n; i++)
people[i] = 1;
if (x != 0)
num = x - 2;
else
num = x - 1;
while (out != n)
{
for (int i = 0; i < n; i++)
{
ans = 0;
if (out > 0)
num = x - 1;
count = 0;
while (count != b + 1)
{
if (out == n - 1 && ans == 1)
break;
if (num < n - 1)
num++;
else
num = 0;
if (people[num] != 0)
count++;
if (count == b && ans == 0)
{
if (out > 0)
cout << " ";
cout << num + 1;
people[num] = 0;
ans = 1;
}
}
x = num;
out++;
}
}
cout << endl;
}
return 0;
}

itsa65 3螺旋矩陣

#include<iostream>
using namespace std;

int main()
{
int size, direction, x, y, tx, ty, num, finish;
char c;
int arr[32][32];
int cw[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
int cc[4][2] = { { 1,0 },{ 0,1 },{ -1,0 },{ 0,-1 } };
while (cin >> size >> c >> direction)
{
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
arr[i][j] = 0;
x = y = tx = ty = finish = 0;
num = 2;
arr[0][0] = 1;
if (direction == 1)
{
for (int i = 0; i <= size / 2; i++)
{
for (int j = 0; j < 4; j++)
{
if (!finish)
{
for (int k = 1; k < size; k++)
{
if (arr[x + cw[j][0] * k][y + cw[j][1] * k] == 0)
{
arr[x + cw[j][0] * k][y + cw[j][1] * k] = num;
tx = x + cw[j][0] * k;
ty = y + cw[j][1] * k;
if (num == size * size)
{
finish = 1;
break;
}
num++;
}
else
break;
}
}
x = tx;
y = ty;
}
}
}
else
{
for (int i = 0; i <= size / 2; i++)
{
for (int j = 0; j < 4; j++)
{
if (!finish)
{
for (int k = 1; k < size; k++)
{
if (arr[x + cc[j][0] * k][y + cc[j][1] * k] == 0)
{
arr[x + cc[j][0] * k][y + cc[j][1] * k] = num;
tx = x + cc[j][0] * k;
ty = y + cc[j][1] * k;
if (num == size * size)
{
finish = 1;
break;
}
num++;
}
else
break;
}
}
x = tx;
y = ty;
}
}
}
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (j > 0)
cout << ",";
if (arr[i][j] < 10)
cout << "00" << arr[i][j];
else if (arr[i][j] < 100)
cout << "0" << arr[i][j];
else
cout << arr[i][j];
}
cout << endl;
}
}
return 0;
}

itsa65 2計算Pi

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

int main()
{
int n;
double sum, k;
while (true)
{
sum = 0, k = 1;
cin >> n;
if (n == 0)
break;
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
sum += 4 / k;
else
sum -= 4 / k;
k += 2;
}
cout << fixed << setprecision(5) << sum << endl;
}
return 0;
}

itsa [C_AR18-易]題目配對

1.N不一定等於M,所以可能N>M或N<M,連線的結果會不同。
2.把全部相同的連線都找出來,把連線的位置存到陣列。
3.將陣列當區間用,依a小 b大的方式存到陣列,將陣列由小至大排序。
例: 設[1,2] [3,2] [3,4] [2,4]
轉成  [1,2] [2,3] [3,4] [2,4]
排序  [1,2] [2,3] [2,4] [3,4]
4.利用剛轉完的區間,做判斷,連線不交叉的最大配對數為,區間不重疊的最大個數。
(有點數學畫數線的感覺)
例:
(1) BCEAD
      BEACD
原 [1,2] [3,2] [4,3] [2,4] [5,5]
轉換並排序 [1,1] [2,3] [2,4] [3,4] [5,5]
取並排的最大值(不重疊,兩端不算)
依單純數學來看,可得到下列可能
[1,1] [2,3] [3,4] [5,5] #4 BEAD
[1,1] [2,4] [5,5]  BCD
[1,1] [2,3] [5,5]  BED
[1,1] [3,4] [5,5]  BAD
(2) BCEAD
     BAECD
原 [1,1] [2,4] [3,3] [4,2] [5,5]

轉換並排序 [1,1] [2,4] [2,4] [3,3] [5,5]
[1,1] [2,4] [5,5] #3 BCD
[1,1] [3,3] [5,5] #3 BED
[1,1] [2,4] [5,5] #3 BAD
從此可得知挑的時候,下一個a要大於前一個的a,b要大於前一個的b

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

int main()
{
string s1, s2;
int N, M, max = 0, x = 0, count = 0, ta = 0, tb = 0, tmp = 0, temp = 0;
cin >> N >> M;
getline(cin, s1);
getline(cin, s1);
getline(cin, s2);
int *a = new int[N];
int *b = new int[N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (s1[i] == s2[j])
{
if (j > i)
{
a[x] = i + 1;
b[x] = j + 1;
}
else
{
a[x] = j + 1;
b[x] = i + 1;
}
x++;
}
}
}
if (x != 0)
{
for (int i = 0; i < x - 1; i++)
{
for (int j = i + 1; j < x; j++)
{
if ((a[i] > a[j]) || (a[i] == a[j] && b[i] > b[j]))
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
}
for (int i = 0; i < x; i++)
{
ta = a[i];
tb = b[i];
count = 1;
for (int j = 0; j < x; j++)
{
if (j > i)
{
if (a[j] > ta && b[j] > tb)
{
ta = a[j];
tb = b[j];
count++;
}
}
}
if (count > max)
max = count;
}
}
cout << max << endl;
return 0;
}