#include<iostream>
using namespace std;
int fact(int n)
{
if(n == 1)
return 1;
else
return n*fact(n-1);
}
int main(){
cout<<fact(4)<<endl;
return 0;
}
#include<iostream>
using namespace std;
int recur()
{
char c;
c = cin.get();
if(c != ‘\n‘)
recur();
cout<<c;
return 0;
}
int main()
{
recur();
return 0;
}
//若输入:abc \n
//打印顺序:\n cba
#include<iostream>
using namespace std;
int q(int n)
{
if(n == 0)
return 1;
else
return(n+q(n-1));
}
int main()
{
cout<<q(4)<<endl;
return 0;
}
//只需递推式和边际条件就能写出递归
递归与递推
用递归实现递推
优点
方法
把关注点放在要求解的目标上
从而
找到第n次做与第n-1次做之间的关系
确定第1次的返回结果
斐波那契数列
#include <iostream>
using namespace std;
int f(int n)
{
if(n==1)
return 1;
if(n==2)
return 2;
else
return(f(n-1)+f(n-2));
}
int main()
{
cout<<f(4)<<endl;
}
#include<iostream>
using namespace std;
void convert(int x)
{
if((x / 2) != 0)
{
convert(x / 2);
cout<<x % 2; //把cout放在递归调用之后,就可以逆序打印出来
}
else
cout<<x;
}
int main()
{
int x;
cin >> x;
convert(x);
return 0;
}
#include <iostream>
using namespace std;
void move(int m, char x, char y, char z) //将m个盘子从A经过B移动到C
{
if(m == 1)
{
cout<<"把一个盘子"从<<x<<"移动到"<<z<<endl;
}
else
{
move(m-1,x,z,y);
cout<<"把一个盘子"从<<x<<"移动到"<<z<<endl;
move(m-1,y,x,z);
}
}
int main()
{
int n;
cout<<"请输入盘数n=";
cin >> n;
cout<<"在三根柱子上移动"<<n<<"只盘的步骤为:"<<endl;
move(n,‘A‘,‘B‘,‘C‘);
return 0;
}
#include <iostream>
using namesapce std;
int count(int m,int n)
{
if( m<= 1 || n <=0) return 1;
if(m < n)
return count(m,m);
else
return count(m,n-1) + count(m-n,n)
}
void main()
{
int apples,plates;
cin >> apples >> plates;
cout << count(apples,plates);
}
#include <iostream>
using namespace std;
double notation()
{
char str[10];
cin >> str;
switch(str[0])
{
case ‘+‘ return notation()+notation();
case ‘-‘ return notation()-notation();
case ‘*‘ return notation()*notation();
case ‘/‘ return notation()/notation();
default: return atof(str);
}
}
int main()
{
cout<<notation();
return 0;
}
//注意这个题目的数据输入方式
#include<iostream>
using namespace std;
int i = 0;
char input[501];
int recur() {
char c = input[i];
i++;
if (c == ‘ ‘) {
return 1;//*句
}
if (c != ‘ ‘&&c != ‘\0‘) {
recur();
cout << c;
}
return 1; //**句
}
int main() {
cin.getline(input, 501);
while (input[i] != ‘\0‘) {
if (recur() == 1)//***句
cout << ‘ ‘;
}
if (input[i] == ‘\0‘) {
cout << endl;//最后输出换行符
return 0;
}
}
//详解:
//以输入‘Hello World‘为例,句***刚被执行时,i=0,当被执行的这次return返回来时,i=6;句***再次被执行时,i=7,当被执行的这次return返回来时,i=8(因为只取了一个空格)
//遇到第一个空格时*句的return内容被"吞"掉了,所以需要**句的存在来补上这一个return
//最终的输出是‘olleh dlrow‘,其中第一个空格来自**句,第二个空格来自*句
#include <iostream>
using namespace std;
int JGG(int x)
{
if(x == 1)
{
cout<<"End"<<endl;
return 0;
}
else if(x % 2 == 0)
{
cout << x << "/2="<< x/2<<endl;
JGG(x/2);
}
else
{
cout << x <<"*3+1="<< (x*3+1)<<endl;
JGG(x*3+1);
}
return 0;
}
int main()
{
int n;
cin >> n;
JGG(n);
return 0;
}
#include <iostream>
using namespace std;
char a[101]={‘\0‘};
int i=0,j=0;
int cut()
{
int movement = 0;
int flag = 0;
for (int i = 0; i < 101; i++) {
if (a[i] != ‘\0‘)
flag = 1;
}
if(flag == 0)
return 0;
for (int i = 0; i < 101; i++) {
if(a[i]!=‘\0‘)
{
for (int j = i+1; j < 101; j++) {
if(a[j] == ‘\0‘)
continue;
if(a[j] == a[i])
break;
if(a[j] != a[i])
{
cout<<i<<‘ ‘<<j<<endl;
a[i] = ‘\0‘;
a[j] = ‘\0‘;
movement = 1;
break;
}
}
if(movement == 1)
break;
}
}
flag = 0;
for (int i = 0; i < 101; i++) {
if (a[i] != ‘\0‘)
flag = 1;
}
if(flag == 1)
cut();
}
int main()
{
cin.getline(a,100,‘\n‘);
cut();
return 0;
}
#include <iostream>
#include <stack>
//双返回值的递归方法我实在有点看不懂(也不知道有没有非看懂不可的必要
//反正用堆栈就能很好的解决了……
using namespace std;
int main()
{
string str;
while(cin >> str) {
char simple[101];
for (int i = 0; i < str.length(); i++) {
if (str[i] == ‘(‘)
simple[i] = ‘$‘;
else if (str[i] == ‘)‘)
simple[i] = ‘?‘;
else simple[i] = ‘ ‘;
}
stack<char> s;
stack<int> index;
for (int i = 0; i < str.length(); i++) {
if (str[i] == ‘(‘) {
s.push(‘(‘);
index.push(i);
}
if (str[i] == ‘)‘) {
if (!s.empty() && s.top() == ‘(‘) //&&两边的两个条件必须且顺序不能改!!!!!
{
simple[index.top()] = ‘ ‘;
index.pop();
s.pop();
simple[i] = ‘ ‘;
} else {
s.push(‘)‘);
index.push(i);
}
}
}
cout << str << endl;
for (int i = 0; i < str.length(); i++) {
cout << simple[i];
}
cout<<‘\n‘;
}
return 0;
}
Coursera课程笔记----C程序设计进阶----Week 3
原文:https://www.cnblogs.com/maimai-d/p/12824965.html