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

2018年8月13日 星期一

itsa [C_AR027-中]整數集合之交集與聯集

1.數的大小可以很大,因為我是用陣列編號去做,所以要設比較大
2.插入元素可以重複(陷阱),所以要判斷是否已經有了
3.判斷子集合,輸出為空集合,即指A本身是空集合,才輸出empty
例:
(1). A {3,4} B {2,3,5} 輸出 F
(2). A {} B {1,2,3} 輸出 empty
4.交集若為空集合,仍要輸出empty
註:num為集合中元素的個數

#include<iostream>
using namespace std;

class poly {
public:
poly() { initializeSet(); }
void initializeSet(void);
void insertElement(void);
void deleteElement(void);
void Intersection(poly a);
void Union(poly a);
void subSet(poly a);
void setPrint(void);
private:
int set[10001];
int num;
};

void poly::initializeSet()
{
for (int i = 0; i < 10001; i++)
set[i] = 0;
num = 0;
}

void poly::insertElement()
{
int x;
cin >> x;
if (set[x] == 0)
{
set[x] = 1;
num++;
}
}

void poly::deleteElement()
{
int y;
cin >> y;
if (set[y] != 0)
{
set[y] = 0;
num--;
}
}

void poly::Intersection(poly a)
{
int j = 0, isEmpty = 1;
int inset[10001] = { 0 };
for (int i = 0; i < 10001; i++)
{
if (set[i] != 0 && a.set[i] != 0)
{
inset[i] = 1;
isEmpty = 0;
}
}
if (isEmpty == 0)
{
for (int i = 0; i < 10001; i++)
{
if (inset[i] != 0)
{
if (j > 0)
cout << ",";
cout << i;
j++;
}
}
cout << endl;
}
else
cout << "empty" << endl;
}

void poly::Union(poly a)
{
int j = 0;
int unset[10001] = { 0 };
for (int i = 0; i < 10001; i++)
{
if (set[i] != 0 || a.set[i] != 0)
unset[i] = 1;
}
for (int i = 0; i < 10001; i++)
{
if (unset[i] != 0)
{
if (j > 0)
cout << ",";
cout << i;
j++;
}
}
cout << endl;
}

void poly::subSet(poly a)
{
int count = 0;
for (int i = 0; i < 10001; i++)
{
if (set[i] != 0 && a.set[i] != 0)
count++;
}
if (num == 0)
cout << "empty" << endl;
else if (count == num)
cout << "T" << endl;
else
cout << "F" << endl;
}

void poly::setPrint()
{
int j = 0;
for (int i = 0; i < 10001; i++)
{
if (set[i] != 0)
{
if (j > 0)
cout << ",";
cout << i;
j++;
}
}
cout << endl;
}

int main()
{
poly A, B;
A.insertElement();
A.insertElement();
A.insertElement();
A.deleteElement();
B.insertElement();
B.insertElement();
B.insertElement();
B.deleteElement();
A.setPrint();
B.setPrint();
A.subSet(B);
A.Intersection(B);
A.Union(B);
return 0;
}

2018年8月10日 星期五

itsa [C_AR026-中]選擇排序法動態展示

1.輸入測資時需使用while,在輸入時以非整數做結束,例:字元( / * - + )
2.選擇排序法:從最大或最小開始排序,這題是從最大
3.先找最大值與陣列編號按順序交換,交換後找次大,以此類推
例:42135
陣列編號:01234
第1次 最大值是5 跟4做交換(編號0) 輸出 52134
第2次 最大值是4 跟2做交換(編號1) 輸出 54132

#include<iostream>
using namespace std;

int main()
{
int N = 0, i = 0;
static int num[1500000];   //其實不用很大 大概最多到100就好
while (cin >> num[i++]);
N = i - 1;
for (int k = 0; k < N; k++)
{
if (k > 0)
cout << ",";
cout << num[k];
}
cout << endl;
for (int i = 0; i < N - 1; i++)
{
int max = i;
for (int j = i + 1; j < N; j++)
if (num[j] > num[max])
max = j;
if (max == i)
continue;
int tmp = num[i];
num[i] = num[max];
num[max] = tmp;
for (int k = 0; k < N; k++)
{
if (k > 0)
cout << ",";
cout << num[k];
}
cout << endl;
}
return 0;
}

itsa [C_AR025-易]計算ASCII字元

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

int main()
{
int count = 0, x = 0;
int num[100] = { 0 }, times[100] = { 0 };
string s;
getline(cin, s);
for (int i = 0; i < s.length(); i++)
{
count = 1;
if (s[i] != ' ')
{
for (int j = 0; j < s.length(); j++)
{
if (i != j && s[j] == s[i])
{
count++;
s[j] = ' ';
}
}
num[x] = s[i];
times[x] = count;
s[i] = ' ';
x++;
}
}
for (int i = 0; i < x - 1; i++)
{
for (int j = i + 1; j < x; j++)
{
if (i != j && num[i] < num[j])
{
int tmp = num[i];
num[i] = num[j];
num[j] = tmp;
int temp = times[i];
times[i] = times[j];
times[j] = temp;
}
}
}
for (int i = 0; i < x; i++)
cout << num[i] << " " << times[i] << endl;
return 0;
}

itsa [C_AR024-難]約會配對問題

1.跑n次迴圈,找目前未配對的陣列最大值
2.每跑完一次,紀錄配對完的男女編號
3.在跑下一次時,剔除配對過的編號

#include<iostream>
using namespace std;

int main()
{
int n, x = 0, max = 0, count = 0, check = 0;
int arr[100][100], a[100], b[100];
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> arr[i][j];
while (count != n)
{
max = 0;
for (int i = 0; i < n; i++)
{
check = 0;
for (int j = 0; j < x; j++)
if (i == a[j])
{
check = 1;
break;
}
if (check == 0)
{
for (int j = 0; j < n; j++)
{
check = 0;
for (int k = 0; k < x; k++)
if (j == b[k])
{
check = 1;
break;
}
if (check == 0 && arr[i][j] > max)
{
max = arr[i][j];
a[x] = i;
b[x] = j;
}
}
}
}
count++;
x++;
}
for (int k = 0; k < x; k++)
cout << "boy " << a[k] + 1 << " pair with girl " << b[k] + 1 << endl;
return 0;
}

itsa [C_AR023-易]字根與子字串

注意:有可能會有要尋找的字串大於被搜尋的字串!

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

int main()
{
int a = 0, ans = 0;
string s1, s2;
getline(cin, s1);
getline(cin, s2);
if (s1.length() <= s2.length())
{
for (int i = 0; i <= s2.length() - s1.length(); i++)
{
a = 0;
for (int j = 0; j < s1.length(); j++)
{
if (s2[i + j] != s1[j])
{
a = 1;
break;
}
}
if (a == 0)
{
ans = 1;
break;
}
}
}
if (ans == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}

itsa [C_AR022-易]字母出現的頻率

先轉全部大寫或小寫,再算出現次數。

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

int main()
{
int a[26] = { 0 };
string s;
getline(cin, s);
for (int i = 0; i < s.length(); i++)
{
if (s[i] >= 'a' && s[i] <= 'z')
s[i] -= 32;    //轉大寫
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
a[s[i] - 'A']++;
}
for (int i = 0; i < 26; i++)
{
if (i > 0)
cout << " ";
cout << a[i];
}
cout << endl;
return 0;
}