LightOj 1042 Secret Origins







#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int i,j,k,l,n,m,a[100000],d,b[100000],sum;
    string s,st;
    vector<string>v;
    //freopen("1042.txt","r",stdin);
    //freopen("1042out.txt","w",stdout);
    cin>>n;
    for(i=1;i<=n;i++)
    {
        sum=0;
        cin>>m;
        s.clear();
        st.clear();
        j=0;
        while(m!=0)
        {
            a[j]=m%2;
            m=m/2;
            j++;
        }
        a[j]=0;
        for(k=j;k>=0;k--)
        {
            st=a[k]+'0';
            s=s+st;
        }
        
        next_permutation(s.begin(),s.end());
        

        d=s.length()-1;
        for(l=0;l<s.length();l++)
        {
            b[l]=s[l]-'0';
            if(b[l]==1)
            {
                sum=sum+pow(2,d);
            }
            d--;


        }

        cout<<"Case "<<i<<": ";
        cout<<sum<<endl;

    }
    return 0;
}

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment