Thursday, December 21, 2017

483 - Word Scramble Uva Solution & Logic

///
* Very Easy Problem , But little Bit Different
* When Get whitespace return rotating that string
* and last word should be rotating in alert mind , don't forget this .
Because , after last word - there is no whitespace

///

See Code ....................


#include<bits/stdc++.h>

// Nayeem Shahriar Joy , Applied Physics & Electronic Engineering , University of Rajshahi.

using namespace std;

int main()
{
    int l,i,j,b;
    string s;
    while(getline(cin,s))
    {
    l=s.size();
    b=-1;
    for(i=0;i<l;i++)
    {
    if(s[i]==' ')
    {

    for(j=i-1;j>b;j--){
    printf("%c",s[j]);
    }

    printf(" ");
    b=i;

    }

    }

   for(i=l-1;i>b;i--){
    printf("%c",s[i]);
   }
    printf("\n");
    }
    return 0;
}

490 - Rotating Sentences Uva Solution & Logic

//
*Very Interesting Problem , Just Think Differently.
*In Output , One Line can be extended upto maximum length of given string
*Print From The Last
///

See Code .............


#include<bits/stdc++.h>

// Nayeem Shahriar Joy , Applied Physics & Electronic Engineering , University of Rajshahi.

using namespace std;




int main() {
    int mx_length = 0;
    string s;
    vector<string> str;
    while(getline(cin, s)) {
        str.push_back(s);
        mx_length = max(mx_length, (int) s.length());
    }
    for(int i = 0; i < mx_length; i++) {
        for(int j = str.size() - 1; j >= 0; j--) {
                if(i<str[j].length())
                {
                    printf("%c",str[j][i]);
                }
                else{
                        char c=' ';
                    printf("%c",c);
                }
        } printf("\n");
    }
    return 0;
}

10420 - List of Conquests Uva Solution & Logic

///

* Everyline Consists a country name as a first word ,so we have to take it as a string & we have to take other charcters using getline .
* Using map we can store that first word ( country ) & Store its frequencies
* map is manually sorted  . As country name stored as a key ( left side ) so , it will be automatically
sorted .

////

See Code ...........................................................

#include<bits/stdc++.h>

// Nayeem Shahriar Joy , Applied Physics & Electronic Engineering , University of Rajshahi.

using namespace std;


    int main()
{
    map<string,int>frequency;
    map<string,int>::iterator it;
    string s1,s2;
    int t;
    cin>>t;
    while(t--)
    {
        cin>>s1;
        getline(cin,s2);
        frequency[s1]++;
    }
    for( it=frequency.begin();it!=frequency.end();it++)
    {
        cout<<it->first<<" "<<it->second<<endl;
    }
    return 0;
}

11936 - The Lazy Lumberjacks Uva Solution & Logic

//
Extremely Basic 

* To Make A Triangle , The summation of Two lengths of triangle  obviously should be greater than third length
///


Now See Code ....................


#include<bits/stdc++.h>

// Nayeem Shahriar Joy , Applied Physics & Electronic Engineering , University of Rajshahi.

using namespace std;


    int main()
{
int a,b,c,i,t;
while(scanf("%d",&t)==1)
{
while(t--){
 scanf("%d%d%d",&a,&b,&c);
 if(a+b<=c || b+c<=a || a+c<=b)
 printf("Wrong!!\n");
 else
 printf("OK\n");
 }
}
}

Wednesday, December 20, 2017

10347 - Medians Uva Solution Logic

//

Very Easy Problem -----

When Three Medians( মধ্যমা ) Are Given , Then Using Heron's Formula , We can solve the area
/////

Now See Code ,


#include<bits/stdc++.h>

// Nayeem Shahriar Joy , Applied Physics & Electronic Engineering , University of Rajshahi.

using namespace std;


int main()
{
   double u,v,w,s,area;

    while (scanf("%lf%lf%lf", &u, &v, &w) == 3){

         s = (u + v + w) / 2.0;
         area = (4.0 / 3.0) * sqrt(s * (s - u) * (s - v) * (s - w));

        if(area > 0)
            printf("%0.3lf\n", area);
        else
            printf("-1.000\n");

    }
    return 0;
}

