Saturday, 27 February 2016

Uva - 11057 - Exact Sum Problem Solution.

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
vector<int>in;

int main(){

    int fin,sin;
    int n,a,m;
    while(scanf("%d",&n) != EOF){
        int minDes = 1000001,fVal,sVal;
        for(int i = 0; i < n; i++){
            scanf("%d",&a);
            in.push_back(a);
        }
        scanf("%d\n",&m);
        sort(in.begin(),in.end());
        for(int i = 0; i < n; i++){
            for(int j = i+1; j < n; j++){
                if(in[i]+in[j] == m && abs(in[i] - in[j]) < minDes){
                    fVal = in[i];
                    sVal = in[j];
                    minDes = abs(in[i] - in[j]);
                }
            }
        }
        printf("Peter should buy books whose prices are %d and %d.\n\n",fVal,sVal);
        in.clear();
    }
    return 0;
}

Uva - 10487 - Closest Sums Problem Solution.

Solution: 


#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int n,ar[1005],query[30],cas = 0,m;
    vector<int>res;
    while(cin >> n && n != 0){
        cas++;
        for(int i = 0; i < n; i++)cin >> ar[i];
        for(int i = 0; i < n; i++){
            for(int j = i+1; j < n; j++){
                res.push_back(ar[i]+ar[j]);
            }
        }
        sort(res.begin(),res.end());
        cin >> m;
        for(int i = 0; i < m; i++)cin >> query[i];
        cout << "Case " << cas << ":" << endl;
        for(int i = 0; i < m; i++){
            for(int j = 1; j < res.size(); j++){
                if(res[j] >= query[i]){
                    if(abs(res[j-1] - query[i]) >= abs(res[j] - query[i]))
                      cout << "Closest sum to " << query[i] << " is " << res[j] << "." << endl;
                    else 
                      cout << "Closest sum to " << query[i] << " is " << res[j-1] << "." << endl;
                    break;
                }
                else if (query[i] >= res[res.size()-1]){
                    cout << "Closest sum to " << query[i] << " is " << res[res.size()-1] << "." << endl;
                    break;
                }
            }
        }
        res.clear();
    }
    return 0;
}

Saturday, 20 February 2016

C Program To Find The Nth Fibonacci Number Using Recursion Problem Solution.


Solution:

#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
long long fib(long long a,long long b,int n){
 if(n == 0)return a;
 else return fib(b,a+b,n-1);
}
int main(void) {

 int n;
 scanf("%d",&n);
 if(n > 90)printf("Input number is to big.\n");
 else printf("The %d number in fibonacci series is: %lld\n",n,fib(0,1,n));
 return 0;
}

Monday, 15 February 2016

Map Coloring Problem Solution Using C++.

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
vector<string>colors;
vector<string>states;
map<string,vector<string> >neighbors;
map<string,string>statesColors;

void initializeColor();
void initializeStates();
void initializeneighbors();
string get_color_of_state(string state);
bool check(string state,string color){
    for(int i = 0; i < neighbors[state].size(); i++){
        string neighborColor = statesColors[neighbors[state][i]];
        if(neighborColor == color)return false;
    }
    return true;
}

int main(){

    initializeColor();
    initializeStates();
    initializeneighbors();

    for(int i = 0; i < states.size(); i++){
        statesColors[states[i]] = get_color_of_state(states[i]);
    }
    for(int i = 0; i < statesColors.size(); i++)cout << states[i] << " : " << statesColors[states[i]] << endl;
    return 0;
}

string get_color_of_state(string state){
    for(int i = 0; i < colors.size(); i++){
        if(check(state,colors[i]))return colors[i];
    }
    return "None";
}

void initializeColor(){
    colors.push_back("Red");
    colors.push_back("Blue");
    colors.push_back("Green");
}

