Thursday, 26 February 2015

Uva-11462 - Age Sort Solution

/*Uva-11462 - Age Sort Solution*/

Solution:

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

    int n;
    while(scanf("%d",&n)==1 && n){
        for(int i = 0; i < n; i++){
            scanf("%d",&ar[i]);
        }
        sort(ar,ar+n);
        for(int i = 0; i < n; i++){
            printf("%ld",ar[i]);
            if(i < n-1)printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Wednesday, 25 February 2015

CodeFight-decimal to octal Solution.

/*Convert decimal to octal then sum all digits in octal numeral system.*/

Solution:

/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
unsigned int Octal(unsigned int n) {
   return n<8?n:n%8+Octal(n/8);
}

Saturday, 21 February 2015

Uva - 572 - Oil Deposits Solution.

/* Uva 572 - Oil Deposits */

Solution:

#include<bits/stdc++.h>
using namespace std;
int m,n;
char str[1002][1002];
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
void DFS(int x,int y){

    if(x < 0 || y < 0 || x == m || y == n)return;
    if(str[x][y] != '@')return;
    if(str[x][y] == '@')str[x][y] = '*';
    DFS(x,y+1);
    DFS(x,y-1);
    DFS(x+1,y);
    DFS(x-1,y);
    DFS(x+1,y+1);
    DFS(x+1,y-1);
    DFS(x-1,y+1);
    DFS(x-1,y-1);
}
int main(){
    int cnt;
    while(scanf("%d %d",&m,&n)!=EOF){
        if(m == 0)break;
        for(int i = 0; i < m;i++){
            for(int j = 0; j < n; j++){
                cin >> str[i][j];
            }
        }
        cnt = 0;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(str[i][j] != '*'){
                  DFS(i,j);
                  cnt++;
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

Friday, 20 February 2015

UVa-10336-Rank the Languages Solution

/* UVa-10336-Rank the Languages Solution */

Solution:

#include<bits/stdc++.h>
using namespace std;
int n,mm,flg;
char str[1002][1002],ch;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
void DFS(int x, int y){

    if(str[x][y] == '*')return;
    if(x < 0 || y < 0 || x == n || y == mm)return;
    if(str[x][y] != ch)return;
    if(str[x][y] == ch){
        str[x][y]='*';
        flg = 1;
    }
    DFS(x,y+1);
    DFS(x,y-1);
    DFS(x+1,y);
    DFS(x-1,y);
}
struct val{
    char word;
    int continar = 0;
};
bool cmp(val a, val b){
    if(a.continar == b.continar)return a.word < b.word;
    return a.continar > b.continar;
}
int main(){

    int T, cnt[30];
    cin >> T;
    for(int t = 1; t <= T;t++){
        cin >> n >> mm;
        memset(cnt, 0, sizeof(cnt));
        val v[30];
        for(int i = 0; i < n; i++){
            for(int j = 0; j < mm; j++){
                cin >> str[i][j];
            }
        }
        for(int i = 0; i < n; i++){
            for(int j = 0; j < mm; j++){
               if(str[i][j] != '*'){
                   ch = str[i][j];
                   DFS(i,j);
                   cnt[ch - 'a']++;
               }
            }
        }
        int k = 0;
        for(int i = 0; i < 26; i++){
            if(cnt[i] != 0){
                v[k].word = 'a'+i;
                v[k].continar = cnt[i];
                k++;
            }
        }
        sort(v,v+k,cmp);
        cout << "World #" << t << endl;
        for(int i = 0; i < k;i++){
            if(v[i].continar != 0)cout << v[i].word << ": " << v[i].continar << endl;
        }
    }
    return 0;
}

Thursday, 19 February 2015

Zeros in binary digits Solution.

/* Let N be an integer, you should calculate number of zeros in binary representation of N. N will begin with '1' in binary. e.g. zeros(5) = 1 because 5 = (101)b, thus it has 1 zero.*/

/*CodeFight*/

Solution:

#include<bits/stdc++.h>
using namespace std;
int z;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int zeros(int n) {
  if(n/2 != 0) {
      zeros(n/2);
  }
  if(n%2 == 0)z++;
  return z;
}
int main(){

    cout << zeros(5) << endl;;
}

Find The Lonely Number C++ Solution.

/*A number is lonely if it only appears in a list once. If a number appears in the list twice it is not lonely. Given a list of numbers where every number in the list appears twice except one number that appears only once. You need to find the lonely number.*/

Solution:

#include<bits/stdc++.h>
using namespace std;
int cnt[1000000];
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int LonelyNumber(vector<int> nums) {

    int lgVal = 0;
    for(int i = 0; i < nums.size(); i++){
        cnt[nums.at(i)]++;
        if(nums.at(i)>lgVal)lgVal = nums.at(i);
    }
    int mx = -1;
    for(int i = 0; i <= lgVal;i++){
        if(cnt[i] == 1){
            mx = i;
        }
    }
    return mx;
}
int main(){

    vector<int>v;
    int n,a;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> a;
        v.push_back(a);
    }
    int ans = LonelyNumber(v);
    cout << ans << endl;
    return 0;
}

Thursday, 12 February 2015

How Many Integers whose digits is 4,7 or 0.

/**Find out how many positive integers exists that are smaller than N and can be represented using only 0, 4, 7. For example: N = 100, Answer =  8 (Those numbers are: 4, 7, 40, 44, 47, 70, 74, 77)   N = 44, Answer = 3
**/

Solution:

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

    int cnt = 0;
    for(int i = 1; i < N; i++){
        int number = i;
        bool check = true;
        while(number > 0){
            int mod = number%10;
            if(mod != 4 && mod != 7 && mod != 0){
                check = false;
                break;
            }
            number/=10;
        }
        if(check)cnt++;
    }
    return cnt;
}
int main(){
    int N;
    cin >> N;
    cout << HowManyIntegers(N) << endl;
    return 0;
}

Wednesday, 11 February 2015

Pro-Palindrome Or Not C++ Solution.

/*
 * Pro Palindrome :: Suppose a = 1,b = 2,c = 3,……,z = 26.
 * Now if “abc” is a string then you can transform it to “123”.
 * Same as “au” is a string then you can transform it to “121”.
 * A string is a Pro Palindrome if its transform string is a palindrome.
 * So, “abc” is not a Pro Palindrome but “au” is a Pro Palindrome.

 */

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
struct helper{
    int check(char c){
        int i = 1;
        for(char a = 'a'; a <= 'z'; a++){
            if(c == a){
                return i;
            }
            i++;
        }
    }
};
int main(){
    helper h;
    string str,s1,s2;
    cin >> str;
    for(int i = 0; i < str.size(); i++){
        stringstream ss;
        ss <<(h.check(str[i]));
        s1+=ss.str();
    }
    s2 = s1;
    reverse(s2.begin(),s2.end());
    if(s1 == s2)cout << "proPalindrome" << endl;
    else cout << "Not proPalindrome" << endl;
    return 0;
}

Project Euler Problem 2 Solution - Even Fibonacci numbers C++ Solution.

/* Project Euler Problem #2 -Even Fibonacci numbers */

Solution:

#include<iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    long long fib = 1,a = 0,b = 1,ans = 0;
    while(fib < 4000000){
        fib=a+b;
        b = a;
        a = fib;
        if(!(fib%2))ans+=fib;
    }
    cout << ans << endl;
}

Uva 11530 - SMS Typing Solution

/* Uva 11530 - SMS Typing Solution */

Solution:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int T;
    char c;
    scanf("%d%c",&T,&c);
    for(int t = 0; t < T;t++){

        char s[200];
        gets(s);
        int len = strlen(s);
        int i,ans = 0;
        for(i = 0; i < len; i++){
            if(s[i]=='a'||s[i]=='d'||s[i]=='g'||s[i]=='j'||s[i]=='m'||s[i]=='p'||s[i]=='t'||s[i]=='w'||s[i]==' '){
                ans+=1;
            }
            else if(s[i]=='b'||s[i]=='e'||s[i]=='h'||s[i]=='k'||s[i]=='n'||s[i]=='q'||s[i]=='u'||s[i]=='x'){
                ans+=2;
            }
            else if(s[i]=='c'||s[i]=='f'||s[i]=='i'||s[i]=='l'||s[i]=='o'||s[i]=='r'||s[i]=='v'||s[i]=='y'){
                ans+=3;
            }
            else if(s[i]=='s'||s[i]=='z'){
                ans+=4;
            }
        }
        cout << "Case #" << t+1 << ": " << ans << endl;
    }
    return 0;
}


Solution 2:


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

int main() {
    int T;
    cin >> T; cin.ignore();
    for(int t = 1; t <= T; t++){
        string str;
        getline(cin,str);
        int val = 0,len = str.length();
        for(int i = 0; i < len; i++){
            if(str[i] == 'a' || str[i] == 'b' || str[i] == 'c')val+= (str[i] - 'a')+1;
            else if(str[i] == 'd' || str[i] == 'e' || str[i] == 'f')val+= (str[i] - 'd')+1;
            else if(str[i] == 'g' || str[i] == 'h' || str[i] == 'i')val+= (str[i] - 'g')+1;
            else if(str[i] == 'j' || str[i] == 'k' || str[i] == 'l')val+= (str[i] - 'j')+1;
            else if(str[i] == 'm' || str[i] == 'n' || str[i] == 'o')val+= (str[i] - 'm')+1;
            else if(str[i] == 'p' || str[i] == 'q' || str[i] == 'r' || str[i] == 's')val+= (str[i] - 'p')+1;
            else if(str[i] == 't' || str[i] == 'u' || str[i] == 'v')val+= (str[i] - 't')+1;
            else if(str[i] == 'w' || str[i] == 'x' || str[i] == 'y' || str[i] == 'z')val+= (str[i] - 'w')+1;
            else if(str[i] == ' ')val+=1;
        }
        cout << "Case #" << t << ": " << val << endl;
    }
    return 0;
}

Uva 11417 - GCD Solution

/*Uva 11417 - GCD Solution */

Solution:

#include<iostream>
#include<cstdio>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int gcd(int a, int b){

    int temp;
    while (b != 0){
        temp = a % b;
        a = b;
        b = temp;
    }
    return a;
}
int main(){

    int N,G;
    while(scanf("%d",&N) == 1){
      if(N == 0)break;
      G = 0;
      for(int i = 1; i < N; i++){
        for(int j = i+1; j <= N; j++){
          G+=gcd(i,j);
        }
      }
      printf("%d\n",G);
    }
    return 0;
}

Neighbor String Or Not C++ Solution.

/*Neighbor String Or Not*/

Solution:

#include <iostream>
#include<string>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
 string str;
 bool check = true;
 cin >> str;
 for(int i = 1; i < str.size();i++){
  if(str[i] != str[i-1]+1){
   check = false;
   break;
  }
 }
 if(check)cout << "Input String Is Neighbor String." << endl;
 else cout << "Input String Is Not Neighbor String." << endl;
 return 0;
}