题解 | #小乐乐走台阶#
小乐乐走台阶
https://www.nowcoder.com/practice/ebf04de0e02c486099d78b7c3aaec255
#include <iostream>
using namespace std;
int func(int n)
{
int res;
if (n == 1)
res = 1;
else if (n == 2)
res = 2;
else
{
int first = 1;
int second = 2;
while (n != 2)
{
res = first + second;
first = second;
second = res;
n--;
}
}
return res;
}
int main() {
int n;
cin >> n;
int num = func(n);
cout << num;
}
// 64 位输出请用 printf("%lld")
递归超时了,用的循环
C++题解 文章被收录于专栏
记录在牛客网用C++刷题的题解思路