void initializeStates(){
    states.push_back("Dhaka");
    states.push_back("Khulna");
    states.push_back("Chittagong");
    states.push_back("Rajshahi");
    states.push_back("Barisal");
    states.push_back("Sylet");
    states.push_back("Ranghpur");
}
void initializeneighbors(){
    neighbors["Dhaka"].push_back("Khulna");
    neighbors["Dhaka"].push_back("Chittagong");
    neighbors["Dhaka"].push_back("Rajshahi");
    neighbors["Dhaka"].push_back("Barisal");
    neighbors["Dhaka"].push_back("Sylet");
    neighbors["Dhaka"].push_back("Ranghpur");

    neighbors["Khulna"].push_back("Dhaka");
    neighbors["Khulna"].push_back("Rajshahi");
    neighbors["Khulna"].push_back("Barisal");

    neighbors["Chittagong"].push_back("Dhaka");
    neighbors["Chittagong"].push_back("Sylet");

    neighbors["Sylet"].push_back("Chittagong");
    neighbors["Sylet"].push_back("Ranghpur");
    neighbors["Sylet"].push_back("Rajshahi");
    neighbors["Sylet"].push_back("Barisal");

    neighbors["Ranghpur"].push_back("Dhaka");
    neighbors["Ranghpur"].push_back("Sylet");

    neighbors["Rajshahi"].push_back("Dhaka");
    neighbors["Rajshahi"].push_back("Sylet");
    neighbors["Rajshahi"].push_back("Khulna");
    neighbors["Rajshahi"].push_back("Barisal");

    neighbors["Barisal"].push_back("Sylet");
    neighbors["Barisal"].push_back("Dhaka");
    neighbors["Barisal"].push_back("Rajshahi");
}

Wednesday, 10 February 2016

Uva - 10473 - Simple Base Conversion Problem Solution.

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int val = 10;
    string num;
    map<char,int>hexVal;
    map<int,char>decVal;
    for(char c = 'A'; c <= 'F'; c++,val++)hexVal[c] = val;
    char c = 'A';
    for(int i = 10; i <= 15; i++,c++)decVal[i] = c;
    while(cin >> num){
        if(num[0] == '-')return 0;
        int len = num.length();
        long long dec = 0,hex,resDec = 0;
        string resHex;
        if(num[1] != 'x'){
            for(int i = 0; i < len; i++)dec = dec *10 + (num[i] - 48);
            while(dec != 0){
                int rem = dec % 16;
                if(rem > 9)resHex += decVal[rem];
                else resHex += (char)(rem) + '0';
                dec/=16;
            }
            reverse(resHex.begin(),resHex.end());
            cout << "0x" << resHex << endl;
        }
        else{
           int p = (len - 1) - 2;
           for(int i = 2; i < len; i++){
                if(num[i] >= 48 && num[i] <= 57)resDec += (num[i]- 48) * pow(16,p);
                else resDec += hexVal[num[i]] * pow(16,p);
                p--;
           }
           cout << resDec << endl;
        }
    }
    return 0;
}

Uva - 10469 - To Carry or not to Carry Problem Solution.


Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    unsigned int a,b;
    while(cin >> a >> b){
        bitset<32>ab(a),bb(b);
        unsigned int res = 0;
        for(int i = 0; i < 32;i++){
            if((ab[i] ^ bb[i]) == 1){
                res += pow(2,i);
            }
        }
        cout << res << endl;
    }
    return 0;
}

Another Way:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int a,b;
    while(cin >> a >> b)cout << (a^b) << endl;
    return 0;
}

Tuesday, 9 February 2016

Uva - 10432 - Polygon Inside A Circle Problem Solution.

Solution:


#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
#define PI 3.1415926535897932384626433832795
int main(){

    double r,n;
    while(scanf("%lf %lf",&r,&n) != EOF){
        printf("%.3lf\n",(.5*(n*(r*r)))*sin(2.0*PI/n));
    }
    return 0;
}

Uva - 11636 - Hello World! Problem Solution.

Solution:


#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int n,cas = 1;
    while(cin >> n && n > 0){
        int hello = 1,cnt = 0;
        while(hello < n){
            hello+=hello;
            cnt++;
        }
        cout << "Case " << cas++ << ": " << cnt << endl;
    }
    return 0;
}

Monday, 8 February 2016

Uva - 10424 - Love Calculator Problem Solution.

