UVa10183 - How Many Fibs?

来源:互联网 发布:数据库时间怎么修改 编辑:程序博客网 时间:2024/05/22 13:43
#include <stdio.h>#include <string.h>#define MAX 150char Fib[5 * MAX][MAX] = {"0", "1", "2"};int Fib_len[MAX] = {1, 1};void call_add(char *first, char *second, char *result);void reverse(char *from, char *to);int main(){int count;int len = 0;char a[MAX], b[MAX];int a_len, b_len;int start, end;#ifndef ONLINE_JUDGE        freopen("d:\\UVa\\uva_in.txt", "r", stdin);#endiffor (count = 3; len <= 110; count++) {call_add(Fib[count - 2], Fib[count - 1], Fib[count]);len = strlen(Fib[count]);if (!Fib_len[len])Fib_len[len] = count;}count--;while (scanf("%s%s", a, b) && ((strcmp(a, "0") != 0) || (strcmp(b, "0") != 0))) {a_len = strlen(a);b_len = strlen(b);start = Fib_len[a_len];end = Fib_len[b_len];if (strcmp(Fib[start], a) < 0) {do {start++;} while (strlen(Fib[start]) == a_len && strcmp(Fib[start], a) < 0);}if (strcmp(Fib[end], b) <= 0) {do {end++;} while (strlen(Fib[end]) == b_len && strcmp(Fib[end], b) <= 0);}end--;        printf("%d\n", end - start + 1);}return 0;}void reverse(char *from, char *to){int i, len = strlen(from);for (i = 0; i < len; i++)to[i] = from[len - 1 - i];to[len] = '\0';}void call_add(char *first, char *second, char *result){char F[MAX], S[MAX], Res[MAX];int f_len, s_len, sum, now, carry;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);}

原创粉丝点击