标签:
题目链接 http://vjudge.net/problem/21247
题意 给A,B。求[A, B] 内有多少个fib数。
解题思路
直接高精度模拟
代码
#include<stdio.h> #include<string.h> #define MAX_SIZE 110 #define MAX_LEN 1010 char a[MAX_SIZE], b[MAX_SIZE]; int f[MAX_LEN][MAX_SIZE]; int len[MAX_LEN]; int cmp(int *x, char *y, int m) { int i = 0; if(m < strlen(y)) return -1; if(m > strlen(y)) return 1; while(i < m && x[MAX_SIZE-m+i] == y[i] - ‘0‘) i++; if(i == m) return 0; return x[MAX_SIZE-m+i] - y[i] + ‘0‘; } void add(int *res, int *x, int *y, int lenx, int leny, int index) { int m = lenx > leny ? lenx : leny; int i = 1; int k = 0; len[index] = m; while(i <= m) { res[MAX_SIZE-i] = x[MAX_SIZE-i] + y[MAX_SIZE-i] + k; k = res[MAX_SIZE-i] / 10; res[MAX_SIZE-i] %= 10; i++; } if(k != 0) { res[MAX_SIZE-i] = k; len[index]++; } } int main() { scanf("%s%s", a, b); while(!(a[0] == ‘0‘ && b[0] == ‘0‘)) { f[1][MAX_SIZE-1] = 1; len[1] = 1; f[2][MAX_SIZE-1] = 2; len[2] = 1; int i = 1; int count = 0; do { if(cmp(f[i], a, len[i]) >= 0) count++; i++; if(i >= 3) add(f[i], f[i-1], f[i-2], len[i-1], len[i-2], i); }while(cmp(f[i], b, len[i]) <= 0); printf("%d\n", count); scanf("%s%s", a, b); } return 0; }
标签:
原文地址:http://www.cnblogs.com/ZengWangli/p/5876407.html