itti_da
itti_da

Reputation: 65

Vector behaving wierdly

I was trying to find super lucky numbers that is numbers that contain only 4 and 7 as their digit (lucky number) and the number of 4's and 7's must be same (super lucky number). I used recursion to find the lucky numbers and stored them in vector. Then i looped over all lucky elements to check if it is super lucky. But while doing this when i was copying each vector element to another variable for checking its digits, the copy is not working properly. I don't know why it is copying wrong number . The code goes like:


#include<iostream>
#include<vector>
using namespace std;
vector<long long int> v;
void comb(long long int x){
    v.push_back(x);
    if(x<10000000){
        comb(x*10+4);
        comb(x*10+7);
    }
}
int main(){
    int n,f,s;
    long long int n1;
    cin>>n;
    comb(0);
    for(long long int i=1;i<v.size();i++){
        n1=v[i];                          //here i copied the element to variable
        f=0; 
        s=0;
        cout<<n1<<" ";        // But when i checked the value of variable it is not equal to element
        while(n1>0LL){
            if(n1%10LL==4LL){
                f++;
            }else s++;
            
            n1/=10LL;
        }
        if(f!=s){
            v.erase(v.begin()+i);
        }
        cout<<v[i]<<" "<<f<<" "<<s<<'\n';
    }
    return 0;
}

The output goes like:


4 44 1 0      // n1   v[i]    f    s
444 4444 3 0
44444 444444 5 0
4444444 44444444 7 0
44444447 4444447 7 1
44444474 44444477 7 1
444447 4444474 5 1
44444744 44444747 7 1
4444477 44444774 5 2
44444777 44447 5 3
444474 4444744 5 1
44447444 44447447 7 1
4444747 44447474 5 2
44447477 444477 5 3
4444774 44447744 5 2
44447747 4444777 5 3
44447774 44447777 5 3
4447 44474 3 1
444744 4447444 5 1
44474444 44474447 7 1
4447447 44474474 5 2
44474477 444747 5 3
4447474 44474744 5 2
44474747 4447477 5 3
44474774 44474777 5 3
44477 444774 3 2
4447744 44477444 5 2
44477447 4447747 5 3
44477474 44477477 5 3
444777 444777 3 3
4447774 44477744 4 3
44477747 44477747 4 4
4447777 44477774 3 4
44477777 447 3 5
4474 44744 3 1
447444 4474444 5 1
44744444 44744447 7 1
4474447 44744474 5 2
44744477 447447 5 3
4474474 44744744 5 2
44744747 4474477 5 3
44744774 44744777 5 3
44747 447474 3 2
4474744 44747444 5 2
44747447 4474747 5 3
44747474 44747477 5 3
447477 447477 3 3
4474774 44747744 4 3
44747747 44747747 4 4
4474777 44747774 3 4
44747777 4477 3 5
44774 447744 3 2
4477444 44774444 5 2
44774447 4477447 5 3
44774474 44774477 5 3
447747 447747 3 3
4477474 44774744 4 3
44774747 44774747 4 4
4477477 44774774 3 4
44774777 44777 3 5
447774 447774 3 3
4477744 44777444 4 3
44777447 44777447 4 4
4477747 44777474 3 4
44777477 447777 3 5
4477774 44777744 3 4
44777747 4477777 3 5
44777774 44777777 3 5
47 47 1 1
474 4744 2 1
47444 474444 4 1
4744444 47444444 6 1
47444447 4744447 6 2
47444474 47444477 6 2
474447 4744474 4 2
47444744 47444747 6 2
4744477 47444774 4 3
47444777 47444777 4 4
47447 474474 3 2
4744744 47447444 5 2
47447447 4744747 5 3
47447474 47447477 5 3
474477 474477 3 3
4744774 47447744 4 3
47447747 47447747 4 4
4744777 47447774 3 4
47447777 4747 3 5
47474 474744 3 2
4747444 47474444 5 2
47474447 4747447 5 3
47474474 47474477 5 3
474747 474747 3 3
4747474 47474744 4 3
47474747 47474747 4 4
4747477 47474774 3 4
47474777 47477 3 5
474774 474774 3 3
4747744 47477444 4 3
47477447 47477447 4 4
4747747 47477474 3 4
47477477 474777 3 5
4747774 47477744 3 4
47477747 4747777 3 5
47477774 47477777 3 5
477 4774 1 2
47744 477444 3 2
4774444 47744444 5 2
47744447 4774447 5 3
47744474 47744477 5 3
477447 477447 3 3
4774474 47744744 4 3
47744747 47744747 4 4
4774477 47744774 3 4
47744777 47747 3 5
..........................................

Why is this behaving like this?

Upvotes: 0

Views: 81

Answers (1)

MarkSouls
MarkSouls

Reputation: 999

You print n1 whatever value it is. Then if it isn't super lucky number, you remove v[i] which is same as n1 then print new v[i], which is the next value of original v[i].

Upvotes: 1

Related Questions