11455 - Behold my quadrangle Uva Solution & Logic

//

*Say , Four Lengths Are  a , b , c , d

* if(a==b &&b==c && c==d &&a==d)  then square

* if( (a==c && b==d)||(a==b && c==d)||(b==c && a==d)) then rectangular 

* if it is not a square & rectangular , then by satisfying the condition 
a<=b+c+d  && b<=a+c+d && c<=b+c+d   it can be  quadrangle

////


Now Code .......................



#include<bits/stdc++.h>

// Nayeem Shahriar Joy , Applied Physics & Electronic Engineering , University of Rajshahi.

using namespace std;


int main()
{
    long long int a, b, c, d;
    int i, t;
    while((scanf("%d", &t)==1))
    {
        for(i=1; i<=t; i++)
        {
            scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
            if(a==b && b==c && c==d) printf("square\n");
            else if( (a==b && c==d) || (b==c && d==a) || (a==c&& b==d)) printf("rectangle\n");
            else if((a<=b+c+d) && (b<=c+d+a) && (c<=d+a+b) && (d<=a+b+c) ) printf("quadrangle\n");
            else printf("banana\n");
        }
    }
}
 

Tuesday, December 19, 2017

10079 - Pizza Cutting Uva Solution & Logic

///
তুমি  n  সংখ্যক লাইন একে সরবোচ্চ কত টুকরা করতে পারবে ???? যখন তুমি একটা লাইনও দেও নি , তখন মাত্র ওই একটা টুকরা ছিলো , A ( 0 ) =1

ধরো তুমি , n তম লাইন দিয়ে কাটতে যাচ্ছো , তাহলে - তোমার আগের ( n -১ ) সংখ্যক সকল লাইনকে ইন্টারসেক্ট করতে হবে , এর মানে , নতুন আরো  n টি টুকরা'র  জন্ম হবে , তাই না ?? তাহলে মোট টুকরা হবে , A ( n )

A ( n ) = A ( n-1 ) +n  + A ( 0 )
A ( n ) =  A ( n-2 ) + ( n -1 ) + n + A ( 0 )
A ( n ) = n + n-1 + n-2 +......+ 1 + A ( 0 )
A ( n ) = n*(n+1)/2 + A (0)
A (n)  = n*(n+1)/2 + 1
///

Now , U can do ............................................


#include<bits/stdc++.h>

// Nayeem Shahriar Joy , Applied Physics & Electronic Engineering , University of Rajshahi.

using namespace std;

   int main(){
    long long n;
    while(scanf("%lld", &n)){
        if(n < 0)
            return 0;

        printf("%lld\n", 1 + n * (n + 1) / 2);
    }
    return 0;
}





11401 - Triangle Counting Uva Solution Logic

নিচের ছবি দুইটি ভালো করে দেখে , প্যাটার্ন বুঝে নাও - তারপর , সলিউশন দেখো :) :)

একটা জিনিস মাথায় রেখো , ত্রিভুজের যে কোনো দুই বাহু এর যোগফল আরেকটা থেকে বড়ো :)


#include<bits/stdc++.h>

using namespace std;

#define LL long long

  LL F[1000007], P[1000007];

void precal()
{
    F[3] = 0; P[3] = 0;
    LL var = 0;
    for(int i=4; i<=1000000; i++)
    {
        if(i%2==0)
        {
            var++;
            P[i] = P[i-1] + var;
            F[i] = F[i-1] + P[i];
        }
        else
        {
            P[i] = P[i-1] + var;
            F[i] = F[i-1] + P[i];
        }
    }
}

int main()
{
    precal();
    int n;
    while(scanf("%d", &n) && n>=3)
    {
        printf("%lld\n", F[n]);
    }
}

11313 - Gourmet Games Uva Solution & Logic

