news 2026/5/11 16:41:21

程序设计基础(二)递推

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
程序设计基础(二)递推

1.母牛的故事

#include<bits/stdc++.h> using namespace std; #define endl '\n' #define INF ((int)1e18) typedef long long ll; const int N=60; int n; ll a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n; a[1]=1; a[2]=2; a[3]=3; for(int i=4;i<=n;i++){ a[i]=a[i-1]+a[i-3]; } cout<<a[n]<<endl; return 0; }

2.养兔子

#include<bits/stdc++.h> using namespace std; #define endl '\n' #define INF ((int)1e18) typedef long long ll; const int N=100; int n; ll a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n; a[1]=1; a[2]=2; for(int i=3;i<=n;i++){ a[i]=a[i-1]+a[i-2]; } cout<<a[n]; return 0; }

3.骨牌铺方格

#include<bits/stdc++.h> using namespace std; #define endl '\n' #define INF ((int)1e18) typedef long long ll; const int N=55; int n; ll a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n; a[1]=1; a[2]=2; for(int i=3;i<=n;i++){ a[i]=a[i-1]+a[i-2]; } cout<<a[n]<<endl; return 0; }

4.马拦过河卒

#include<bits/stdc++.h> using namespace std; #define endl '\n' #define INF ((int)1e18) typedef long long ll; const int N=1e5+10; const int mod=998244353; int x,y; int m,n; vector<vector<ll>> a(30,vector<ll>(30,1)); int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>m>>n>>x>>y; int dx[]={2,1,-1,-2,2,1,-1,-2}; int dy[]={1,-2,-2,-1,-1,2,2,1}; a[x][y]=0; for(int i=0;i<8;i++){ int nx=x+dx[i]; int ny=y+dy[i]; a[nx][ny]=0; } if(a[0][0]==0) cout<<"0"<<endl; else{ for(int i=0;i<=m;i++){ for(int j=0;j<=n;j++){ if(i==0&&j==0) continue; if(a[i][j]==0) continue; if(i==0) a[i][j]=a[i][j-1]; else if(j==0) a[i][j]=a[i-1][j]; else{ a[i][j]=a[i-1][j]+a[i][j-1]; } } } cout<<a[m][n]<<endl; } return 0; }

5.黄金时代

#include<bits/stdc++.h> using namespace std; #define endl '\n' #define INF ((int)1e18) typedef long long ll; const int N=35; int n; int a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n; a[1]=5; for(int i=2;i<=n;i++){ a[i]=round(a[i-1]/0.6180339887); } cout<<a[n]<<endl; return 0; }

6.爬楼梯

#include<bits/stdc++.h> using namespace std; #define endl '\n' #define INF ((int)1e18) typedef long long ll; const int N=55; int n; ll a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n; a[1]=1; a[2]=2; for(int i=3;i<=n;i++){ a[i]=a[i-1]+a[i-2]; } cout<<a[n]<<endl; return 0; }

7.三国佚事--巴蜀之危

#include<bits/stdc++.h> using namespace std; #define endl '\n' #define INF ((int)1e18) typedef long long ll; const int N=55; int n; ll a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n; a[1]=0; a[2]=1; a[3]=2; for(int i=4;i<=n;i++){ a[i]=(i-1)*(a[i-1]+a[i-2]); } cout<<a[n]<<endl; return 0; }

8.王小二切饼

#include<bits/stdc++.h> using namespace std; #define endl '\n' #define INF ((int)1e18) typedef long long ll; const int N=105; int n; ll a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n; a[1]=2; for(int i=2;i<=n;i++){ a[i]=i+a[i-1]; } cout<<a[n]<<endl; return 0; }

9.蟠桃记

#include<bits/stdc++.h> using namespace std; #define endl '\n' #define INF ((int)1e18) typedef long long ll; const int N=35; int n; ll a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n; a[1]=1; for(int i=2;i<=n;i++){ a[i]=(a[i-1]+1)*2; } cout<<a[n]<<endl; return 0; }

10.拍皮球

#include<iostream> #include<string> #include<algorithm> #include<cmath> #include<iomanip> using namespace std; int main() { double h; int n; cin >> h >> n; double a[n+1]; double sum; a[0] = h; for (int i = 1; i <= n; i++) { a[i] = a[i-1] / 2.0; } if (n == 1) { sum = h; } else { sum = h; for (int i = 1; i < n; i++) { sum += a[i] * 2.0; } } cout << fixed << setprecision(2) << sum << " " << a[n]; return 0; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/11 16:39:51

告别手动修复!用Word宏自动化解决多级列表编号不显示的顽疾

告别手动修复&#xff01;用Word宏自动化解决多级列表编号不显示的顽疾 每次打开几十页的技术文档&#xff0c;发现精心设置的多级列表编号突然"消失"&#xff0c;只能机械地重复CtrlShiftS——这场景对文档工程师来说再熟悉不过。更令人崩溃的是&#xff0c;这个临…

作者头像 李华
网站建设 2026/5/11 16:38:47

CentOS 7 无线网卡驱动安装全攻略:从内核升级到驱动加载

1. 为什么你的CentOS 7认不出无线网卡&#xff1f; 刚装好CentOS 7准备连WiFi时&#xff0c;发现系统根本不识别无线网卡&#xff1f;这种情况我见过太多新手遇到。根本原因在于&#xff1a;CentOS 7默认内核版本&#xff08;3.10&#xff09;太老旧&#xff0c;对新型无线网卡…

作者头像 李华
网站建设 2026/5/11 16:36:43

Libre Barcode:颠覆传统条码生成的开源字体解决方案

Libre Barcode&#xff1a;颠覆传统条码生成的开源字体解决方案 【免费下载链接】librebarcode Libre Barcode: barcode fonts for various barcode standards. 项目地址: https://gitcode.com/gh_mirrors/li/librebarcode 在当今数字化时代&#xff0c;条码作为信息传递…

作者头像 李华
网站建设 2026/4/9 18:08:59

Libtorch库安装

libtorch是pytorch的C库&#xff0c;常用于pytorch模型的C部署。 1、cuda安装 参考&#xff1a; windows/linux安装NVIDIA驱动&#xff08;cuda加速&#xff09;-CSDN博客https://blog.csdn.net/m0_69115733/article/details/159944127?spm1001.2014.3001.5502 2、libtorch…

作者头像 李华
网站建设 2026/4/9 18:04:45

在RK3588开发板上搞定FPGA的PCIe通信:XDMA驱动编译与加载避坑实录

在RK3588开发板上实现FPGA的PCIe通信&#xff1a;XDMA驱动全流程实战指南 当RK3588遇上FPGA&#xff0c;PCIe通信便成为两者之间高速数据交互的黄金通道。不同于传统嵌入式总线&#xff0c;PCIe协议栈的复杂性常让开发者陷入驱动适配与硬件调测的泥潭。本文将手把手带您穿越从源…

作者头像 李华