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