/
* n জন ক্যান্ডিডেট এর ভেতর থেকে  প্রতি শো'তে অংশ নেবে m জন ।
* প্রতি রাউন্ড থেকে এক জন বাদে বাকি সবাই ( m-১ ) বাদ পড়বে ।।
* এই সকলের ভেতরে যে কোনো এক জন চ্যাম্পিয়ন হবে বাকি সবাই বেচে থাকবে ( n-১ ) জন।
* অরথাত  শর্ত অনুযায়ী চলার জন্য , রাউন্ড গুলো চালাতে গেলে - ( n-১ ) গুলো লোক ( m-১ )হারে বাদ পড়বে , এর মানে মোট  ( n-১ ) / ( m-১ ) গুলো রাউন্ড লাগবে :) :)
/

Now See Your Code .................................

#include<bits/stdc++.h>

// Nayeem Shahriar Joy , Applied Physics & Electronic Engineering , University of Rajshahi.

using namespace std;

   int main()
{
    int n, m, i, t;
    scanf("%d", &t);
    for (i = 0; i < t; i++)
    {
        scanf("%d %d", &n, &m);

        if ((n - 1) % (m - 1) != 0)
        {
            puts("cannot do this");
        }
        else
        {
            printf("%d\n", (n - 1) / (m - 1));
        }
    }
    return 0;
}

Friday, December 8, 2017

12403 Save Setu Uva Problem Solution



#include<bits/stdc++.h>

// Nayeem Shahriar Joy, Applied Physics & Electronic Engineering , University of Rajshahi.

using namespace std;

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

    cin>>t;

    long long sum=0;

    string s;


    while ( t-- )
    {
        cin>>s;
        if(s=="donate")
        {
            cin>>a;
            sum=sum+a;
        }
        else
        {
            cout<<sum<<endl;
        }
    }

    return 0;
}

12372 - Packing for Holiday Uva Problem Solution



#include<bits/stdc++.h>

// Nayeem Shahriar Joy, Applied Physics & Electronic Engineering , University of Rajshahi.

using namespace std;

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

    cin>>t;

     int i=1;

    while ( t-- )
    {
        cin>>a>>b>>c;
        cout<<"Case "<<i<<": ";
        if(a<=20&&b<=20&&c<=20)
        {
            cout<<"good"<<endl;
        }
        else
        {
            cout<<"bad"<<endl;
        }
        i++;
    }

    return 0;
}

12250 - Language Detection Uva Solution




#include<bits/stdc++.h>

// Nayeem Shahriar Joy, Applied Physics & Electronic Engineering , University of Rajshahi.

using namespace std;
int main()
{

    string s;
    int i=1;
        while(cin>>s)
        {

            if(s=="HELLO")
            {
                cout<<"Case "<<i<<": ";
                cout<<"ENGLISH"<<endl;
            }
            else if(s=="HOLA")
            {
                cout<<"Case "<<i<<": ";
                cout<<"SPANISH"<<endl;
            }
            else if(s=="HALLO")
            {
                cout<<"Case "<<i<<": ";
                cout<<"GERMAN"<<endl;
            }
            else if(s=="BONJOUR")

           {
               cout<<"Case "<<i<<": ";
               cout<<"FRENCH"<<endl;

            }

            else if(s=="CIAO")
            {
                cout<<"Case "<<i<<": ";
                cout<<"ITALIAN"<<endl;
            }
            else if(s=="ZDRAVSTVUJTE")
            {
                cout<<"Case "<<i<<": ";
                cout<<"RUSSIAN"<<endl;
            }
            else if(s=="#")
            {
                break;
            }
            else
            {
                cout<<"Case "<<i<<": ";
                cout<<"UNKNOWN"<<endl;
            }
            i++;

            }
    return 0;
}

11727 - Cost Cutting Uva Problem Solution




#include<bits/stdc++.h>

// Nayeem Shahriar Joy, Applied Physics & Electronic Engineering , University of Rajshahi.

using namespace std;
int main()
{

    int t,a,b,c;
    cin>>t;
        for(int i=1;i<=t;i++){
           vector<int>joy;
           cin>>a>>b>>c;
           joy.push_back(a);
           joy.push_back(b);
           joy.push_back(c);
           sort(joy.begin(),joy.end());
           cout<<"Case "<<i<<": ";
           cout<<joy[1]<<endl;
        }


    return 0;
}

