当前位置:网站首页>【暑期每日一题】洛谷 P1255 数楼梯
【暑期每日一题】洛谷 P1255 数楼梯
2022-08-02 06:07:00 【AC_Dragon】
题目链接:P1255 数楼梯 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
题目描述
楼梯有 N 阶,上楼可以一步上一阶,也可以一步上二阶。
编一个程序,计算共有多少种不同的走法。
输入格式
一个数字,楼梯数。
输出格式
输出走的方式总数。
样例 #1
样例输入 #1
4样例输出 #1
5提示
- 对于 60% 的数据,N <= 50;
- 对于 100% 的数据,1 <= N <= 5000。
AC code:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
string f(string a,string b)
{
if(a=="0" && b=="0")
{
return "0";
}
int t1=a.size();
int t2=b.size();
int n=abs(t1-t2);
for(int i=0;i<n;i++)
{
if(t1>t2)
b="0"+b;
else
a="0"+a;
}
string s; // 和
int c=0; // 进位
int d=0; // 低位
int len=a.size()-1;
for(int i=len;i>=0;i--)
{
d=(a[i]-'0'+b[i]-'0')%10+c;
if(d==10)
{
c=1;
s="0"+s;
continue;
}
s=to_string(d)+s;
c=(a[i]-'0'+b[i]-'0')/10;
if(c==1 && i==0)
s="1"+s;
}
if(s[0]=='0')
s="1"+s;
return s;
}
int main()
{
vector<string> a(1);
a.push_back("1");
a.push_back("2");
for(int i=3;i<=5000;i++)
{
string t=f(a[i-2],a[i-1]);
a.push_back(t);
}
int n;
cin>>n;
cout<<a[n];
return 0;
}边栏推荐
- At age 94, pioneer Turing award winner, computational complexity theory, Juris Hartmanis, died
- The nacos source code can not find the istio package
- PHP Warning: putenv() has been disabled for security reasons in phar
- Specified URL is not reachable,caused by :‘Read timed out
- Servlet
- awk语法-01-基础语法(命令、选项、内部变量)
- nodejs的安装和全局配置(超详细哦)
- Nacos installation detailed process
- postgres 多个变量填充字符串,字串格式化
- 专家见解|经济低迷期把握创新机会的 3 大方法
猜你喜欢

Kind of weird!Access the destination URL, the host can container but not

速看!PMP新考纲、PMBOK第七版解读

(部分不懂,笔记整理未完成)【图论】差分约束

数据库概论之MySQL表的增删改查1

Leading the demand and justifying the HR value - the successful launch of the "Human Resource Leading Model HRLM"

How does abaqus quickly import the assembly of other cae files?

看图就懂|衡量业务增长健康的销售指标如何选择

ASP.NET Core Web API 幂等性

GCC编译器技术解析

MySQL Advanced Statements (1)
随机推荐
Node installation and environment configuration
mysql索引失效的常见9种原因详解
两篇不错的php debug教程
MySQL高级语句(一)
Mining game (C language)
宝塔+FastAdmin 404 Not Found
Servlet
node安装及环境配置
chrome plugin development guide
推出 Space On-Premises (本地部署版) Beta 版!
有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
(笔记整理未完成)【图论】图的遍历
In-depth analysis of the initialization of member variables and local variables
Node installation and environment variable configuration
About the local server problem after ue4.27 pixel streaming package
HCIP 第一天
MySQL Advanced SQL Statements (2)
MySQL classic 50 practice questions and the most detailed analysis of the whole network
关于ue4.27像素流送打包后的本地服务器问题
【npm install 报错问题合集】- npm ERR! code ENOTEMPTY npm ERR! syscall rmdir