首页 > 编程语言 >c++递归

c++递归

时间:2024-08-05 13:56:44浏览次数:10  
标签:turtle bg word 递归 randint random c++ snakeList

这是我发的第一篇讲解类型的文章

主要是报的班那边讲到了个很有趣的东西 到时候会给些案例

本期直接把花絮挂在最后面

_____________________________________________________________________________

c++里有两种函数

一种是可以看成数据的(这种定义函数的类型有 long long,int,bool,char,float,double……)

实例:

#include<bits/stdc++.h>//万能头
using namespace std;//使用工具箱
bool zs(int num){//求质数 是为true 不是为false
	bool a=true;//先默认为是质数
	for(int i=2;i<num;i++){//从最小的质数2开始
		if(num%i==0){
            a=false;
        }//用枚举判断是否是合数 是就把bool类型的变量a改为false
	}
	return a;//传真 让使用这个函数的时候可以获取数据
}
int main(){//主函数
    int n;//声明变量n为int类型
    cin>>n;//输入变量n
    if(zs(n)){
        cout<<"是质数";
    }//如果n是质数就输出是质数
    else{
        cout<<"是合数";
    }
    return 0;
}

_____________________________________________________________________________

另一种类似于Python的def 用的是void

实例(把以前做的井字棋塞进去):

#include<bits/stdc++.h>
using namespace std;
void jzq(){
    //看不懂的可以去翻我写的那期c++井字棋
    cout<<1<<" "<<2<<" "<<3<<endl;
    cout<<4<<" "<<5<<" "<<8<<endl;
    cout<<7<<" "<<8<<" "<<9<<endl;
    cout<<endl;
    int a1[3]={4,4,4},a2[3]={4,4,4},a3[3]={4,4,4};
    for(int b=0;b!=9;b++){
    	int c;
        cout<<"你要下几号位:";
        cin>>c;
        cout<<endl;
        if(c<4){a1[c-1]=b%2;}
        else if(6<c){a3[c-7]=b%2;}
        else{a2[c-4]=b%2;}
        for(int i=0;i!=3;i++){
        	if(a1[i]==4){cout<<"  ";}
			else{cout<<a1[i]<<" ";}}
        cout<<endl;
        for(int i=0;i!=3;i++){
			if(a2[i]==4){cout<<"  ";}
			else{cout<<a2[i]<<" ";}}
		cout<<endl;
		for(int i=0;i!=3;i++){
			if(a3[i]==4){cout<<"  ";}
			else{cout<<a3[i]<<" ";}}
		cout<<endl;
		int t1=a1[0]+a1[1]+a1[2],t2=a2[0]+a2[1]+a2[2],t3=a3[0]+a3[1]+a3[2],t4=a1[0]+a2[0]+a3[0],t5=a1[1]+a2[1]+a3[1],t6=a1[2]+a2[2]+a3[2],t7=a1[0]+a2[1]+a3[2],t8=a1[2]+a2[1]+a3[0];
        if(t1==3){cout<<"“1”胜利";break;}
        else if(t1==0){cout<<"“0”胜利";break;}
        else if(t2==3){cout<<"“1”胜利";break;}
        else if(t2==0){cout<<"“0”胜利";break;}
        else if(t3==3){cout<<"“1”胜利";break;}
        else if(t3==0){cout<<"“0”胜利";break;}
        else if(t4==3){cout<<"“1”胜利";break;}
        else if(t4==0){cout<<"“0”胜利";break;}
        else if(t5==3){cout<<"“1”胜利";break;}
        else if(t5==0){cout<<"“0”胜利";break;}
        else if(t6==3){cout<<"“1”胜利";break;}
        else if(t6==0){cout<<"“0”胜利";break;}
        else if(t7==3){cout<<"“1”胜利";break;}
        else if(t7==0){cout<<"“0”胜利";break;}
        else if(t8==3){cout<<"“1”胜利";break;}
        else if(t8==0){cout<<"“0”胜利";break;}
        else{cout<<endl;continue;}}
    return 0;
}
int main(){
    jzq();
}

_____________________________________________________________________________

偷偷插点Python:

python里我只知道有个def可以定义函数

实例:

def fz(tx,bc):#定义函数fz
    for i in range(bc):#运行bc次
        for j in range(bc):#运行bc次
            print(tx,end=' ')#输出图形 用空格隔开
        print()#换行
fz("*",5)

_____________________________________________________________________________

现在进入正题:递归

递归就是在我讲的第一种定义函数方式里使用这个定义的函数

并且如果想退出 就要使用return 1或者return 0

c++可以用我讲的第一种定义函数方式做递归

下面就是一个输出斐波那契数列的实例

实例:

