n(n<=1e18)
第n个数所在的那一层之前的所有层里共有多少个数
6
4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
//总结目前牛客问题 第一,没有循环输入问题, 第二 有循环输入问题, 第三 输入有多余空格问题 ,第四 中间插入多余空行问题 ....
namespace Test0001
{
class Program
{
public static void Main(string[] args)
{
string line;
while (!string.IsNullOrEmpty(line = Console.ReadLine())) Func(line);
}
public static void Func(string line)
{
long n = long.Parse(line);
if (n <= 2)
{
Console.WriteLine(Math.Max(n - 1, 0));
return;
}
long a = 1, b = 1;
while (b<=n)
{
long t = a;
a = b;
b += t;
}
Console.WriteLine(a-1);
}
}
}
斐波那契数 答案就是 当b>n时 前一个数(a)-1 并不需要什么记录层数啊,在拿去减,麻烦!
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ll n;
cin>>n;
if(n<=1){
cout<<0<<endl;
return 0;
}
if(n==2){
cout<<1<<endl;
return 0;
}
ll a=1, b=1;
ll s = a + b, t;
while(s < n){
t = a + b;
s += t;
a = b;
b = t;
}
cout<<s-t<<endl;
return 0;
} #include <bits/stdc++.h>
using namespace std;
int main()
{
long long n;
cin>>n;
if(n==0 || n==1)
{
cout<<0;
return 0;
}
if(n==2)
{
cout<<1;
return 0;
}
long long first=1,second=1;
long long sum=first+second;
long long cur=0;
for(int i=2;;i++)
{
cur=first+second;
sum+=cur;
first=second;
second=cur;
if(sum>=n)
{
cout<<sum-cur;
return 0;
}
}
}
#include <iostream>
using namespace std;
int main(){
long long n;
cin >> n;
if(n <= 1){
cout << 0 << endl; //第1个数之前 没有数
return 0;
}
long long i = 1;
long long j = 1;
long long sum = 1;
while(sum < n) {
j = i + j;
i = j - i;
sum += i;
}
cout << sum - i << endl;
return 0;
} import java.util.*;
public class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
while(sc.hasNextLong())
{
long n=sc.nextLong();
long sum=0;
long index=0;//第n个数所处的层数
for(long i=1;;i++)
{
sum+=fib(i);
if(sum>=n)//假如说累加和大于等于n了,说明此时的i就是第n个数所处的行数
{
index=i;
break;
}
}
long before=index-1;
long s=0;
for(long i=1;i<=before;i++)
{
s+=fib(i);
}
System.out.println(s);
}
}
private static long fib(long n)//斐波那契函数
{
long a=1;
long b=1;
for(long i=1;i<n-1;i++)
{
b=a+b;
a=b-a;
}
return b;
}
}
var line =readline();
var N=parseInt(line);
print(getNum(N))
function getNum(n){
if(n==1){
return 0
}else if(n==2){
return 1
}else{
var feibo=new Array();
feibo[0]=1;
feibo[1]=1;
var sum=feibo[0]+feibo[1];
for(var i=2;;i++){
feibo[i]=feibo[i-1]+feibo[i-2];
sum+=feibo[i];
if(sum>=n){
return 2*feibo[i-1]+feibo[i-2]-1
}
}
}
} #include<bits/stdc++.h>
using namespace std;
int main()
{
// 每个数字的个数是按斐波那契数列排的
// 用sum记录当前已经排了多少数字
// fibo记录下一个数字的个数
long long n,first=1,second=1,sum=2,fibo=2;
cin>>n;
if(n==1) {
cout<<0; return 0;
}
if(n==2){
cout<<1;return 0;
}
while(sum<n)
{
fibo = first + second;
sum += fibo;
first = second;
second = fibo;
}
cout<<sum-fibo<<endl;
return 0;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
long n = Long.parseLong(bf.readLine());
//one表示第n-1层的数字个数,two表示第n层的数字个数,sum表示第n层的总数字个数
//如果 sum + two的个数大于等于n,则前一层所有个数就是sum,直接return
long one = 0, two = 1, sum = 0;
while (sum < n) {
long temp = one + two;
one = two;
two = temp;
if (sum + one >=n) {
System.out.println(sum);
return;
} else {
sum += one;
}
}
}
}
n = int(input()) a,b,res,num = 0,1,0,0 while(num<n): res = a+b a=b b=res if num+a>=n: break else: num+=a print(num)
// 获取数字序列的第n个数所在的那一层之前的所有层里的数字个数之和
function getCount(n) {
let currentLayer = checkInWhichLayer(n);
if (currentLayer <= 1) {
return 0;
} else {
return getTotalLayerCount(currentLayer - 1);
}
}
// 获取某一层里的数字个数
function getLayerCount(layer) {
if (layer < 1) {
return;
} else if (layer === 1 || layer === 2) {
return 1;
} else {
return getLayerCount(layer - 1) + getLayerCount(layer - 2)
}
}
// 获取从第1层到第n层所有层里的数字个数总和
function getTotalLayerCount(layer) {
if (layer === 1) return 1;
if (layer === 2) return 2;
let sum = 0;
if (layer > 2) {
for(let i = 1; i <= layer; i++) {
sum += getLayerCount(i);
}
}
return sum;
}
// 检测第n个数在第几层
function checkInWhichLayer(n, layer = n) {
// console.log("n: " + n)
// console.log("layer: " + layer)
if (n === 1) return 1;
if (n > 1) {
if (n < getTotalLayerCount(layer)) {
// console.log("less than current layer count");
if (n > getTotalLayerCount(layer - 1)) {
// console.log("greater than previous layer count");
return layer;
} else if (n === getTotalLayerCount(layer - 1)){
// console.log("equal to previous layer count");
return layer - 1;
} else {
return checkInWhichLayer(n, layer - 1);
}
} else if (n === getTotalLayerCount(layer)) {
return layer;
}
}
}
随便用js写了下过程,js对于具体获取输入的数字,这个编辑器怎么处理机制不太懂,所以就不加了,用其他语言会比较好点function myF(n){var myArr = new Array();myArr[0] = 1;myArr[1] = 1;var sum = myArr[0] + myArr[1];for(var i=2;;i++){myArr[i] = myArr[i-1] + myArr[i-2];sum += myArr[i];if(sum >= n){return sum-myArr[i];}}}console.log(myF(6)) //输出为4