寒假1 蓝桥杯-递推递归

寒假1 蓝桥杯-递推递归_第1张图片

#include
#include
using namespace std;

int f(int n)
{
	if (n == 1)return 1;
	if (n == 2)return 1;
	return f(n - 1) + f(n - 2);
}

int main()
{
	int n;
	cin >> n;
	cout << f(n) << endl;
	return 0;
}

寒假1 蓝桥杯-递推递归_第2张图片

#include
#include
using namespace std;
int n;
int arr[16];
void dfs(int u)
{
	if (u > n)
	{
		for (int i = 1;i <= n;i++)
		{
			if (arr[i] == 1)
			{
				cout << i << " ";
			}
			
		}
		cout << endl;
		return;
	}
	arr[u] = 2;
	dfs(u + 1);
	arr[u] = 1;
	dfs(u + 1);
}
int main()
{
	cin >> n;
	dfs(1);
	return 0;
}

寒假1 蓝桥杯-递推递归_第3张图片

#include
using namespace std;
#include
int n;
int arr[10];
int arr2[10];//1没用过,2用过
vectorv;
void dfs(int u)
{
	if (u > n)
	{
		for (int i = 1;i <= n;i++)
		{
			cout << arr[i]<<" ";
		}
		cout << endl;
	}
	for (int i = 1;i <= n;i++)
	{
		if (arr2[i] == 1)
		{
			arr[u] = i;
			arr2[i] = 2;
			dfs(u + 1);
			arr2[i] = 1;
		}
	}
}
int main()
{
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		arr2[i] = 1;
	}
	dfs(1);
	return 0;
}

寒假1 蓝桥杯-递推递归_第4张图片

#include
using namespace std;
int arr[26];
int use[26];//1没用过,2用过
int n, k;

void dfs(int u)
{
	
	if (u > k)
	{
			for (int i = 1;i <= k;i++)
			{
				cout << arr[i] << " ";
			}
			cout << endl;
		return;
	}
	for (int i = 1;i <= n;i++)
	{
		if (use[i] == 1 && i > arr[u - 1])
		{
			arr[u] = i;
			use[i] = 2;
			dfs(u + 1);
			use[i] = 1;
		}
	}
	
}

int main()
{
	cin >> n >> k;
	for (int i = 1;i <= n;i++)
	{
		use[i] = 1;
	}
	dfs(1);
	return 0;
}

 寒假1 蓝桥杯-递推递归_第5张图片

#include
using namespace std;

long long dfs(int n)
{
    if(n==1)return 1;
    return n*dfs(n-1);
}

int main()
{
    int n;
    cin>>n;
    cout<

 寒假1 蓝桥杯-递推递归_第6张图片

#include
using namespace std;
#include
int arr[10];
int arr2[10];
int test[10];
bool use[10];
int n;

void dfs(int u)
{
    if (u > n)
    {
       /* int flag = 1;
        for (int i = 1;i <= n;i++)
        {
            if (arr2[i] != test[i])
            {
                flag = 0;
            }
            test[i] = arr2[i];
        }
        if (flag == 0)
        {*/
            for (int j = 1;j <= n;j++)
            {
                cout << arr2[j] << " ";
            }
            cout << endl;
        //}
        return;
    }
    for (int i = 1;i <= n;i++)
    {
        if (!use[i])
        {
            arr2[u] = arr[i];
            use[i] = true;
            dfs(u + 1);
            use[i] = false;
            while (i + 1 <= n && arr[i + 1] == arr[i]) i++;
        }
    }
}
int main()
{
    cin >> n;
    for (int i = 1;i <= n;i++)
    {
        cin >> arr[i];
    }
    sort(arr+1, arr + n+1);
    /*for (int i = 1;i <= n;i++)
    {
        cout << arr[i];
    }*/
    dfs(1);
    return 0;
}

寒假1 蓝桥杯-递推递归_第7张图片

你可能感兴趣的:(蓝桥杯,职场和发展)