Wednesday, August 31, 2016

UVA 10235 - Simply Emirp solution

problem link-click here

it's an easy implement based problem....please follow the statement given in the question.

an accepted output is given below:

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

int main()
{
    long long int num,i,m,rev,r,p,tmp,j;

    while(cin>>num)
    {
        m=0;
        tmp=num;

        for(i=2;i<=pow(num,0.5);i++)
        {
            if(num%i==0)
            {
                m=1;
                break;
            }

        }

        if(m==0)
        {
            rev=0;
            p=0;

            while(num)
            {
            r=num%10;
            rev=rev*10+r;
            num=num/10;
            }


            for(j=2;j<=pow(rev,0.5);j++)
            {
                if(rev%j==0)
                {
                    p=1;
                    break;
                }
            }

            if(p==0 && tmp!=rev)
            {
                cout<<tmp<<" is emirp."<<endl;
            }

            else
               cout<<tmp<<" is prime."<<endl;
        }

        else
            cout<<tmp<<" is not prime."<<endl;
    }

    return 0;
}

Sunday, August 28, 2016

UVA 499 - What's The Frequency, Kenneth? solution

problem link-click here

it's an easy implement based string processing problem. just read the question statement carefully.

an accepted code is given below:

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

int main()
{
    string str;

    while(getline(cin,str))
    {
        bool arr[150]={0};
        vector<int> v;
        vector<char> v1;

        for(int i=0;i<str.length();i++)
        {
            int c=0;

            if(arr[str[i]]==0 && ((str[i]>=65 && str[i]<=90) || (str[i]>=97 && str[i]<=122)) )
            {
                for(int j=i;j<str.length();j++)
                {
                    if(str[i]==str[j])
                    {
                        c++;
                    }
                }

                arr[str[i]]=1;
                v.push_back(c);
                v1.push_back(str[i]);
            }
        }
        for(int i=0;i<v.size()-1;i++)
        {
            for(int j=i+1;j<v.size();j++)
            {
                if(v[i]>v[j])
                {
                    int temp=v[i];
                    v[i]=v[j];
                    v[j]=temp;

                    char temp1=v1[i];
                    v1[i]=v1[j];
                    v1[j]=temp1;
                }
            }
        }


        bool arr1[150]={0};
        vector<char> v2;

        arr1[v[v.size()-1]]=1;

        for(int i=0;i<v.size();i++)
        {
            if(arr1[v[i]]==1)
            {
                v2.push_back(v1[i]);
            }
        }

        sort(v2.begin(),v2.end());

        for(int i=0;i<v2.size();i++)
        {
            cout<<v2[i];
        }

        cout<<" "<<v[v.size()-1]<<endl;

    }

    return 0;
}

Saturday, August 27, 2016

UVA 10490 - Mr. Azad and his Son!!!!! solution

problem link-click here

solving this problem i have learnt a new rule which is proved by Euclid, a well-known mathematician. the rule is that  2^(p-1)(2^p − 1) is a perfect number whenever 2^p − 1 is prime

where p is an integer.


for more information regarding perfect numbers have a look here,
https://en.wikipedia.org/wiki/Perfect_number#Even_perfect_numbers


we need to precalculate power of 2 till 31 which means till 2^31 and store them in a vector array. one important thing is use unsigned long long here to get desired result.


an accepted output is given below:


#include<bits/stdc++.h>

#define s 33

using namespace std;


bool arr[s];

vector<long long int> v;


void sieve()

{

for(int i=2;i*i<=s;i++)

{

if(arr[i]==0)

{

for(int j=2;i*j<=s;j++)

{

arr[i*j]=1;

}

}

}

}


void power()

{

v.push_back(0);

v.push_back(2);


for(int i=2;i<=31;i++)

{

v.push_back(v[i-1]*2);

}

}


bool isprime(int n)

{


for(int i=2;i<=pow(n,0.5);i++)

{

if(n%i==0)

{

return false;

}

}


return true;

}


int main()

{

sieve();

power();


int n;


while(cin>>n && n!=0)

{

unsigned long long int a;


a=v[n-1]*(v[n]-1);


if(isprime((v[n]-1)))

{

cout<<"Perfect: "<<a<<"!"<<endl;

}

else if(arr[n]==0 && !isprime((v[n]-1)))

{

cout<<"Given number is prime. But, NO perfect number is available."<<endl;

}

else

{

cout<<"Given number is NOT prime! NO perfect number is available."<<endl;

}

}


return 0;
}

Friday, August 26, 2016

