当前位置:网站首页>【暑期每日一题】洛谷 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;
}边栏推荐
- PHP Warning: putenv() has been disabled for security reasons in phar
- SphereEx苗立尧:云原生架构下的Database Mesh研发实践
- MySQL(3)
- Leading the demand and justifying the HR value - the successful launch of the "Human Resource Leading Model HRLM"
- 两篇不错的php debug教程
- Node installation and environment configuration
- zabbix email alarm and WeChat alarm
- 一文搞懂C操作符
- Kind of weird!Access the destination URL, the host can container but not
- abaqus如何快速导入其他cae文件的assembly?
猜你喜欢

MySQL - Multi-table query and case detailed explanation

【21天学习挑战赛】顺序查找

武汉高性能计算大会2022举办,高性能计算生态发展再添新动力

node安装及环境变量配置

The nacos source code can not find the istio package

(笔记整理未完成)【图论】图的遍历

ASP.NET Core Web API 幂等性

MySQL Advanced Statements (1)

MySQL union query (multi-table query)

See the picture to understand | How to choose sales indicators to measure the health of business growth
随机推荐
MySQL (3)
pointer arithmetic in c language
Xgboost报错 ValueError: Invalid shape: (1650, 2) for label
Technology empowers Lhasa's "lungs", Huawei helps Lalu Wetland Smart Management to protect lucid waters and lush mountains
MySQL高级语句(一)
GCC编译器技术解析
[数据集][VOC]眼睛佩戴数据集VOC格式6000张
Connection reset by peer 问题解析
Expert Insights | 3 ways to seize innovation opportunities in a downturn
MySQL高阶---存储引擎、索引、锁
The second day HCIP
PMP新考纲通关秘籍,告别抓瞎
Specified URL is not reachable,caused by :‘Read timed out
Node installation and environment variable configuration
推出 Space On-Premises (本地部署版) Beta 版!
nacos源码启动找不到istio包
一文搞懂C操作符
关于ue4.27像素流送打包后的本地服务器问题
MySQL Advanced Statements (1)
odoo field 设置匿名函数domain