One day I asked Saima that how much she loves me. Her answer was “71.43%”. I was surprised as well as shocked by her answer. I could not understand why she didn’t tell 100% and why she told a particular and peculiar fraction like 71.43. Looking at my surprised, shocked and nervous face she burst out laughing and told that she loves me more than any thing in this universe and it was nothing but a silly and funny love calculation. Then she described me the calculation. In this problem you will have to write a program so that any one can calculate love between any two persons very quickly (of course a very silly game).
Rules:
You will be given two names. These two names can have white space or some other non-alphabetical characters like $ & % etc. But only the alphabets from a to z or A to Z will participate in love calculation. Each alphabet has a particular value. The values are from 1 to 26 in ascending order of the alphabets. It’s like this, a = 1, b = 2, c = 3, ..., z = 26. Both upper case and lower case holds the same values. Then make the sum of these numbers until it comes in one digit. [For example, consider a name ‘bcz’. Here, b = 2, c = 3 and z = 26. So, the sum is (2+3+26) = 31 = (3+1) = 4.] Then the ratio of these two numbers in percentage will be the result.

Remember: Result can not be more than 100% . Take the ratio carefully to avoid this problem.

Input
Your input will be two names. Each name holds not more than 25 characters. End of file will indicate the end of input.

Output
For each pair of names your program will have to calculate the love between those two persons and give the result as output. In result two digits to be displayed after the decimal point. All the results must be in new lines.

Sample Input
saima
shanto
Pakistan
India
USA
USSR

Sample Output 
71.43 %
100.00 %
100.00 %

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
char low(char ch){
    if(ch >= 'A' && ch <= 'Z')return ch+=32;
    return ch;
}
int main(){

    string s1,s2;
    int ar[26];
    for(int i = 1; i <= 26; i++)ar[i-1] = i;
    while(getline(cin,s1)){
        getline(cin,s2);
        int sum1 = 0,sum2 = 0,len = s1.length();
        for(int i = 0; i < len; i++){
            if((s1[i] >= 'A' && s1[i] <= 'Z') || (s1[i] >= 'a' && s1[i] <= 'z'))sum1 += ar[low(s1[i])-'a'];
        }
        len = s2.length();
        for(int i = 0; i < len; i++){
            if((s2[i] >= 'A' && s2[i] <= 'Z') || (s2[i] >= 'a' && s2[i] <= 'z'))sum2 += ar[low(s2[i])-'a'];
        }

        int tmps = 0,tmpdig1 = 0,tmpdig2 = 0;
        while(sum1 != 0){
            tmpdig1+= sum1 % 10;
            sum1 /= 10;
        }
        if(tmpdig1 > 9){
            tmps = tmpdig1;
            tmpdig1 = 0;
            while(tmps != 0){
                tmpdig1 += tmps%10;
                tmps/=10;
            }
        }
        while(sum2 != 0){
            tmpdig2 += sum2 % 10;
            sum2 /= 10;
        }
        if(tmpdig2 > 9){
            tmps = tmpdig2;
            tmpdig2 = 0;
            while(tmps != 0){
                tmpdig2 += tmps % 10;
                tmps /= 10;
            }
        }

        if(tmpdig1 > tmpdig2)printf("%0.2f %\n",((float)tmpdig2*100)/(float)tmpdig1 );
        else printf("%0.2f %\n",((float)tmpdig1*100)/(float)tmpdig2);
    }
    return 0;
}

Sunday, 7 February 2016

Uva - 10107 - What is the Median? Problem Solution.

Problem Link

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){
    long long a,ar[10000],i;
    while(cin >> a){
        ar[i++] = a;
        sort(ar,ar+i);
        if(i%2 == 0)cout << (ar[i/2-1]+ar[i/2])/2 << endl;
        else cout << ar[i/2] << endl;
    }
    return 0;
}

Saturday, 6 February 2016

Sparse Arrays Hacker Rank Problem Solution Using C++.

There are N strings. Each string's length is no more than 20 characters. There are also Qqueries. For each query, you are given a string, and you need to find out how many times this string occurred previously.
Input Format
The first line contains N, the number of strings.
The next N lines each contain a string.
The N+2nd line contains Q, the number of queries.
The following Q lines each contain a query string.
Constraints
1N1000
1Q1000
1 length of any string20
Sample Input
4
aba
baba
aba
xzxb
3
aba
xzxb
ab
Sample Output
2
1
0
Explanation
Here, "aba" occurs twice, in the first and third string. The string "xzxb" occurs once in the fourth string, and "ab" does not occur at all.

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    map<string,int>m;
    int n,q;
    string str;
   
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> str;
        m[str]++;
    }
    cin >> q;
    for(int i = 0; i < q; i++){
        cin >> str;
        cout << m[str] << endl;
    }
    return 0;
}