UVA 11900 - Boiled Eggs solution

problem link-click here

nothing to say about this problem. just read the given statement very carefully and solve it. it's fully implemented based problem along with super easy.

an accepted code is given below:

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

int main()
{
    int t,p,q,n,i=1;

    scanf("%d",&t);

    while(t--)
    {
        int arr[100]={0},j=0;
        scanf("%d%d%d",&n,&p,&q);

        while(j<n)
        {
            scanf("%d",&arr[j]);
            j++;
        }

        sort(arr,arr+n);

        int sum=0,c=0;

       for(int k=0;k<n;k++)
       {
           sum=sum+arr[k];

           if(sum<=q)
           {
               c++;
           }
           else
           {
               break;
           }

           if(c>=p)
           {
               break;
           }
       }

        printf("Case %d: %d\n",i,c);

        i++;
    }

    return 0;
}

UVA 294 - Divisors solution

problem link-click here

In this problem it's easy to get TLE while using general approach. here you have to follow one rule which is

       1. The total number of divisors of a number is just doubled of divisors till the square root of that number though there's some special case which is if the square root of given number is an integer then the total number of divisors is 2*number of divisors till sqrt(number) -1 otherwise 2*number of divisors till sqrt(number).

for example let a given number is 6 then sqrt(6) = 2.4494 . here 1 and 2 (2 divisors till sqrt(6)) is the divisors till 2.4494(sqrt(6)) and the square root is not an integer(2.4494) so total number of divisors is 2*2=4.

again, let a given number is 25 then sqrt(25) = 5 . here 1 and 5 (2 divisors till sqrt(25)) is the divisors till 5(sqrt(25)) and the square root is an integer(5) so total number of divisors is 2*2-1=3.
similarly you will have to find each number's total number of divisors. then output the number which has largest divisors.

an accepted code is given below:

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

int main()
{
    int t,l,u;

    scanf("%d",&t);

    while(t--)
    {
        int num,s=0;

        scanf("%d%d",&l,&u);

        for(int i=l;i<=u;i++)
        {
            int c=0;

            for(int j=1;j<=sqrt(i);j++)
            {
                if(i%j==0)
                {
                    c++;

                }
            }

            int tmp=sqrt(i);

            if(tmp==sqrt(i))
            {
                c=c*2-1;
            }
            else
            {
                c=c*2;
            }

            if(c>s)
            {
                s=c;
                num=i;
            }
        }

        printf("Between %d and %d, %d has a maximum of %d divisors.\n",l,u,num,s);
    }

    return 0;
}

UVA 492 - Pig-Latin solution

probelm link-click here

it's after all an easy problem to implement. its a string processing related problem. you have to just keep in mind that if in a word the 1st letter is vowel then you have to write the word appending "ay" with it. otherwise if you get 1st letter of a word consonant then you have to remove the 1st letter of the word appending it in the last along with "ay". for example suppose a word is "SOJOL". 1st letter S which is consonant so in output it should be "OJOLSay" and suppose a word "ISLAM" here 1st letter is I which is a vowel so in output it should be shown as "ISLAMay". thats it.

an accepted code is given below:

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

bool isvowel(char c)
{
    if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U' || c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
    {
        return true;
    }
    else
    {
        return false;
    }
}

void processing(string str)
{
    vector<char> v;

    if(isvowel(str[0]))
    {
        int i=0;

        while(isalpha(str[i]))
        {
            cout<<str[i];

            i++;
        }

       if(i==str.size())
        {
        cout<<"ay.";
        }
        else
        {
            cout<<"ay";

        }
    }
    else if(!isvowel(str[0]) && ((str[0]>=65 && str[0]<=91) || (str[0]>=97 && str[0]<=122)) )
    {
        int i=1;

        while(isalpha(str[i]))
        {
            cout<<str[i];

            i++;
        }

        if(i==str.size())
        {
        cout<<str[0]<<"ay.";
        }
        else
        {
            cout<<str[0]<<"ay";

        }
    }
    else
    {
        cout<<str[0];
    }

    for(int k=1;k<str.length();)
    {
        if(!isalpha(str[k]))
        {
            cout<<str[k];
            k++;
        }
       else if(isalpha(str[k]) && !isalpha(str[k-1]))
        {
            if(isvowel(str[k]))
            {
                while(isalpha(str[k]))
                {
                    cout<<str[k];
                    k++;
                }
            cout<<"ay";
            }
            else
            {
                char v=str[k];
                k++;

                while(isalpha(str[k]))
                {
                    cout<<str[k];
                    k++;
                }

            cout<<v<<"ay";
            }

        }
        else
        {
            k++;
        }
    }

    cout<<endl;

}

