当前位置:网站首页>【牛客刷题】NowCoder号称自己已经记住了1-100000之间所有的斐波那契数。 为了考验他,我们随便出一个数n,让他说出第n个斐波那契数。如果第n个斐波那契大于6位则只取后6位。
【牛客刷题】NowCoder号称自己已经记住了1-100000之间所有的斐波那契数。 为了考验他,我们随便出一个数n,让他说出第n个斐波那契数。如果第n个斐波那契大于6位则只取后6位。
2022-06-27 15:41:00 【小型骷髅】

试题链接:斐波那契凤尾__牛客网
先贴代码:
import java.util.*;
public class Main {
public static void main(String[] args) {
int border = -1; // 记录下第一个大于六位数的斐波那契数的下标
long[] ans = new long[100000]; // 创建数组
ans[0] = 1;
ans[1] = 2;
for (int i = 2; i < 100000; i++) {
long next = ans[i - 1] + ans[i - 2];
if (border == -1 && next >= 1000000) {
border = i + 1; // 当有第一个next大于六位数时,border记录下它的坐标
}
ans[i] = next % 1000000; // 模上 1000000 表示取后6位,有几个0,表示取后几位!
}
Scanner s = new Scanner(System.in);
while (s.hasNextInt()) {
// 输入n:
int n = s.nextInt();
long f = ans[n - 1];
if (n < border) {
System.out.printf("%d\n", f); // 整数正常输出
} else {
System.out.printf("%06d\n", f); // 整数输出,宽度为6,不足处左边补0
}
}
}
}解题思路:
题目告诉我们它记住了 1~100000 之间的所有斐波那契数,所以整个题目的区间就是1~10000,要求我们输出第 n 个斐波那契数,如果大于6位则输出后6位即可。
由上可知,n 的范围是 1~100000 ,所以我们可以先创建一个数组,容量就为100000,在这个数组里存储每个斐波那契数或者它的后六位。不过要注意,我们创建数组时要创建 long 长整型数组!

然后开始往这个数组里填入数据,数据为斐波那契数,不过我们需要另外设置一个边界值 border ,这个边界值作用是标记这个数组里的第一个超过六位数的斐波那契数的下标,方便我们之后输出,如果遍历数组时,下标小于这个边界值 border ,那么说明该下标的斐波那契数是不足六位或者是正好六位数的,此时直接输出即可,反之则说明该数是大于六位数的,这时候就得取它的后六位输出。

设置好之后就可以根据输入的 n 来输出对应的第 n 个斐波那契数了。

边栏推荐
- 【kotlin】第二天
- Cesium 使用MediaStreamRecorder 或者MediaRecorder录屏并下载视频,以及开启摄像头录像。【转】
- Relation and operation of ORM table
- SQL injection principle
- SQL parsing practice of Pisa proxy
- express
- Li Chuang EDA learning notes 16: array copy and array distribution
- [pyGame games] this "eat everything" game is really wonderful? Eat them all? (with source code for free)
- Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
- LeetCode每日一练(两数之和)
猜你喜欢

The interview lasted for half a year. Last month, I successfully got Alibaba p7offer. It was all because I chewed the latest interview questions in 2020!

LeetCode每日一练(无重复字符的最长子串)

Four characteristics of transactions

SQL injection principle

substrate 技术每周速览 20220411

鸿蒙发力!HDD杭州站·线下沙龙邀您共建生态

【Pygame小游戏】这款“吃掉一切”游戏简直奇葩了?通通都吃掉嘛?(附源码免费领)
![Luogu_ P1003 [noip2011 improvement group] carpet laying_ Violence enumeration](/img/65/413ac967cc8fc22f170c8c7ddaa106.png)
Luogu_ P1003 [noip2011 improvement group] carpet laying_ Violence enumeration

Li Chuang EDA learning notes 16: array copy and array distribution

#27ES6的数值扩展
随机推荐
LeetCode每日一练(主要元素)
国家食品安全风险评估中心:不要盲目片面追捧标签为“零添加”“纯天然”食品
一场分销裂变活动,不止是发发朋友圈这么简单!
Hung - Mung! HDD Hangzhou station · salon hors ligne vous invite à construire l'écologie
Hierarchical clustering and case analysis
郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
防火墙基础之源NAT地址转换和服务器映射web页面配置
数据中心表格报表实现定制统计加班请假汇总记录分享
锚文本大量丢失的问题
事务的隔离级别详解
Jialichuang EDA professional edition all offline client release
跨域图像的衡量新方式Style relevance:论文解读和代码实战
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
3.4 fixed number of cycles II
Weekly snapshot of substrate technology 20220411
List to table
Taishan Office Technology Lecture: the first difficulty is vertical positioning
等保2.0密码要求是什么?法律依据有哪些?
开源二三事|ShardingSphere 与 Database Mesh 之间不得不说的那些事
洛谷入门2【分支结构】题单题解