题解
2024-05-04 19:08:53
发布于:广东
9阅读
0回复
0点赞
使用栈,栈有先进后出的特性,我们就可以从大到小入栈,再从小到大出栈
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
int a[55];
void _init(){
a[0] = 1;
for(int i = 1; i <= 45; i++){
a[i] = a[i - 1] + a[i - 2];
}
}
int main(){
_init();
int t, n;
cin >> t;
while(t--){
stack <int> s;
cin >> n;
printf("%d=", n);
for(int i = 45; i >= 1; i--){//从大到小遍历
while(n >= a[i]){
s.push(a[i]);//入栈
n -= a[i];
}
}cout << s.top();//第一个不用写+
s.pop();
while(!s.empty()){//出栈
printf("+%d", s.top());
s.pop();
}cout << endl;
}
return 0;
}
时间复杂度:(不知道的一律写logn)
这里空空如也
有帮助,赞一个