基础练习 Sine之舞 递归

题目链接

思路

规律的输出用递归。

code

#include <bits/stdc++.h>
using namespace std;
int x,y,n;//因为Sn和An是反着的,y要根据每次Sn递归的上界更改
void dfs(int n){
    if(n==0) return;
    cout<<"sin(";
    cout<<y-n+1;
    if(n>1) (y-n+1)%2?cout<<'-':cout<<'+';
    dfs(n-1);
    cout<<')';
}
void dfs1(int n){
    if(n==0) return;
    if(n>1) cout<<'(';
    y=x-n+1;
    dfs(x-n+1);
    cout<<'+'<<n;
    if(n>1)cout<<')';
    dfs1(n-1);
}
int main(){
    cin>>n;
    x=n;
    dfs1(n);
    return 0;
}

发布了83 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43077261/article/details/103730310