当前位置:网站首页>【暑期每日一题】洛谷 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;
}边栏推荐
- A detailed introduction to the deployment and usage of the Nacos registry
- request.getSession(), the story
- Technology empowers Lhasa's "lungs", Huawei helps Lalu Wetland Smart Management to protect lucid waters and lush mountains
- 文件上传漏洞(二)
- [npm install error report collection] - npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
- How does abaqus quickly import the assembly of other cae files?
- Specified URL is not reachable,caused by :‘Read timed out
- 2022年7月18日-7月31日(Ue4视频教程和文档,20小时。合计1412小时,剩8588小时)
- MySql - there is no insert, there is update or ignored
- Leading the demand and justifying the HR value - the successful launch of the "Human Resource Leading Model HRLM"
猜你喜欢

MySQL Advanced Statements (1)

Leetcode Weekly 304

The nacos source code can not find the istio package

zabbix auto-discovery and auto-registration

HCIP 第二天

Node installation and environment variable configuration

关于ue4.27像素流送打包后的本地服务器问题

MySQL Advanced - MVCC (ultra-detailed finishing)

MySQL 5.7 installation tutorial (full-step, nanny-level tutorial)

HCIP 第四天
随机推荐
MySQL经典50道练习题及全网最详细解析
MySQL Advanced SQL Statements
MySQL高级SQL语句
2022年8月计划,着重ue4视频教程
Node installation and environment configuration
node安装及环境配置
PMP新考纲考试内容介绍
npm ---- install yarn
APT + Transform to realize multi module Application distributed Application life cycle
Detailed explanation of 9 common reasons for MySQL index failure
Difference between npm and yarn
数据库概论之MySQL表的增删改查2
(部分不懂,笔记整理未完成)【图论】差分约束
Xgboost报错ValueError:无效的形状:标签(1650 2)
Nodejs installation tutorial
数据库概论之MySQL表的增删改查1
返回文件名问题
【npm install 报错问题合集】- npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
rhce homework