#include<bits/stdc++.h>
using namespace std;
int fi(int a){//输出斐波那契数列
	if(a==1||a==2){
        return 1;
    }//停止套用
	else{
        return fi(a-1)+fi(a-2);//传真
    }//递归简单来说就是在函数里套用函数
}
void fb(int n,int x){//输出n~x之间的斐波那契数列
	for(int i=1;i<x;i++){
		if(fi(i)<n){continue;}//如果没到n就继续
		if(fi(i)>x){break;}//如果超出x了就跳出循环
		if(fi(i)>n&&fi(i)<x){cout<<fi(i)<<" ";}//如果刚刚好 就输出
	}
}
int main(){
    int a,b;//声明变量a,b为int
    cin>>a>>b;//输入a,b
    fb(a,b);//a,b放进fb函数里
}

_____________________________________________________________________________

现在 你可以试试看使用递归了 考虑到你可能还不熟练

先做个简单的 就阶乘好了

想锻炼自己 就好好写代码 别看下面的题解

阶乘的定义:用n!表示 等于  n*(n-1)*(n-2)*……*2*1

输入案例:

输入6  输出720

输入5  输出120

题解:

#include<bits/stdc++.h>
using namespace std;
int jc(int n){
	if(n<1){return 1;}
	return n*jc(n-1);
}
int main(){
	int n;
    cin>>n;
	cout<<jc(n);

_____________________________________________________________________________

如果你写出来了 不妨写写阶乘和

阶乘和的定义:n!+(n-1)!+……+2!+1!

输入案例:

输入6 输出873

输入8 输出42633

题解:

#include<bits/stdc++.h>
using namespace std;
int jc(int n){
	if(n<1){return 1;}
	return n*jc(n-1);
}
int jch(int n){
    if(n<1){return 1;}
    return jc(n)*jc(n-1);
}
int main(){
	int n;
    cin>>n;
	cout<<jch(n);

如果你连这个都写出来了 那么就可以try try看这一道《牛的故事》了:

小牛要在四岁的时候生一头小牛 往后的每一年生一头,

这里假设每头牛都是母的

那么请制作一个输入在第几个年头 输出有几头牛的程序

输入案例:

输入7 输出6

输入15 输出129

题解我放到结尾了

_____________________________________________________________________________

题解:

#include<bits/stdc++.h>
using namespace std;
int sz(int n){
	if(n<4){return 1;}
	return sz(n-1)+sz(n-3);//今年的牛数相当于去年的加三年前的 不信你试试看
}
int main(){
	int n;
	cin>>n;
	cout<<sz(n);
}

哈哈 其实这就是结尾了awa

_____________________________________________________________________________

花絮:

如何卡爆你VX好友的手机或者电脑

教你一招 让VX好友的VX占用率拉满

只需一招:

import turtle,random
turtle.listen()
turtle.tracer(0)
def wmqs():
    for i in range(10):
        turtle.pencolor(random.randint(0,255),random.randint(0,255),random.randint(0,255))
        turtle.circle(random.randint(10,600),random.randint(10,360))
        turtle.onkeypress(wmqs,'s')
        def wmqs2():
            for i in range(10):
                turtle.pencolor(random.randint(0,255),random.randint(0,255),random.randint(0,255))
                turtle.circle(random.randint(10,600),random.randint(10,360))
                turtle.onkeypress(wmqs2,'s')
                def wmqs3():
                    for i in range(10):
                        turtle.pencolor(random.randint(0,255),random.randint(0,255),random.randint(0,255))
                        turtle.circle(random.randint(10,600),random.randint(10,360))
                        turtle.onkeypress(wmqs3,'s')
                        def wmqs4():
                            for i in range(10):
                                turtle.pencolor(random.randint(0,255),random.randint(0,255),random.randint(0,255))
                                turtle.circle(random.randint(10,600),random.randint(10,360))
                                turtle.onkeypress(wmqs4,'s')
while 1:
    import turtle
    turtle.onkeypress(wmqs,'s')

_____________________________________________________________________________

什么?

不想那么麻烦 我可你教你一个更简单的办法

找到我往期的贪吃蛇 然后改造一下:

import turtle,random,time;turtle.setup(610,610);a=turtle.Screen();a.bgcolor('white');turtle.tracer(False);bg=turtle.Turtle();bg.color('gray')
for i in range(31):bg.pu();bg.goto(-300,-300+i*20);bg.pd();bg.fd(600)
bg.left(90)
for j in range(31):bg.pu();bg.goto(-300+j*20,300);bg.pd();bg.bk(600)
turtle.update();bg.ht();turtle.tracer(False);snakeList,f=[],5
def eggtf():
    global eggx,eggy,egg;egg=turtle.Turtle();egg.pu();eggx,eggy=random.randint(-14,14),random.randint(-14,14);egg.goto(eggx*20,eggy*20);egg.shape('square')
for i in range(5):t=turtle.Turtle();t.shape('square');t.pu();t.goto(i*20,0);snakeList.append(t)
word=turtle.Turtle();word.ht()
def score(tf):
    if tf:global word;word.ht();word.clear()
    word=turtle.Turtle();word.pu();word.goto(-270,270);word.write('得分:'+str(f),font=('微软雅黑',18));word.ht()
def go(x1,y1):
    global f,g
    try:t=snakeList.pop();t.ht()
    except:pass
    t1=turtle.Turtle();t1.shape('square');snakeList.insert(0,t1);t1.pu();t1.goto(x1,y1);x2,y2=snakeList[0].pos()
    if snakeList[0].xcor()==eggx*20 and snakeList[0].ycor()==eggy*20:
        f+=1;score(True);snakeList.append('square');egg.ht();eggtf();t.shape('square')
    elif x2>300 or y2>300 or x2<-300 or y2<-300:
        for i in snakeList:i.reset()
    turtle.update()
eggtf();score(False)
def goLeft():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x-20,y)
def goUp():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x,y+20)
def goRight():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x+20,y)
def goDown():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x,y-20)
while 1;
    turtle.listen()
    turtle.onkeypress(goLeft,'Left')
    turtle.onkeypress(goRight,'Right')
    turtle.onkeypress(goDown,'Down')
    turtle.onkeypress(goUp,'Up')