int main()
{
    string str;

    while(getline(cin,str))
    {
        processing(str);
    }

    return 0;
}

Thursday, August 25, 2016

UVA 160 - Factors and Factorials solution(Only hints)

problem link-click here

at first glance you may think that you have to find the factorial of the given N then the prime factors but it's not like that.. just find the prime factorization of number 1 to N and count the primes like guess 5! which can be written as,
                              5! = 120 = 2*2*2*3*5
again if we assume prime factor of 5 is 5, 4 is 2*2 , 3 is 3, 2 is 2.... so we get 2*2*2*3*5 from here so both the facts are same. but the 2nd one is less time consuming. follow the output format. you need to find primes within 100 because it will be enough for this problem.

UVA 10922 - 2 the 9s solution

problem link-click here

here's only one problem which is understanding how to determine the 9-degree of N. this is simple. this indicates recursively how many steps needed to get 9. let 99999 is the input N.
                          now sum= 9+9+9+9+9=45 //45 is divisible by 9
                          again sum= 4+5=9 here we get 9 by two steps
1st step adding string and 2nd step is again adding the sum value.. this will continue until we get 9 from a given string and the steps we need to get 9 is our desired 9-degree N.

an accepted code is given below:


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

int value(string str)
{
    int sum=0;

    for(int i=0;i<str.length();i++)
    {
        sum=sum+(str[i]-48);
    }

    return sum;
}

int recursion(int sum)
{
         int i=1;

         if(sum%9==0)
         {
             while(sum!=9 && sum>9)
             { int r=0;
         while(sum!=0)
         {
             r=r+(sum%10);
             sum=sum/10;

         }
         sum=r;

          i++;
             }

          return i;

         }

         else
         {
             return -1;
         }


}

int main()
{
    string str;

    while(cin>>str && str!="0")
    {
        int sum=value(str);

        int i=recursion(sum);
        cout<<str;

        if(i==-1)
        {
           printf(" is not a multiple of 9.\n");
        }
        else if(str=="9")
        {
            printf(" is a multiple of 9 and has 9-degree 1.\n");
        }
        else
        {
        printf(" is a multiple of 9 and has 9-degree %d.\n",i);
        }
    }

    return 0;
}

Friday, August 19, 2016

UVA 10189 - Minesweeper solution

problem link-click here

This problem is easy just think it as easiest problem because this is an implementation based problem. now let's come to the main point.

you have to output the number of mines in the cell which is not a mine. The most simple way to do this is to reset your matrix and just add to all adjacent cells if a mine is found and also tag whether or not a cell is a mine or not. remember there is 8 adjacent square box for each mine.

an accepted output is as follows:

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

int main()
{
     int n,m,t,k=1;

     while( scanf("%d%d",&n,&m)==2)
     {
         if((m!=0 && n!=0) && k!=1)
         {
             printf("\n");
         }
         else if(m==0 && n==0)
         {
             break;
         }

         string str[110];

         for(int i=0;i<n;i++)
         {
            cin>>str[i];
         }

         for(int i=0;i<n;i++)
         {
             for(int j=0;j<m;j++)
             {
                 if(str[i][j]=='.')
                 {
                     str[i][j]='0';
                 }
             }
         }

        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(str[i][j]=='*')
                {
                    if(((i-1)>=0 && (j-1)>=0) && str[i-1][j-1]!='*')
                    {
                        str[i-1][j-1]=str[i-1][j-1]+1;
                    }

                    if(((i>=0 && (j-1)>=0)) && (str[i][j-1]!='*'))
                    {
                        str[i][j-1]=str[i][j-1]+1;
                    }

                    if(((i-1)>=0 && j>=0) && (str[i-1][j]!='*'))
                    {
                        str[i-1][j]=str[i-1][j]+1;
                    }

                    if(((i-1)>=0 && j+1<m) && str[i-1][j+1]!='*')
                    {
                        str[i-1][j+1]=str[i-1][j+1]+1;
                    }

                    if((i>=0 && j+1<m) &&  str[i][j+1]!='*')
                    {
                        str[i][j+1]=str[i][j+1]+1;
                    }

                    if((i+1<n && j+1<m) && str[i+1][j+1]!='*')
                    {
                        str[i+1][j+1]=str[i+1][j+1]+1;
                    }

                    if((i+1<n && j<m) && str[i+1][j]!='*')
                    {
                        str[i+1][j]=str[i+1][j]+1;
                    }

                    if((i+1<n && j-1>=0) && str[i+1][j-1]!='*')
                    {
                        str[i+1][j-1]=str[i+1][j-1]+1;
                    }
                }
            }
        }

        printf("Field #%d:\n",k);

        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cout<<str[i][j];
            }

            cout<<endl;
        }

         k++;
     }

     return 0;
}

