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? (模拟高精度)的更多相关文章
- hdu 1316 How Many Fibs?
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1316 How Many Fibs? Description Recall the definition ...
- hdu 1316 How many Fibs?(高精度斐波那契数)
// 大数继续 Problem Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn : ...
- HDU 1316 How Many Fibs?(java,简单题,大数)
题目 /** * compareTo:根据该数值是小于.等于.或大于 val 返回 -1.0 或 1: public int compareTo(BigInteger val) 将此 BigInteg ...
- HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)
HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...
- HDU 1002 - A + B Problem II - [高精度]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 Problem DescriptionI have a very simple problem ...
- HDU 5948 Thickest Burger 【模拟】 (2016ACM/ICPC亚洲区沈阳站)
Thickest Burger Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))
Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- HDU 4814 Golden Radio Base 模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4814 题目大意: 把一个正整数表示为φ进制, φ = (1+√5)/2 . 且已知: 1. φ + 1 ...
随机推荐
- SQL Server :理解GAM和SGAM页
原文:SQL Server :理解GAM和SGAM页 我们知道SQL Server在8K 的页里存储数据.分区就是物理上连续的8个页.当我们创建一个数据库,数据文件会被逻辑分为页和区,当用户对象创建时 ...
- Binder Proxy技术方案
Binder Proxy技术方案 作者 低端码农 时间 2014.08.23 0x0 看到有多朋友尝试通过hook系统进程system_process的ioctl,以企图截获系统的IPC通讯.这个方法 ...
- 解决SMARTFORMS 中table 控件单行跨页的问题
在CX项目中,MM模块做了大量的的单据打印的工作,一个问题困扰了我好久,一直不能解决.当物料描述很长时,table控件在单元格中能自动换行,这样就有可能在换页处出现一行记录的一部分打在上一页,一部分记 ...
- 【Android进阶】为什么要创建Activity基类以及Activity基类中一般有哪些方法
现在也算是刚刚基本完成了自己的第一个商业项目,在开发的过程中,参考了不少人的代码风格,然而随着工作经验的积累,终于开始慢慢的了解到抽象思想在面向对象编程中的重要性,这一篇简单的介绍一下我的一点收获. ...
- 【Swift】学习笔记(一)——熟知 基础数据类型,编码风格,元组,主张
自从苹果宣布swift之后,我一直想了解,他一直没有能够把它的正式学习,从今天开始,我会用我的博客来驱动swift得知,据我们了解还快. 1.定义变量和常量 var 定义变量,let定义常量. 比如 ...
- hdu3530Subsequence rmq
//使用rmq办,ma[i][j],同i作为一个起点2^j阵列的最大长度值 //启动枚举问最长的子列 //枚举的最大长度2^(j-1)和2^(j)z之间 //然后在该范围内找到 #include< ...
- FileStream:The process cannot access the file because it is being used by another process
先看下面一段代码(先以共享的方式打开文件读写,然后以只读的方式打开相同文件): FileStream fs = new FileStream(filePath, FileMode.Open, Fil ...
- defgen工具
构造defgen档 由于 Oracle 和 SQL Server 中的数据类型不同.所以您必须建立数据类型转换.GoldenGate 提供了一个名为 DEFGEN 的专用工具.用于生成数据定义,当源表 ...
- Xamarin.Android 入门实例(2)之实现WCF 寄宿于IIS 的Web服务提供
1.WCF 契约 ICalculator.cs using System.ServiceModel; namespace Contracts { [ServiceContract] public in ...
- cocos2dx --- Widget 载入中 CCNode
如果说. Widget 有addChild() 与 addNode() 两个方法. 如今我要载入一个粒子特效进去,下图: Widget* layout = dynamic_cast<Wid ...