Saturday, September 16, 2017

Converting a number From One base to another

Converting a number From One base to another :



#include<bits/stdc++.h>
using namespace std;
int main()
{
    int base1,base2;
    int num;

    while(1){cout<<"Enter a number to convert(enter 0 to exit ): ";
    cin>>num;
    if(num==0)break;
    int x=num;
    cout<<"Enter the present base of this number: ";
    cin>>base1;
    cout<<"Enter a base in which you want to convert this number :";
    cin>>base2;
    int rem;
    int base=1;
    int num2=0;
    while(num>0)
    {
        rem=num%base2;
        num2=num2+rem*base;
        num=num/base2;
        base=base*base1;
    }
    cout<<x<<" after converted to  "<< base2 <<":"<<num2<<endl;
    cout<<"sajibtalukder2k16.blogspot.com"<<endl;}


}

 

 

 

 

 

 


Share:

The great 3n+1

UVa 100

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    while(scanf("%d%d",&a,&b)==2)
    {
        cout<<a<<" "<<b<<" ";
        if(a>b)
        {
            a=a+b;
            b=a-b;
            a=a-b;
            ///cout<<a<<" "<<b<<endl;
        }
        int i,j,maxx;
        j=1;
        maxx=0;
        for(i=a;i<=b;i++)
        {
            int m=i;
            while(m!=1)
            {
                if(m%2==0)m=m/2;
                else m=m*3+1;

                j++;
            }
            if(j>maxx)maxx=j;
            j=1;
        }
        cout<<maxx<<endl;
    }

}

 

 


 

Share:

Thursday, September 14, 2017

why this kolaverri spoj



Why this kolaveri di spoj:

 

 #include <cstdio>

using namespace std;

int main(){
    int T,n;

    scanf("%d",&T);

    while(T--){
        scanf("%d",&n);

        int ans = 0;

        for(int i = 2;i <= n;++i)
            ans = (ans + n + 1 - i) % i;

        printf("%d\n",ans + 1);
    }

    return 0;
}

Share: