Split Array into Fibonacci Sequence

本文介绍了一种算法,用于将给定的数字字符串拆分为满足斐波那契数列特性的序列。通过递归方法检查所有可能的分割方式,确保每个数字不超过32位整数的范围,并且不包含前导零。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

Given a string S of digits,such as S = "123456579",we can split it into a Fibonacci-likesequence [123, 456, 579].

Formally, a Fibonacci-like sequence is a list F of non-negative integers such that:
1.0 <= F[i] <= 2^31 - 1, (that is, each integer fits a 32-bit signed integer type);
2.F.length >= 3;
3.and F[i] + F[i+1] = F[i+2] for all 0 <=i< F.length - 2.

Also, note that when splitting the string into pieces, each piece must not have extra leading zeroes,except if the piece is the number 0 itself.

Return any Fibonacci-like sequence split from S, or return [] if it cannot be done.

Example 1:
Input: "123456579"
Output: [123,456,579]

Example 2:
Input: "11235813"
Output: [1,1,2,3,5,8,13]

Example 3:
Input: "112358130"
Output: []
Explanation: The task is impossible.

Example 4:
Input: "0123"
Output: []
Explanation: Leading zeroes are not allowed, so "01", "2", "3" is not valid.

Example 5:
Input: "1101111"
Output: [110, 1, 111]
Explanation: The output [11, 0, 11, 11] would also be accepted.

Note:
1.1 <= S.length <= 200
2.S contains only digits.

思路:这道题完全和Additive Number一样,不过在截取数字的时候要注意题目中对斐波那契数的范围要求。

#include<cstdio>
#include<string>
#include<vector>
#include<stdlib.h>
using namespace std;

bool split(string S,vector<int>& nums,int count,int start)
{
	if(count==S.length())
	{
		if(nums.size()>2)	
	        return true;		    
		else
		    return false;
	}
	else
	{ 
		for(int i=1;i+start<=S.length();i++)
		{
			int number = atoi(S.substr(start,i).c_str());
			//防止分隔成的数超过int的最大值 
			if(number<0)
			    return false;
			//分割成的数字不能以0开头 
			if(number!=0&&atoi(S.substr(start,1).c_str())==0)
			    return false;    
			if(nums.size()>=2)
			{
				int num1 = nums[nums.size()-1];
				int num2 = nums[nums.size()-2];				
				if(number!=num1+num2)
					continue;
			}    		
			nums.push_back(number);
			if(split(S,nums,count+i,start+i))
			    return true;
			nums.pop_back();
		}
		return false;		
	}
}
vector<int> splitIntoFibonacci(string S) 
{
	vector<int> result;
	if(S.length()<3)
	    return result;
	split(S,result,0,0);    
	return result;		        
}

int main(){
	string S = "214748364721474836422147483641";
	vector<int> result = splitIntoFibonacci(S);
	printf("size()=%d\n",result.size());
	for(int i=0;i<result.size();i++)
	    printf("%d ",result[i]);
	printf("\n");    
	return 0;
} 

 

### 斐波那契数列算法的C/C++ 实现 斐波那契数列是一个经典的递推关系问题,在编程中经常被用来作为教学例子。下面提供两种常见的方法来计算斐波那契数列:迭代法和递归法。 #### 迭代方式实现 通过循环结构可以有效地减少重复计算,提高效率。此方法的空间复杂度为O(1),时间复杂度为O(n): ```cpp #include <iostream> // 计算第n项斐波那契数值 long long fibonacci(int n) { if (n <= 0) return 0; if (n == 1) return 1; long long prev = 0, curr = 1; for (int i = 2; i <= n; ++i) { long long next = prev + curr; prev = curr; curr = next; } return curr; } int main() { int num; std::cout << "Enter the position in Fibonacci sequence: "; std::cin >> num; std::cout << "Fibonacci number at position " << num << ": " << fibonacci(num) << '\n'; } ``` #### 递归方式实现 虽然简单易懂但是性能较低效,因为存在大量的冗余计算。该版本的时间复杂度大约是指数级增长即O(φ^n),其中φ≈1.618... 是黄金比例[^4]。 ```cpp #include <iostream> // 使用递归来求解斐波那契数列中的某一项 unsigned long long fib_recursive(unsigned int n){ if (n<=1) return n; else return fib_recursive(n-1)+fib_recursive(n-2); } int main(){ unsigned int pos; std::cout<<"请输入要查询的位置:"; std::cin>>pos; std::cout<<pos<<"位置处的斐波那契值为:"<<fib_recursive(pos)<<std::endl; return 0; } ``` 对于实际应用而言,建议采用迭代的方法或者记忆化技术优化后的递归形式以获得更好的执行速度与资源利用率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值