UVA12333--Revenge of Fibonacci
来源:互联网 发布:知达常青藤中学校费用 编辑:程序博客网 时间:2024/05/16 20:28
题意:
给你一个数字串,判断他是哪一个Fib数的前缀,有多种答案输出最小的,不存在输出-1。
思路:
大数加法计算斐波那契数,字典树搜索
计算过程中进行字典树的创建,计算时,当斐波那契数大于50位时,只计算高50位即可。
代码:
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#define maxn 100100using namespace std;typedef struct Node { int value; Node* next[10]; Node() { value = -1; for (int i = 0; i < 10; i++) next[i] = NULL; }} Node, *Tree;int num[2][maxn];Tree head;void addNode(int *s, int len, int n) { Node* p = head; for (int i = 0; i < len; i++) { int x = s[i]; if (p->next[x] == NULL) p->next[x] = new Node; p = p->next[x]; if (p->value == -1) p->value = n; }}//计算斐波那契额数并创建字典树void getFibAndTree() { head = new Node; head->next[1] = new Node; head->next[1]->value = 0; memset(num, 0, sizeof(num)); num[0][0] = num[1][0] = 1; int b = 0, e = 1, flag = 0; for (int i = 2; i < 100000; i++) { int jw = 0; for (int j = b; j < e; j++) { int t = num[0][j] + num[1][j] + jw; jw = 0; if (t > 9) jw = 1; num[flag][j] = t % 10; } if (jw) num[flag][e++] = 1; if (e - b > 50) b++; int len = e - b; if (len > 40) len = 40; int cc[len]; for (int x = 0; x < len; x++) cc[x] = num[flag][e - 1 - x]; addNode(cc, len, i); flag = !flag; }}//搜索int msearch(char *in) { int len = strlen(in); Node* p = head; for (int i = 0; i < len; i++) { if (p->next[in[i] - '0'] == NULL) return -1; p = p->next[in[i] - '0']; } return p->value;}int main() { getFibAndTree(); char in[45]; int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> in; printf("Case #%d: %d\n", i, msearch(in)); }}
0 0
- UVa12333 - Revenge of Fibonacci
- UVA12333--Revenge of Fibonacci
- 算法竞赛入门经典 第二版 习题5-15 Fibonacci的复仇 Revenge of Fibonacci uva12333
- [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci
- HDU4099 Revenge of Fibonacci
- 12333 - Revenge of Fibonacci
- HDU 4099 Revenge of Fibonacci
- hdu 4099 Revenge of Fibonacci
- UVa 12333 - Revenge of Fibonacci
- HDU 4099 Revenge of Fibonacci
- HDU 4099 Revenge of Fibonacci
- hdu 5018 Revenge of Fibonacci
- HDOJ 5018 Revenge of Fibonacci
- hdoj 5018 Revenge of Fibonacci
- hdu 5018 Revenge of Fibonacci
- hdoj 5018 Revenge of Fibonacci
- UVA 12333 Revenge of Fibonacci
- HDU 5018 Revenge of Fibonacci
- python八大排序算法
- 单例模式 接口回调 异常
- IplImage结构体
- Ubuntu下安装hive,并配置mysql作为元数据库
- TBSchedule原理与实践
- UVA12333--Revenge of Fibonacci
- 167. Two Sum II - Input array is sorted 难度:medium
- C++关于堆上与栈上分配对象
- leetcode交换相邻的两个节点的值
- 用Python做投资-关于term spread
- 内存初始化(上)
- Laxcus大数据管理系统单机集群版
- Android中的调色板功能的实现
- 71. Simplify Path