Monday, August 29, 2016

Time Conversion

Problem Link (Click here)

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

int main(){
    string time;
    string temp;
    cin >> time;
    temp = time;
    if(time[8]=='A'){
        time.erase (8,2);
        if(time[0]=='1'&&time[1]=='2'){
            time[0]='0';
            time[1]='0';
        }
        cout<<time;
    }
    else{
        time.erase (2,8);
        stringstream ss(time);
        int x;
        ss >> x;
        if(x!=12){
            x=x+12;
        }
        cout<<x;
        temp.erase (0,2);
        temp.erase (6,2);
        cout<<temp;
    }

    return 0;

}


Sunday, August 28, 2016

COUNTING SORT WITH NEGATIVE VALUE

#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    int temp_p[100];
    int temp_n[100];
    int m,n,i,x;
    int j=0;
    int p=0;
    cout<<"Total number : ";
    cin>>n;
    cout<<"Maximum number : ";
    cin>>m;
    int pos[m+1];
    int neg[m+1];
    memset(pos, 0, sizeof pos);
    memset(neg, 0, sizeof neg);
    for(i=0;i<n;i++){
        cin>>x;
        if(x>=0){
            temp_p[j]=x;
            pos[x]++;
            j++;
        }
        else{
            temp_n[p]=x*-1;
            neg[x*-1]++;
            p++;
        }
    }
    for(i=1;i<=m;i++){
        pos[i]=pos[i]+pos[i-1];
        neg[i]=neg[i]+neg[i-1];
    }
    int arry_p[j];
    int arry_n[p];

    for(i=0;i<j;i++){
        int b=temp_p[i];
        arry_p[pos[b]-1]=b;
        pos[b]--;
    }
    for(i=0;i<p;i++){
        int b=temp_n[i];
        arry_n[neg[b]-1]=b;
        neg[b]--;
    }
    for(i=p-1;i>=0;i--){
        cout<<(arry_n[i]*-1)<<" ";
    }
    for(i=0;i<j;i++){
        cout<<arry_p[i]<<" ";
    }
    cout<<endl;


    return 0;
}

COUNTING SORT

#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    int temp[100];
    int m,n,i;
    cout<<"Total number : ";
    cin>>n;
    cout<<"Maximum number : ";
    cin>>m;
    int c[m+1];
    memset(c, 0, sizeof c);
    for(i=0;i<n;i++){
        cin>>temp[i];
        int a=temp[i];
        c[a]++;
    }
    for(i=1;i<=m;i++)
        c[i]=c[i]+c[i-1];

    int arry[n];
    for(i=0;i<n;i++){
        int b=temp[i];
        arry[c[b]-1]=b;
        c[b]--;
    }
    for(i=0;i<n;i++){
        cout<<arry[i]<<" ";
    }
    cout<<endl;

    return 0;
}

Tuesday, August 2, 2016

Kruskal's algorithm

#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

struct node{
    int s;
    int e;
    int weight;
};

int makeset(int a[],int e){
    for(int i=0;i<=e;i++)\
        a[i]=i;
    return a[e];
}
int found(int arry[], int x){
    if(x==arry[x])
        return x;
    return found(arry, arry[x]);
}
int Union(int arry[],int x,int y){
    if(x!=y)
        arry[y]=x;
}

bool compare(node a, node b){
    if(a.weight<b.weight)
        return true;
    else
        return false;
}

int main(){

    int i, totalEdge, totalNode, p1, p2, w;
    node edgeList[50];
    cout << "How many vertices?  " ;
    cin>>totalNode;
    cout << "How many edges?  " ;
    cin>>totalEdge;

    for(i=0;i<totalEdge;i++){
        cin>>p1;
        cin>>p2;
        cin>>w;

        edgeList[i].s = p1;
        edgeList[i].e = p2;
        edgeList[i].weight = w;
        edgeList[i].s = p2;
        edgeList[i].e = p1;
    }
    sort(edgeList,edgeList+totalEdge, compare);
    int arry[totalEdge];
    makeset(arry,totalEdge);

    int total=0;
    for(int i=0; i<totalEdge; i++){
        int x=found(arry, edgeList[i].s);
        int y=found(arry, edgeList[i].e);
        if(x!=y){
            Union(arry,x,y);
            total+=edgeList[i].weight;
            cout << edgeList[i].s << " " << edgeList[i].e << " " << edgeList[i].weight << endl;
        }
    }
    cout<< total<<endl;


    return 0;
}