UVA 10018 - Reverse and Add Solutions

problem link-click here

accepted code

#include <stdio.h>
int main()
{

    long long int t,n,c,sum,r,d;
    scanf("%lld",&t);
    while (t--)
    {
        scanf("%lld",&n);
        d=n;
        c=0;
        while (1)
        {
            sum=0;
            while (d>0)
            {
                r=d%10;
                sum=sum*10+r;
                d=d/10;
            }
            if (n==sum)
                break;
            else
            {
                d=sum+n;
                n=sum+n;
                c++;
            }
        }
        printf("%lld %lld\n",c,n);
    }
    return 0;
}

Friday, August 12, 2016

UVA 11936 - The Lazy Lumberjacks solution

problem link-click here

Use your brain and find the statement if you can find which is

"The sum of the lengths of any two sides of a triangle must be greater than the third side".

if this is not true then you do not have a triangle...........

for more information visit Triangle_inequality/Wikipedia

here we go,

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

int main()
{
    int t;

    scanf("%d",&t);

    while(t--)
    {
        int a[10];

        scanf("%d%d%d",&a[0],&a[1],&a[2]);
        sort(a,a+3);

        if(a[0]+a[1]>a[2])
        {
            printf("OK\n");
        }
        else
        {
            printf("Wrong!!\n");
        }

    }

    return 0;
}

UVA 10432 - Polygon Inside A Circle solution

problem link-click here

its a very easy problem just use  the formula of finding area of a polygon & the formula is

see the  above mentioned formulas in the picture ( collected from mathworlds.com )
















Area = n*r*r/2 * sin ( 360/n)

here we go,


#include<bits/stdc++.h>
#define pi acos(-1)
using namespace std;

int main()
{
    double a,b,c;

    while(scanf("%lf%lf",&a,&b)==2)
    {
        c=((b*a*a)/2)*sin(2*pi/b);
        printf("%.3lf\n",c);
    }

    return 0;

}

Friday, August 5, 2016

UVA 10127 - Ones solution

problem link-click here

This  is indeed an easy problem but realizing the problem statement is quite hard. but don't worry as i'm here.

in the problem it is said that you have to find the length of the multiple which is consist of only sequence of 1 ( no other digit without 1 ) for  a given number N.

for example assume that n=7 . we have to find smallest multiple of 7 and this multiple have to be consist of only the digit 1. so you just need to check whether

1,11,111,1111,11111,111111,1111111........so on which is divisible by 7 and also has to be smaller one.for 7 the smallest multiple is 111111 as,
                                       
                                                    111111 = 7 x 15873



an accepted code  is given below:


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

int process(int n)
{
    vector<int> v;

    for(int i=0;i<n;i++)
    {
         long long int sum=0;

        v.push_back(1);

        for(int j=0;j<v.size();j++)
        {
            sum=(sum*10+v[j])%n;
        }

        if(sum==0)
        {
            break;
        }
    }

    return v.size();
}

int main()
{
    int n;

    while(scanf("%d",&n)==1)
    {
        cout<<process(n)<<endl;
    }

    return 0;
}

Tuesday, August 2, 2016

UVA 10013 - Super long sums solution

problem link-click here

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

void add(vector<int> v[])
{
    vector<int> a;
    int s=0,carry=0;

    for(int i=v[1].size()-1;i>=0;i--)
    {
        s=v[1][i]+v[0][i]+carry;

        if(s>=10)
        {
            a.push_back(s%10);
            carry=s/10;
        }
        else
        {
            a.push_back(s);
            carry=0;
        }
    }

    if(carry>0)
    {
        a.push_back(s);
        carry=0;
    }

    for(int i=a.size()-1;i>=0;i--)
    {
        cout<<a[i];
    }

    cout<<endl;
}

int main()
{
    int t,m,x,y;

    scanf("%d",&t);

    while(t--)
    {
        vector<int> v[3];

        scanf("%d",&m);

        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            v[0].push_back(x);
            v[1].push_back(y);
        }

        add(v);

        if(t!=0)
        {
            cout<<endl;
        }

    }

    return 0;
}