I have been trying really hard to understand this question.
Question is:- In a mathematics class, Teacher ask Alice to find the number of all n digit distinct integers which is formed by the two distinct digits a and b but there is a rule to form n digit integer.
Rule: She has to form n digit integer by using two digits a and b without consecutive b.
Input Format:- The first line contains T, the number of test cases. Further T lines contains the value n which is the number of digit in the integer.
Code:-
#include<bits/stdc++.h>
using namespace std;
void classAssignment(int n,string ans, int &count){
if(ans.length()==n){
cout<<ans<<endl;
count++;
return;}
classAssignment(n,ans+"a",count);
if(ans.length()==0 || ans.at(ans.length()-1)!='b'){
classAssignment(n,ans+"b",count);}
}
int main() {
int t;
cin>>t;
for(int i=0;i<t;i++){
int count=0;
int n;
cin>>n;
classAssignment(n,"",count);
cout<<"Total paths are "<<count<<endl;}
return 0;}
Output:
aaa
aab
aba
baa
bab
Total paths are 5
Now I am unable to understand how this code generates this output?? How are these 2 recursive calls are working to get this output??