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