回复 7楼 rjsp
#include<stdio.h>#define M 10000
void multiply(int x[], int y, int z[]) {
int i, temp, b = 0;
for (i = M - 1; i >= 0; i--)
{
temp = x[i] * y + b;
z[i] = temp % 10;
b = temp / 10;
}
}
void add(int a[], int b[], int c[]) {
int i, temp, k = 0;
for (i = M - 1; i >= 0; i--) {
temp = a[i] + b[i] + k;
c[i] = temp % 10;
k = temp / 10;
}
}
void copy(int x[], int y[]) {
int i, j;
for (i = 0; i < M; i++) {
if (x[i] > 0) {
j = i;
break;
}
}
for (i = j; i < M; i++) {
y[i] = x[i];
}
}
void pell(int i, int a[], int b[], int a1[], int a2[], int b1[], int b2[]) {
int j, m, n;
if (i == 0) { printf("end"); }
else {
multiply(a, 3, a2);
multiply(b, 4, b2);
add(a2, b2, a1);
multiply(a, 2, a2);
multiply(b, 3, b2);
add(a2, b2, b1);
copy(a1, a);
copy(b1, b);
for (j = 0; j < M; j++) {
if (a1[j] > 0)
{
m = j;
break;
}
}
printf("(");
for (n = m; n < M; n++)
printf("%d", a1[n]);
printf(", ");
for (j = 0; j < M; j++)
{
if (b1[j] > 0)
{
m = j;
break;
}
}
for (n = m; n < M; n++)
printf("%d", b1[n]);
printf(")");
printf("\n");
pell(i - 1, a, b, a1, a2, b1, b2);
}
}
int main() {
int a[M] = { 0 }, b[M] = { 0 };
a[M - 1] = 3;
b[M - 1] = 2;
int a1[M] = { 0 }, a2[M] = { 0 }, b1[M] = { 0 }, b2[M] = { 0 };
printf("(p, q):\n");
printf("(3, 2)\n");
pell(49, a, b, a1, a2, b1, b2);
fflush(stdin);
getchar();
return 0;
}
这个可以,秒出