算法--360面试:使用递归实现:a0=1,a1=1;a2=a0+a1;a3=a1+a2...以此类推,求a30

这篇博客介绍了360面试中的一道算法题,要求使用递归计算数列a0=1,a1=1,后续项a(n)等于前两项之和。博客分别提供了逆向思维和正向思维两种解法,通过动态规划和递归计算得出a30的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Q题目

编程求解

使用递归实现:a0=1,a1=1;a2=a0+a1;a3=a1+a2;a4=a2+a3...以此类推,求a30

Answer解法

方式一:采用逆向思维

非常明显这是一道简单动态规划的题目,

从后往前逆向推理,递推公式如下:

An
#include <GL/glut.h> #include <stdio.h> #include <stdlib.h> #include <vector> using namespace std; struct Point { int x, y; }; Point pt[4],bz[11]; vector<Point> vpt; bool bDraw; int nInput; void CalcBZPoints() { float a0, a1, a2, a3, b0, b1, b2, b3; a0 = pt[0].x; a1 = -3 * pt[0].x + 3 * pt[1].x; a2 = 3 * pt[0].x - 6 * pt[1].x + 3 * pt[2].x; a3 = -pt[0].x + 3 * pt[1].x - 3 * pt[2].x + pt[3].x; b0 = pt[0].y; b1 = -3 * pt[0].y + 3 * pt[1].y; b2 = 3 * pt[0].y - 6 * pt[1].y + 3 * pt[2].y; b3 = -pt[0].y + 3 * pt[1].y - 3 * pt[2].y + pt[3].y; float t = 0; float dt = 0.01; for (int i = 0; t < 1.1; t += 0.1, i++) { bz[i].x = a0 + a1 * t + a2 * t * t + a3 * t * t * t; bz[i].y = b0 + b1 * t + b2 * t * t + b3 * t * t * t; } } void ControlPoint(vector<Point> vpt) { glPointSize(2); for (int i = 0; i < vpt.size(); i++) { glBegin(GL_POINTS); glColor3f(1.0f, 0.0f, 0.0f); glVertex2i(vpt[i].x, vpt[i].y); glEnd(); } } void PolylineGL(Point* pt, int num) { glBegin(GL_LINE_STRIP); for (int i = 0; i < num; i++) { glColor3f(1.0f, 1.0f, 1.0f); glVertex2i(pt[i].x, pt[i].y); } glEnd(); } void myDisplay() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 0.0f, 1.0f); if (vpt.size() > 0) { ControlPoint(vpt); } if (bDraw) { PolylineGL(pt, 4); CalcBZPoints(); PolylineGL(bz, 11); } glFlush(); } void Init() { glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_SMOOTH); printf("Please Click left button of mouse to Input control point of Bezier curve!\n"); } void myReshape(int w, int h) { glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h); } void myMouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { if (nInput == 0) { pt[0].x = x; pt[0].y = 480 - y; nInput = 1; vpt.clear(); vpt.push_back(pt[0]); bDraw = false; glutPostRedisplay(); } else if (nInput == 1) { pt[1].x = x; pt[1].y = 480 - y; vpt.push_back(pt[1]); nInput = 2; glutPostRedisplay(); } else if (nInput == 2) { pt[2].x = x; pt[2].y = 480 - y; vpt.push_back(pt[2]); nInput = 3; glutPostRedisplay(); } else if (nInput == 3) { pt[3].x = x; pt[3].y = 480 - y; bDraw = true; vpt.push_back(pt[3]); nInput = 0; glutPostRedisplay(); } } break; default: break; } } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(640, 480); glutCreateWindow("Hello World!"); Init(); glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape); glutMouseFunc(myMouse); glutMainLoop(); return 0; } 模仿上述代码,以(10,5,0)、(5,100)、(-5,15,0)、(-10-5,0)、(4-40)、(10, 5, 0)、 (5,100)、(-5,15,0)、(-10-5,0)、(10,5,0)为控制点,将其转变为B样条曲线生成算 法
05-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值