Find the Minimum Number of Fibonacci Numbers Whose Sum Is K(C++和为 K 的最少斐波那契数字数目)

解题思路:

(1)先求出小于等于k的最大斐波那契数列

(2)从后往前依次判断

class Solution {
public:
    int findMinFibonacciNumbers(int k) {
        vector<int> v(2,1);
        int a=1,b=1,c=0;
        while(c<k) {
            c=a+b;
            v.push_back(c);
            a=b;
            b=c;
        }
        int count=0,i=v.size()-1;
        while(i>=0) {
            if(v[i]<=k) {
                count++;
                k-=v[i];
                if(k==0) return count;
            } else i--;
        }
        return count;
    }
};

猜你喜欢

转载自blog.csdn.net/coolsunxu/article/details/114689927
今日推荐