Project Euler 25 1000-digit Fibonacci number
题意:在斐波那契数列( 1 ,1,2,3,5 ...... )中,第一个有1000位数字的是第几项?
思路:****滚动数组 + 大数加法
/*************************************************************************
> File Name: euler025.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月25日 星期日 11时24分33秒
************************************************************************/
#include <stdio.h>
#include <inttypes.h>
int32_t main() {
int32_t fib[3][1100] = {0}; // fib[][0] 记录该数组所代表数字的最大位数
fib[1][0] = fib[1][1] = 1;
fib[2][0] = fib[2][1] = 1;
int32_t number = 2 , idx1 , idx2 , idx3;
while( fib[ number % 3 ][0] < 1000 ) {
number++;
idx1 = (number - 2) % 3; // 依靠取模运算进行“滚动”,fib[idx1] < fib[idx2] < fib[idx3]
idx2 = (number - 1) % 3;
idx3 = number % 3;
for(int32_t i = 1 ; i <= fib[idx2][0] ; i++)
fib[idx3][i] = fib[idx2][i] + fib[idx1][i];
fib[idx3][0] = fib[idx2][0];
for(int32_t i = 1 ; i <= fib[idx3][0] ; i++) {
if( fib[idx3][i] >= 10 ){
fib[idx3][i+1] += fib[idx3][i] / 10;
fib[idx3][i] %= 10;
if( fib[idx3][0] < i + 1 ) fib[idx3][0] = i + 1;
}
}
}
printf("%d\n",number);
return 0;
}
Project Euler 25 1000-digit Fibonacci number的更多相关文章
- project euler 25 fibonacci
数学方法: Saying that a number contains 1000 digits is the same as saying that it's greater than 10**999 ...
- project euler 12 Highly divisible triangular number
Highly divisible triangular number Problem 12 The sequence of triangle numbers is generated by addin ...
- Project Euler 92:Square digit chains C++
A number chain is created by continuously adding the square of the digits in a number to form a new ...
- Project Euler 20 Factorial digit sum( 大数乘法 )
题意:求出100!的各位数字和. /************************************************************************* > Fil ...
- Project Euler 16 Power digit sum( 大数乘法 )
题意: 215 = 32768,而32768的各位数字之和是 3 + 2 + 7 + 6 + 8 = 26. 21000的各位数字之和是多少? 思路:大数乘法,计算 210 × 100 可加速计算,每 ...
- Project Euler 435 Polynomials of Fibonacci numbers (矩阵快速幂)
题目链接: https://projecteuler.net/problem=435 题意: The Fibonacci numbers $ {f_n, n ≥ 0}$ are defined rec ...
- Project Euler 51: Prime digit replacements
通过替换*3这样一个两位数的第一位,我们可以发现形成的九个数字有六个是质数,即13, 23,43,53,73,83.类似的,如果我们用同样的数字替换56**3这样一个五位数的第三位和第四位,会生成56 ...
- Project Euler 56: Powerful digit sum
一个古戈尔也就是\(10^{100}\)是一个天文数字,一后面跟着一百个零.\(100^{100}\)更是难以想像的大,一后面跟着两百个零.但是尽管这个数字很大,它们各位数字的和却只等于一.考虑两个自 ...
- Project Euler 63: Powerful digit counts
五位数\(16807=7^5\)也是一个五次幂,同样的,九位数\(134217728=8^9\)也是一个九次幂.求有多少个\(n\)位正整数同时也是\(n\)次幂? 分析:设题目要求的幂的底为\(n\ ...
随机推荐
- COGS——C1176. [郑州101中学] 月考
http://cogs.pro/cogs/problem/problem.php?pid=1176 [题目描述] 在上次的月考中Bugall同学违反了考场纪律还吃了处分,更可气的是在第二天的校会时 间 ...
- find-median-from-data-stream & multiset priority queue 堆
https://leetcode.com/problems/find-median-from-data-stream/ 这道题目实在是不错,所以单独拎出来. https://discuss.leetc ...
- RubyMine生成reader/writer方法
RubyMine生成reader/writer方法 在非类的ruby文件中,Alt+Insert会出现新建文件的选项: 在ruby文件的类中,Alt+Insert会出现get/set方法生成提示和重构 ...
- ntp服务及时间同步问题
今有一小型项目,全然自主弄,原来以为非常easy的NTP服务.我给折腾了2个多小时才整撑头(曾经都是运维搞,没太注意,所以这技术的东西.在简单都须要亲尝啊).这里记录为以后别再浪费时间. 目标环境,5 ...
- scala并发编程原生线程Actor、Case Class下的消息传递和偏函数实战
參考代码: import scala.actors._ case class Person(name:String,age:Int) class HelloActor extends Actor{ d ...
- android制作闪动的红心
先上一张效果图吧: 说说这个东西的来源吧.今天突然想到笛卡尔心形图,想去看看能不能画个心出来,可是看到一篇不错的文章,那篇文章罗列了非常多关于心形的函数方程,这可把我高兴坏了,于是我选取了一个比較好看 ...
- UML的基本图(二)
Both sequence diagrams and communication diagrams are kinds of interaction diagrams. An interacti ...
- DotNetBar.Bar菜单的使用
DotNetBar.Bar菜单的使用 老帅 在C#中使用控件DevComponents.DotNetBar.Bar时,怎样设计菜单呢? 1.拖放生成一个菜单容器 拖放一个D ...
- Linux内核OOM机制的详细分析【转】
本文转载自:http://blog.csdn.net/liukuan73/article/details/43238623 Linux内核根据应用程序的要求分配内存,通常来说应用程序分配了内存但是并没 ...
- [CQOI 2007] 涂色
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1260 [算法] 区间DP [代码] #include<bits/stdc++. ...