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
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