此时 只需报上我的名号 然后把我的贪吃蛇包装成文件发给你的VX好友

这边给一下我以前写的贪吃蛇:

import turtle,random,time;turtle.setup(610,610);a=turtle.Screen();a.bgcolor('white');turtle.tracer(False);bg=turtle.Turtle();bg.color('gray')
for i in range(31):bg.pu();bg.goto(-300,-300+i*20);bg.pd();bg.fd(600)
bg.left(90)
for j in range(31):bg.pu();bg.goto(-300+j*20,300);bg.pd();bg.bk(600)
turtle.update();bg.ht();turtle.tracer(False);snakeList,f=[],5
def eggtf():
    global eggx,eggy,egg;egg=turtle.Turtle();egg.pu();eggx,eggy=random.randint(-14,14),random.randint(-14,14);egg.goto(eggx*20,eggy*20);egg.shape('square')
for i in range(5):t=turtle.Turtle();t.shape('square');t.pu();t.goto(i*20,0);snakeList.append(t)
word=turtle.Turtle();word.ht()
def score(tf):
    if tf:global word;word.ht();word.clear()
    word=turtle.Turtle();word.pu();word.goto(-270,270);word.write('得分:'+str(f),font=('微软雅黑',18));word.ht()
def go(x1,y1):
    global f,g
    try:t=snakeList.pop();t.ht()
    except:pass
    t1=turtle.Turtle();t1.shape('square');snakeList.insert(0,t1);t1.pu();t1.goto(x1,y1);x2,y2=snakeList[0].pos()
    if snakeList[0].xcor()==eggx*20 and snakeList[0].ycor()==eggy*20:
        f+=1;score(True);snakeList.append('square');egg.ht();eggtf();t.shape('square')
    elif x2>300 or y2>300 or x2<-300 or y2<-300:
        for i in snakeList:i.reset()
    turtle.update()
eggtf();score(False)
def goLeft():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x-20,y)
def goUp():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x,y+20)
def goRight():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x+20,y)
def goDown():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x,y-20)
turtle.listen();turtle.onkeypress(goLeft,'Left');turtle.onkeypress(goRight,'Right');turtle.onkeypress(goDown,'Down');turtle.onkeypress(goUp,'Up')

接着等TA把我的贪吃蛇玩爽了以后

删掉这条信息 把文件改为占用率拉满的贪吃蛇文件 你朋友再打开时就开心不起来了

当然 这也有个前提 就是对方得有Python编译器

_____________________________________________________________________________

你知道我为什么要发这篇文章吗

请看下文:

沃日你打把的


妈妈你说好什么事都可以沟通解决 但我的事凡牵扯点老妹 你就不乐意了

妈妈你说我钱花的多?

一年12个月 我妹妹每个月零花钱一般是6或者8开头的四位数

这让多少打工人望尘莫及啊

一个暑假报了二十几个班 花的钱当然多了


我作业本来就有那么多了 你还报班

我妹妹连报个画画班都累死了 妈妈你还说我很闲

我五升六暑假作业的事 请看VCR:

Such as(比如):

324页的金星数学全解

68页的抄写本

74页的语文阅读

78页的快乐暑假

3张语文试卷

背六年级上册所有的古诗以及要背的课文,文言文

60天的日记

8篇周记

两篇作文

20篇一起作业

我的问题 你会用我解决不了的事解决
我做不了的事 妈妈你会用更多我做不了的事来完成这一件我做不了的事

