四个代码融合 依次:小青蛙上台阶 ;求阶乘;求最大公因数;地盘划分(均为递归算法)

发布时间 2023-11-04 09:36:37作者: 黛玉醉打将门神
小壁灯上楼梯 
#include <iostream>
using namespace std;
int a(int c){
    if(c<=2){
        return c;
    }else{
        return a(c-1)+(c-2);
    }
}
int main(int argc, char** argv) {
    int c,k;
    cin>>c;
    cout<<a(c);
    return 0;
}

  

求阶乘
#include <iostream> 
using namespace std;
int s(int n){
	if(n<=2){
		return n;
	}else{
		return n*s(n-1);
	}
} 
int main(){
    int n;
    cin>>n;
    cout<<s(n);
    return 0;
}

  

求最大公因数 
#include <iostream>
using namespace std;
int i(int w,int k){
    if(w%k==0){
        return k;
    }else{
        return i(k,w%k);
    }
    
}
int main(int argc, char** argv) {
    int w;
    int k;
    cin>>w>>k;
    cout<<i(w,k);
    return 0;
}

  

地盘划分
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
long long a,b,c,s=0;
int main(){
    scanf("%lld",&a);
    scanf("%lld",&b);
    while(a>1){
        if(b>a){c=a;a=b;b=c;}
        s+=a/b;
        a%=b;
    }
    if(a!=0){s+=b;}
    printf("%lld",s);
    return 0;
}