+1
仰望星空...........不忘初心!
#include<stdio.h> #define ACC 100 #define N ACC + 2 int cmp(char * a, char * b, int n) { int i; for(i = 0; i < n; i++) if(a[i] != b[i]) return a[i] - b[i]; return 0; } int sub(char * a, char * b, int n) { int f; for(f = 0 ; --n >= 0; f = a[n] < 0 ? (a[n] += 10, 1) : 0) a[n] -= b[n] + f; } void div(char * a, char * b, char * c, int n) { int i, j; for(i = 0; i < n; i++) { for(c[i] = 0; cmp(a, b, n) >= 0; c[i]++) sub(a, b, n); a[0] = a[0] * 10 + a[1]; for(j = 1; j < n - 1; j++) a[j] = a[j + 1]; a[n - 1] = 0; } } void golden_number(char * x, int n) { char * p, * a, * b, * c, * t; int i; p = (char *)malloc(n * 3); a = p; b = a + n; c = b + n; for(i = 0; i < n; c[i++] = 0); do{ t = a; a = c; c = t; for(i = 0; i < n; b[i] = a[i++]); a[0] += 1; b[0] += 2; div(a, b, c, n); b[0] -= 2; }while(cmp(b, c, n)); for(i = 0; i < n; x[i] = c[i++]); free(p); } void output(char * x, int n) { int i; printf("%d.", x[0]); for(i = 1; i < n; printf("%d", x[i++])); } int main() { char x[N]; int i; golden_number(x, N); if(x[N - 1] >= 5) for(i = N - 2; i >= 0 && ++x[i] > 9; x[i--] = 0); output(x, N - 1); return 0; }