hdu 1316 How Many Fibs? (模拟高精度)

题目大意:

问[s,e]之间有多少个 斐波那契数。

思路分析:

直接模拟高精度字符串的加法和大小的比较。

注意wa点再 s 可以从 0 开始

那么要在判断输入结束的时候注意一下。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

struct node
{
    char str[111];
    int len;

    void print()
    {
        for(int i=len-1; i>=0; i--)
        {
            printf("%c",str[i]);
        }
        puts("");
    }

} fib[1500];

node operator + (const node &a,const node &b)
{
    node c;
    for(int i=0; i<=110; i++)c.str[i]='0';
    for(int i=0; i<max(a.len,b.len); i++)
    {
        int dig=c.str[i]-'0'+a.str[i]-'0'+b.str[i]-'0';
        int s=0;
        if(dig>=10)
        {
            dig-=10;
            s++;
        }
        c.str[i]=dig+'0';
        if(s)c.str[i+1]='1';
    }
    for(c.len=110; c.str[c.len]=='0' ; c.len--);
    c.len++;
    return c;
}
bool operator <= (node &a,node &b)
{
    if(a.len!=b.len)return a.len<b.len;
    else
    {
        for(int i=a.len-1; i>=0; i--)
        {
            if(a.str[i]>b.str[i])
                return false;
            else if(a.str[i]<b.str[i])
                return true;
        }
        return true;
    }
}

bool operator < (node &a,node &b)
{
    if(a.len!=b.len)return a.len<b.len;
    else
    {
        bool is=true;

        for(int i=0; i<a.len; i++)
            if(a.str[i]!=b.str[i])is=false;

        for(int i=a.len-1; i>=0 && !is; i--)
        {
            if(a.str[i]>b.str[i])
                return false;
            else if(a.str[i]<b.str[i])
                return true;
        }
        return !is;
    }
}

int main()
{
    fib[1].len=1;
    fib[1].str[0]='1';
    fib[2].len=1;
    fib[2].str[0]='2';
    for(int i=3;; i++)
    {
        fib[i]=fib[i-1]+fib[i-2];
        if(fib[i].len>101)break;
    }

    node s,e;
    while(scanf("%s%s",s.str,e.str)!=EOF)
    {
        if(s.str[0]=='0' && e.str[0]=='0')break;
        s.len=strlen(s.str);
        e.len=strlen(e.str);
        reverse(s.str,s.str+s.len);
        reverse(e.str,e.str+e.len);
        int i=1,st,ed;
        while(fib[i]<s)i++;
        st=i;
        while(fib[i]<=e)i++;
        ed=i;
        printf("%d\n",ed-st);
    }
    return 0;
}

hdu 1316 How Many Fibs? (模拟高精度),布布扣,bubuko.com

时间: 2024-10-26 10:41:04

hdu 1316 How Many Fibs? (模拟高精度)的相关文章

hdu 1316 How many Fibs?(高精度斐波那契数)

//  大数继续 Problem Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn := fn-1 + fn-2 (n >= 3) Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a, b]. Input The input contains several test cas

HDU 1316 How Many Fibs?(java,简单题,大数)

题目 /** * compareTo:根据该数值是小于.等于.或大于 val 返回 -1.0 或 1: public int compareTo(BigInteger val) 将此 BigInteger 与指定的 BigInteger 进行比较. 对于针对六个布尔比较运算符 (<, ==, >, >=, !=, <=)中的每一个运算符的各个方法,优先提供此方法. 执行这些比较的建议语句是:(x.compareTo(y) <op> 0), 其中 <op> 是

hdu 1316 How Many Fibs?

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1316 How Many Fibs? Description Recall the definition of the Fibonacci numbers: $f_1 := 1$ $f_2 := 2$ $f_n := f_{n-1} + f_{n-2} \ \ (3 \leq n)$ Given two numbers a and b, calculate how many Fibonacci num

HDU 1316 How Many Fibs? (大Fib数,还是Java大法好)

How Many Fibs? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5822    Accepted Submission(s): 2267 Problem Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn := fn-

HDU 1316 How Many Fibs? java大数(水

水一水.. import java.math.*; import java.util.*; import java.io.*; public class Main { BigInteger[] fib = new BigInteger[505]; public void work(){ fib[1] = BigInteger.ONE; fib[2] = BigInteger.valueOf(2); for(int i = 3; i <= 500; i++) fib[i] = fib[i-1].a

HDU 1316 斐波那契数列+高精度

How Many Fibs? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4235    Accepted Submission(s): 1669 Problem Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn := fn-1

POJ 2413 How many Fibs? /HDOJ 1316 How Many Fibs?

题目来源:http://poj.org/problem?id=2413 / http://acm.hdu.edu.cn/showproblem.php?pid=1316 How many Fibs? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10742   Accepted: 3979 Description Recall the definition of the Fibonacci numbers: f1 :=

Hdu 3887树状数组+模拟栈

题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 582 Problem Description You are given a tree, it’s root is p, and the node is numbered fr

hdu 4891 The Great Pan(模拟)

题目链接:hdu 4891 The Great Pan 题目大意:给出一个文本,问说有多少种理解方式. 1. $$中间的,(s1+1) * (s2+1) * ...*(sn+1), si表示连续的空格数. 2.{}中间,即 | 的个数+1. 解题思路:模拟. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1<<22