#include <stdio.h>
#include <string.h>
#define MAX 650
#define N 1005
char map[N][MAX] = {"0", "1", "2"};
char buf[MAX], temp[MAX];
char F[MAX], S[MAX], Res[MAX];;
void reverse(char *from, char *to);
void call_mult(char *first, char *second, char *result);
void call_minus(char *first, char *second, char *result);
void call_add(char *first, char *second, char *result);
void call_divide(char *first, int div, char *result);
int main()
{
int n, i;
setvbuf(stdout, NULL, _IONBF, 0);
for (i = 3; i < N; i++) {
sprintf(buf, "%d", 4 * i - 2);
call_mult(map[i - 1], buf, temp);
call_divide(temp, i + 1, map[i]);
}
while (scanf("%d", &n) != EOF) {
printf("%s\n", map[n]);
}
return 0;
}
void reverse(char *from, char *to)
{
int len = strlen(from), i;
for (i = 0; i < len; i++) {
to[i] = from[len - 1 - i];
}
to[len] = '\0';
}
void call_mult(char *first, char *second, char *result)
{
int f_len, s_len, f, s, r, r_len, sum, carry;
f_len = strlen(first);
s_len = strlen(second);
r_len = f_len + s_len;
r = -1;
reverse(first, F);
reverse(second, S);
for (f = 0; f < r_len; f++)
Res[f] = '0';
Res[f] = '\0';
for (s = 0; s < s_len; s++) {
for (f = 0, carry = 0; f < f_len; f++) {
sum = (F[f] - '0') * (S[s] - '0') + (Res[f + s] - '0') + carry;
Res[f + s] = sum % 10 + '0';
carry = sum / 10;
if (f + s > r)
r = f + s;
}
while (carry) {
sum = (Res[f + s] - '0') + carry;
Res[f + s] = sum % 10 + '0';
carry = sum / 10;
if (f + s > r)
r = f + s;
f++;
}
}
for (; r > 0 && Res[r] == '0'; r--);
Res[r + 1] = '\0';
reverse(Res, result);
}
void call_minus(char *first, char *second, char *result)
{
int f_len, s_len, now, borrow, sum;
f_len = strlen(first);
s_len = strlen(second);
reverse(first, F);
reverse(second, S);
for (now = 0, borrow = 0; now < f_len && now < s_len; now++) {
sum = (F[now] - '0') - (S[now] - '0') - borrow;
if (sum < 0) {
borrow = 1;
F[now] = sum + 10 + '0';
} else {
borrow = 0;
F[now] = sum + '0';
}
}
for (; now < f_len && borrow; now++) {
sum = (F[now] - '0') - borrow;
if (sum < 0) {
borrow = 1;
F[now] = sum + 10 + '0';
} else {
borrow = 0;
F[now] = sum + '0';
}
}
for (now = f_len - 1; now > 0 && F[now] == '0'; now--);
F[now + 1] = '\0';
reverse(F, result);
}
void call_add(char *first, char *second, char *result)
{
int f_len, s_len, now, carry, sum;
f_len = strlen(first);
s_len = strlen(second);
reverse(first, F);
reverse(second, S);
for (now = 0, carry = 0; now < f_len && now < s_len; now++) {
sum = (F[now] - '0') + (S[now] - '0') + carry;
Res[now] = sum % 10 + '0';
carry = sum / 10;
}
for (; now < f_len; now++) {
sum = (F[now] - '0') + carry;
Res[now] = sum % 10 + '0';
carry = sum / 10;
}
for (; now < s_len; now++) {
sum = (S[now] - '0') + carry;
Res[now] = sum % 10 + '0';
carry = sum / 10;
}
if (carry)
Res[now++] = carry + '0';
Res[now] = '\0';
reverse(Res, result);
}
void call_divide(char *first, int div, char *result)
{
int len = strlen(first);
int remainder = 0, now, sum;
for (now = 0; now < len; now++) {
sum = remainder * 10 + (first[now] - '0');
Res[now] = sum / div + '0';
remainder = sum % div;
}
Res[now] = '\0';
for (now = 0; Res[now] == '0'; now++);
strcpy(result, &Res[now]);
if (strlen(result) == 0)
strcpy(result, "0");
}
UVa10303 - How Many Trees?
最新推荐文章于 2020-03-07 17:45:21 发布