Blue Bridge Cup 2013 4th C language group B provincial exercise problem solution-exercise C. 39th step

Daily Questions (103)

39th step

Title description

Xiao Ming just finished watching the movie "The 39th Steps". When he left the cinema, he counted the number of steps in front of the auditorium, which happened to be 39!
Standing in front of the steps, he suddenly thought of another question:
If I only had one step at a time Can step up 1 or 2 steps. Take the left foot first, then alternate left and right, the last step is to take the right foot, which means that you have to take an even number of steps in total. So, how many different ways are there after 39 steps?
Please take advantage of the computer to help Xiao Ming find the answer.

Subject requirements

The required submission is an integer.
Note: Do not submit the answering process, or other supporting explanatory text.

Ideas

It's dfs again. . .

C++ code

#include<bits/stdc++.h>
using namespace std;

int ans = 0;
int sum = 0;
vector<int> a(40, 0);

void dfs(int steps)
{
    
    
	if(sum >= 39)
	{
    
    
		if((steps - 1) % 2 == 0 && sum == 39)
			ans++;
		return;
	}
	
	for(int i = 1; i <= 2; i++)
	{
    
    
		a[steps] = i;
		sum += i;
		dfs(steps + 1);
		sum -= i;
	}
}


int main()
{
    
    
	dfs(1);
	cout << ans << endl;
	return 0;
}

Operation result: The
Insert picture description here
answer is:
51167078

Turnip and greens have their own love (another C++ solution)

#include <iostream>
#include <algorithm>
using namespace std;
int cnt;
void dfs(int step,int sum){
    
    
	if(sum>=39){
    
    
		if(sum==39&&step%2==0) cnt++;//注意只有恰好sum==39符合偶数步才ok 
		return;
	}
	dfs(step+1,sum+1);
	dfs(step+1,sum+2);
}
int main() {
    
    
	dfs(0,0);
	cout<<cnt;
	return 0;
}

So, is this movie good? See you in the comments
Insert picture description here

If you like my article, please remember three consecutive times, like and follow the collection, every like and every one of your attention and every collection will be my infinite motivation on the way forward! ! ! ↖(▔▽▔)↗Thank you for your support, the next issue will be more exciting! ! !

Guess you like

Origin blog.csdn.net/qq_44631615/article/details/115027388