Search This Blog

Thursday, 25 August 2016

SPOJ - A very Easy Problem

SPOJ - A very Easy Problem 

Solution:-
/* You guys can use recursion/bits manipulation to come up with some other pattern. All are acceptable till they give the same number. */


137=2(2(2)+2+2(0))+2(2+2(0))+2(0)
1315=2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
73=2(2(2)+2)+2(2+2(0))+2(0)
136=2(2(2)+2+2(0))+2(2+2(0))
255=2(2(2)+2+2(0))+2(2(2)+2)+2(2(2)+2(0))+2(2(2))+2(2+2(0))+2(2)+2+2(0)
1384=2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2)+2(2(2)+2(0))+2(2+2(0))
16385=2(2(2+2(0))+2(2)+2)+2(0)

SPOJ - Happy Numbers Problem Solution

SPOJ - Happy Numbers Problem Solution

Solution:-

#include<bits/stdc++.h>
using namespace std;
#define ll long long

ll cal_num(ll num)
{
    int val;
    ll sum=0;
    while(num)
    {
        val=num%10;
        sum+=val*val;
        num/=10;
    }
    return sum;
}

int main()
{
    ll num;
    cin>>num;
    int count=0;
    while(1)
    {
        if(num==1)
        {
            cout<<count;
            break;
        }
        else if(num<10 && num!=7)
        {
            cout<<"-1";
            break;
        }
        num=cal_num(num);
        count++;
    }
    return 0;

}

SPOJ - Max Lines Problem Solution

SPOJ - Max Lines Problem Solution

Solution:-

#include<bits/stdc++.h>
using namespace std;

int main()
{
    cout<<fixed;
    cout<<setprecision(2);
    int tc;
    cin>>tc;
    for(int i=1;i<=tc;i++)
    {
        double r,max_line;
        cin>>r;
        max_line=(double)(4*r*r)+0.25;
        cout<<"Case "<<i<<": "<<max_line<<endl;
    }
    return 0;

}