Friday, April 28, 2017

Solution of DevSkill DCP-316: Names

See the problem: DCP-316: Names

/*  Bismillahir Rahmanir Rahim 
    Its just a string multiplication.
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a, i, j, n, t, z, sz, ar[1000001];
string s;

void multiply(int i){  // string multiplication
    int carry=0, j;
    for(j=10000; j>10000-z; j--){
        int temp=ar[j]*i+carry;
        ar[j]=temp%10;
        carry=temp/10;
    }
    while(carry){
        ar[j--]=carry%10;
        carry=carry/10;
        z++;
    }
}

int main(){

    cin>>t;
    while(t--){
        cin>>n>>s;
        sz=s.size();
        ar[10000]=1;
        n=n-sz, z=1;
        for(i=1; i<=n; i++){
            multiply(26);
        }
        for(i=10001-z; i<=10000; i++) cout<<ar[i];
        cout<<endl;
    }
    return 0;
}

Calculating Average Using OOP (C++)

# include<iostream>
#include<conio.h>
using namespace std;

class array{
    float *f_a;
    public:
    array(int);
    ~array();
    void getdata(int);
    float average(int);
};

array::array(int k){
    f_a=new float[k];
    if(!f_a){
        cout<<"Allocation failure."<<endl;
    }
}

array::~array(){
    delete[]f_a;
}

void array::getdata(int l){
    cout<<"Enter "<<l<<" elements: ";
    for(int i=0; i<l;i++){
        cin>>f_a[i];
    }
}

float array::average(int m){
    float sum;
    for(int j=0;j<m;j++){
        sum+=f_a[j];
    }
    return(sum/m);
}

int main(){
    int n;
    cout<<"Number of elements: ";
    cin>>n;
    array f(n);
    f.getdata(n);
    cout<<"The average is: "<<f.average(n)<<endl;
    return 0;
}

Solution of Dev Skill DCP-237: What a problem!

See the problem:  DCP-237: What a problem!

/*  Bismillahir Rahmanir Rahim
    Its better to try yourself than follow others code.
    My solution Idea: 
    To store the array use one dimensional array 
    Now print according to their system. 
 */
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll i, j, n, k, cs=1, m, t, x, sz,  ar[1000006];

int main(){
     cin>>t;
     while(t--){
        cin>>n;
        k=1;
        for(i=1; i<=n; i++){
            for(j=1; j<=n; j++) cin>>ar[k++];
        } 
        cout<<"Case #"<<cs++<<": ";
        m=n+1;
        x=n*n-(n-1);
        if(n==1) cout<<ar[1];
        else{
            cout<<ar[x];
            for(i=1; ; i++){
                x++;
                cout<<" "<<ar[x];
                for(j=1; j<=i; j++){
                    x=x-m;
                    cout<<" "<<ar[x];
                }
                if(x==1||x==n*n) break;
                i++;
                x=x-n;
                cout<<" "<<ar[x];
                for(j=1; j<=i; j++){
                    x=x+m;
                    cout<<" "<<ar[x];
                }
                if(x==1||x==n*n) break;
            }
            if(x==1){
                for(i=2; ; i++){
                    x++;
                    cout<<" "<<ar[x];
                    if(x==n) break;
                    for(j=i; j<n; j++){
                        x=x+m;
                        cout<<" "<<ar[x];
                    }
                    if(x==n) break;
                    x=x-n;
                    cout<<" "<<ar[x];
                    if(x==n) break;
                    i++;
                    for(j=i; j<n; j++){
                        x=x-m;
                        cout<<" "<<ar[x];
                    }
                    if(x==n) break;
                }
            }
            else{
                for(i=2; i<n; i++){
                    if(x==n) break;
                    x=x-n;
                    cout<<" "<<ar[x];
                    for(j=i; j<n; j++){
                        x=x-m;
                        cout<<" "<<ar[x];
                    }
                    if(x==n) break;
                    x++;
                    cout<<" "<<ar[x];
                    if(x==n) break;
                    i++;
                    for(j=i; j<n; j++){
                        x=x+m;
                        cout<<" "<<ar[x];
                    }
                    if(x==n) break;
                }
            }
        }
        cout<<endl;
     }
    return 0;
}

Monday, April 24, 2017

Selection Sort - Implementation with C++

/*  Bismillahir Rahmanir Rahim
    Implementation of Selection-Sort using C++
*/
#include<bits/stdc++.h>
using namespace std;

int main(){
    int ar[1000], i, j, n, temp, loc, mn;
    cin>>n; // Enter the number of element
    for(i=0; i<n; i++) cin>>ar[i]; // all elements
    for(i=0; i<n-1; i++){
        mn=ar[i]; // let, its the minimum
        loc=i;    // location of minimum
        for(j=i+1; j<n; j++){
            if(ar[j]<mn){
                mn=ar[j]; // minimum updated
                loc=j;    // location of minimum
            }
        }
        temp=ar[i];
        ar[i]=ar[loc];
        ar[loc]=temp;
    }
    for(i=0; i<n; i++) cout<<ar[i]<<" ";
    cout<<endl;
    return 0;
}