我做的有弊的事 你说我 我不在意了
我做的有利的事 你要么说是别人做的 要么不注意

别人对自己家做的有弊的事 你说我

我妹妹干有利的事 你使劲夸

我不管干没干 你先挑刺 挑不出来就假装看不到


要不是我数学三位数 你还刚喝完酒 你看到我语文很普通得给我干死

要不是我科学超常发挥了 你看到我英语在40个人的班里只有第十 我照样得被你干死

_____________________________________________________________________________

本期MVP结算:

本期字数:8858

本期质量分:86

标签:turtle,bg,word,递归,randint,random,c++,snakeList
From: https://blog.csdn.net/kgxgfrb/article/details/140923519

相关文章

  • 快速幂的模板和维持动态变化中的最大值最小值,以及常见递归
      快速幂和大根堆小根堆都是一些需要记忆的东西,方便后面在题目中实现应用。图中的最短路径两个常用算法:  prim算法:通过小根堆来实现的,小根堆的作用是用来时刻维持状态的最小值。kruskal算法:核心手段(并查集)然后一开始是打算学习01bfs的,发现对于递归的过程确实还是理......
  • c++中的sort
    前言Hello,大家好啊,我是文宇。正文sort函数是C++标准库提供的用于对数组或容器中的元素进行排序的函数。通过使用快速排序或其它高效的排序算法,sort函数能够以非常高效的方式对元素进行排序。sort函数用法灵活多样,可以对不同类型的元素进行排序,并且可以通过自定义比较函数或......
  • C++ 动态内存管理: `std::unique_ptr
    定义与头文件std::unique_ptr的功能定义于<memory>头文件中。它主要用于管理动态分配的内存,保证资源正确释放。函数模板std::make_unique非数组类型template<classT,class...Args>unique_ptr<T>make_unique(Args&&...args);C++14起用于构造非数组类......
  • C++递归找规律典型题:派蒙的问题
    题目描述:有一天,旅行者和派蒙来到了一个未知的世界。这个世界充满了奇幻的景色和令人迷失的迷宫。他们决定一起探索这个神秘的地方,希望寻找宝藏和解开谜题。当他们穿越迷宫的时候,他们突然来到了一个巨大的房间。房间的中央有一个巨大的石头柱子,上面镶嵌着奇怪的符号和图案,上面......
  • c++递归算法较难题:分解数字
    题目描述:输入自然数 n,然后将其分拆成由若干数相加的形式,参与加法运算的数可以重复,要求输出降序排列。输入描述:一个待拆分的自然数n,(n≤50) 。输出描述:若干个拆分的加法等式。样例输入:5样例输出:5=55=4+15=3+25=3+1+15=2+2+15=2+1+1+15=1+1+1+1+1题目思想:将要分......
  • C++回溯算法经典例题:四皇后问题
    问题简介:在一个4×4的棋盘上,任意两个皇后都不能处在同一行、同一列任意两个皇后都不能处在同一斜线上(主斜线、反斜线)。题目分析:1.假设第一个皇后在(1,1):    1)在x=3时会卡死            2)在x=4时会卡死        2.假设第一个皇后在(2,1): ......
  • C++简单模拟:电梯问题
    问题描述:某城市最高建筑物只有一个电梯,一个请求列表是由 n 个正整数组成的。数字表示电梯将停在哪个楼层。电梯向上移动一层需要 6 秒,向下移动一层需要 4 秒。电梯每次停下会停留 5 秒,对于给定的请求列表,需要计算用于满足列表中所有请求的总时间。电梯开始时在第一层......
  • C++商店管理系统
    代码中使用了C++11的特性后面有些输出(cout输出的)的提示文本是英文,因为懒得敲中文源码在最后面文末投票参与一下谢谢商品数据保存在items.txt用户数据保存在users.txt实现功能1.添加商品(商品ID,商品名,库存数量,价格)填写完信息后询问是否执行2.修改商品(先提示原有信......
  • SpringBoot-书店信息管理系统+93494(免费领源码+开发文档)可做计算机毕业设计JAVA、PHP
    基于springboot书店信息管理系统摘 要书店信息管理系统采用B/S结构、java开发语言、以及Mysql数据库等技术。系统主要分为管理员和用户两部分,管理员管理主要功能包括:首页、轮播图、公告栏、资源管理(图书资讯、资讯分类)交流管理(留言板、留言板分类)系统用户(管理员、顾客用户......
  • 【每日一题】【DFS】【试除法求约数】【大剪枝】清楚姐姐跳格子 牛客周赛 Round 54 D
    牛客周赛Round54D题清楚姐姐跳格子题目背景牛客周赛Round54题目描述样例#1样例输入#1523154样例输出#12做题思路首先知道ai......