zoj1962「建议收藏」

2022-07-19 08:52:54 浏览数 (3)

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说zoj1962「建议收藏」,希望能够帮助大家进步!!!

题目大意:

给定两个数字a和b,计算a和b之间有多少Fibonacci数字

解题思路:

大数相加和比较

代码如下:

代码语言:javascript复制
#include <stdio.h>
#include <string.h>
char a[102], b[102], len_a, len_b;
int low[102], high[102], num[102], num2[102];
int cmp(int *x, int *y)
{
    int i, last_x, last_y;
    last_x = last_y = 101;
    while (x[last_x] == 0)
    {
        last_x--;
    }
    while (y[last_y] == 0)
    {
        last_y--;
    }
    if(last_x < last_y) return -1;
    if(last_x > last_y) return 1;
    for(i = last_x; i >= 0; i--)
    {
        if(x[i] > y[i]) return 1;
        if(x[i] < y[i]) return -1;
    }
    return 0;
}


void get_next(void)
{
    int i, add, temp[102], last_num;
    last_num = 101;
    while(num[last_num] == 0)
    {
        last_num--;
    }
    for(i = 0; i <= last_num 1; i  )
        temp[i] = num[i];
    add = 0;
    for(i = 0; i <= last_num 1; i  )
    {
        num[i]  = num2[i]   add;
        if(num[i] >= 10)
        {
            add = 1;
            num[i] -= 10;
        }
        else add = 0;
    }
    for(i = 0; i <= last_num 1; i  )
       num2[i] = temp[i];
}
int main(void)
{
    int i, cnt;
    while (scanf("%s %s", a, b) != EOF && (strcmp(a, "0") || strcmp(b, "0")))
    {
        for(i = 0; i < 102; i  )
            low[i] = high[i] = num[i] = num2[i] = 0;
        cnt = 0;
        num[0] = num2[0] = 1;
        len_a = strlen(a);
        len_b = strlen(b);
        for(i = 0; i < len_a; i  )
            low[i] = a[len_a-1-i] - '0';
        for(i = 0; i < len_b; i  )
            high[i] = b[len_b-1-i] - '0';
        while (cmp(num, low) < 0)
        {
            get_next();
        }
        while (cmp(num, high) <= 0)
        {
            cnt  ;
            get_next();
        }
        printf("%dn", cnt);
    }
    return 0;
}

今天文章到此就结束了,感谢您的阅读,Java架构师必看祝您升职加薪,年年好运。

0 人点赞