11547 - Automatic Answer Uva Problem Solution




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

    int t,n,i;
    cin>>t;
        while(t--){
            scanf("%d",&n);
            n=n*567;
            n=n/9;
            n+=7492;
            n*=235;
            n/=47;
            n=n-498;
            n/=10;
            n%=10;
            n=abs(n);
            printf("%d\n",n);
        }


    return 0;
}

11044 - Searching for Nessy Uva Problem Solution


#include <bits/stdc++.h>
using namespace std;
// Nayeem Shahriar Joy , Applied Physics & Electronic Engineer9ng, University of Rajshahi.

int main() {
    int T,a,b;
    cin >> T;
    for(int t = 0; t< T; t++){

        cin >> a>>b;
        cout<<(a/3)*(b/3)<<endl;
    }
    return 0;
}

11364 - Parking Uva Problem Solution






#include <bits/stdc++.h>
using namespace std;
// Nayeem Shahriar Joy , Applied Physics & Electronic Engineer9ng, University of Rajshahi.

int main() {
    int T,d;
    cin >> T;
    for(int t = 0; t< T; t++){
        int n;
        cin >> n;
        vector<int>distance;
        for(int i = 0; i < n; i++){
            cin >> d;
            distance.push_back(d);
        }
        sort(distance.begin(),distance.end());
        cout << 2*(distance[n-1]-distance[0]) << endl;
    }
    return 0;
}

Friday, December 1, 2017

DevSkill - Factorially Even or Odd Problem Solution

https://www.devskill.com/CodingProblems/ViewProblem/354


// Nayeem Shahriar Joy , Applied Physics & Electronic Engineering , University of Rajshahi.

#include<bits/stdc++.h>

using namespace std;

unsigned long long trailingzeroesfinding(unsigned long long n)

{

    unsigned long long  count=0;

       for(int i=5;n/i>=1;i=i*5)

       {

           count=count+(n/i);

       }

       return count;

}


int main()

{
   int t;
   cin>>t;
   unsigned long long n;
   while(t--)
   {
       cin>>n;
       unsigned long long  trailing=trailingzeroesfinding(n);
       int joy =0;
       if(trailing==1)
       {
           joy=1;
       }
       else{
       for(unsigned long long i=1;pow(2,i)<=trailing;i++)

       {
           if(pow(2,i)==trailing)

           {
               joy=1;
               break;

           }
       }
       }
     if(joy)
     {
         cout<<"Even"<<endl;
     }
     else
     {
         cout<<"Odd"<<endl;
     }
   }
   return 0;
}

Part-6 (Longest Increasing Subsequence )

The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. For example, the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33, 50, 60, 80}.


                                                   DYNAMIC PROGRAMMING       


#include<bits/stdc++.h>

using namespace std;


int main()

{
     int arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 };
    int n = sizeof(arr)/sizeof(arr[0]);


     int lis[n];
    for(int i=0;i<n;i++)
    {
        lis[i]=1;

    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<i;j++)
        {
            if((arr[i]>arr[j])&&((lis[j]+1)>lis[i]))
               {
                   lis[i]=lis[j]+1;
               }
        }
    }
    sort(lis,lis+n);

    cout<<lis[n-1]<<endl;
    return 0;
}

PROBLEM LINK: https://practice.geeksforgeeks.org/problems/longest-increasing-subsequence/0

                                      USING DYNAMIC PROGRAMMING


// Nayeem Shahriar Joy , Applied Physics & Electronic Engineering , University of Rajshahi.

#include<bits/stdc++.h>

using namespace std;


int main()

{
    int n;

    cin>>n;

    while(n--){

     int x;

    cin>>x;

     int arr[x];

    for(int i=0;i<x;i++)

    {
       cin>>arr[i];

    }

    int lis[x];

    for(int j=0;j<x;j++)

    {
        lis[j]=1;
    }

    for(int k=0;k<x;k++)

    {
        for(int y=0;y<k;y++)

        {
            if(arr[k]>arr[y]&&((lis[y]+1)>lis[k]))

            {
                lis[k]=lis[y]+1;
            }
        }
    }

    sort(lis,lis+x);

    cout<<lis[x-1]<<endl;

    }
    return 0;
}