Bubble Sort - Implementation with C++

/*  Bismillahir Rahmanir Rahim
    Implementation of Bubble-Sort using C++
*/
#include<bits/stdc++.h>
using namespace std;

int main(){
    int a[50],n,i,j,temp;
    cin>>n; // Enter the number of element
    for(i=0; i<n; i++) cin>>a[i]; // Enter all elements
    for(i=1; i<n; i++){
        for(j=0; j<(n-i); j++){
            if(a[j]>a[j+1]){
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
    for(i=0; i<n; i++) cout<<a[i]<<" ";
    cout<<endl;
    return 0;
}

Quick Sort - Implementation with C++

/*  Bismillahir Rahmanir Rahim
    Implementation of Quick-Sort using C++
*/
#include<bits/stdc++.h>
using namespace std;
int ar[1000];

int partition (int low, int high){
    int pivot=ar[high];
    int i=(low-1);
    for(int j=low; j<high; j++){
        if(ar[j]<=pivot){
            i++;
            swap(ar[i],ar[j]);
        }
    }
    swap(ar[i+1], ar[high]);
    return (i+1);
}

void quickSort(int low, int high){
    if(low<high){
        int pi=partition(low, high);
        quickSort(low, pi-1);
        quickSort(pi+1, high);
    }
}

int main(){
    int n,i;
    cin>>n;
    for(i=0;i<n;i++) cin>>ar[i];
    quickSort(0, n-1);
    for(i=0; i<n; i++) cout<<ar[i]<<" ";
    cout<<endl;
    return 0;
}

Merge Sort - Implementation with C++

/*  Bismillahir Rahmanir Rahim
    Implementation of Merge Sort using C++
*/
#include<bits/stdc++.h>
using namespace std;
int ar[1000];

int marg_partition(int st, int mid , int ed){
    int arr[1000], left, right, j=0, i;
    left=st;
    right=mid+1;
    while(left<=mid && right<=ed){
        if(ar[left]<ar[right]){
            arr[j++]=ar[left];
            left++;
        }
        else{
            arr[j++]=ar[right];
            right++;
        }
    }
    if(left>mid){
        for(i=right;i<=ed;i++)
            arr[j++]=ar[i];
    }
    else {
        for(i=left;i<=mid;i++)
            arr[j++]=ar[i];
    }
    for(i=0;i<j;i++) ar[i+st]=arr[i];
    return 0;
}

int marg_sort(int st,int ed){
    if(st<ed){
        int mid=(st+ed)/2;
        marg_sort(st,mid);
        marg_sort(mid+1,ed);
        marg_partition(st,mid,ed);
    }
}

int main(){
    int i,j,n,k;
    cin>>n;
    for(i=0;i<n;i++) cin>>ar[i];

    marg_sort(0,n-1);
    for(i=0;i<n;i++) cout<<ar[i]<<" ";
    cout<<endl;
    return 0;
}

Insertion Sort - C++ Implementation

Try to understand the following procedure.

77  33  44  11  88  22  66  55

33  77 . . . . . . . . . . . . . . . . . .
33  44  77 . . . . . . . . . . . . . . .
11  33  44  77 . . . . . . . . . . . .
11  33  44  77  88 . . . . . . . . .
11  22  33  44  77  88 . . . . . .
11  22  33  44  66  77  88 . . .
11  22  33  44  55  66  77  88 

The following Code represent the concept.
/*  Bismillahir Rahmanir Rahim
    Implementation of Insertion Sort using C++
*/
#include<bits/stdc++.h>
using namespace std;

int main(){
    int ar[100000], n, i, j, key;
    cin>>n;  // number of elements
    for(i=0; i<n; i++) cin>>ar[i]; // input elements one by one
    
    for(i=1; i<n; i++){ // starting from 2nd element
        key=ar[i]; 
        j=i-1; 
        while(j>=0 && key<ar[j]){ //comparing key with all previous
            ar[j+1]=ar[j]; 
            j--;
        }
        ar[j+1]=key; 
    }
    
    for(i=0;i<n;i++) cout<<ar[i]<<" ";
    return 0;
}

Friday, April 7, 2017

Random Value Generator in C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*  Bismillahir Rahmanir Rahim
    Random Value Generating
    Input any integer(n).
*/
#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
     int n;
     while(cin>>n){// for understanding the output clearly
         cout<<"Random value (any integer)   = "<<rand()<<endl;
         cout<<"Random value (from 0 to 9)   = "<<rand()%10<<endl;
         cout<<"Random value (from 1 to 100) = "<<rand()%100+1<<endl;
         cout<<"Random value (from 3 to 8)   = "<<rand()%6+3<<endl;
     }
     return 0;
}