Ronzii's Blog

Just your average geek's blog

Random Number Generation

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int rand5()
{
    return rand()%5;
}
int rand7()
{
    int vals[5][5] =
    {
        { 1, 2, 3, 4, 5 },
        { 6, 7, 1, 2, 3 },
        { 4, 5, 6, 7, 1 },
        { 2, 3, 4, 5, 6 },
        { 7, 0, 0, 0, 0 }
    };

    int result = 0;
    while (result == 0)
    {
        int i = rand5();
        int j = rand5();
        result = vals[i-1][j-1];
    }
    return result;
}
int main()
{
    srand(time(NULL));
    cout<<rand7();
    return 0;
}
Advertisements

March 29, 2012 Posted by | C++ | , , , | Leave a comment

Shortest distance between 2 Integers


#include <iostream>
#include <climits>
using namespace std;
int dist(int a[],int n, int x, int y)
{
    int a1=-1,a2=-1, min_distance = INT_MAX;
    for(int i=0;i<n;i++)
    {
        if(a[i]==x)
        {
            a1 = i;
            int distance = a1-a2;
            if(a2>=0 && distance<min_distance)
            {
                min_distance = distance;
            }
        }
        else if(a[i]==y)
        {
            a2 = i;
            int distance = a2-a1;
            if(a1>=0 && distance<min_distance)
            {
                min_distance = distance;
            }
        }
    }
    return min_distance;
}
int main()
{
    int a[] = {6,5,5,5,1,2,4,4,6,6,3,4,3,4,1,2,4};
    int size= sizeof(a)/sizeof(a[0]);
    cout<<dist(a,size,1,6);
    return 0;
}

March 29, 2012 Posted by | C++ | , , , , | Leave a comment

Number to String

#include <iostream>
#include <cstring>
using namespace std;
string digits[] = {"one","two","three","four","five","six","seven","eight","nine"};
string teens[] = {"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
string tens[] = {"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
string bigs[] = {"","thousand","million"};
string numToString100(int n)
{
    string result = "";
    if(n>=100)
    {
        result+= digits[n/100-1] + " hundred ";
        n%=100;
    }
    if(n>=11 & n<=19)
    {
        result+= teens[n-11]+ " ";
    }
    else if(n==10 || n>=20)
    {
        result+= tens[n/10-1]+ " ";
        n%=10;
    }
    if(n>=1 && n<=9)
    {
        result += digits[n-1] + " ";
    }
    return result;
}
string numToString(int n)
{
    string result="";
    if(n==0)
    {
        return "zero";
    }
    else if(n<0)
    {
        result= "negative " + numToString(n*-1);
    }
    int count = 0;
    while(n>0)
    {
        if(n%1000!=0)
        {
            result = numToString100(n%1000) + bigs[count] + " " + result;
        }
        n/=1000;
        count++;
    }
    return result;
}

int main()
{
    cout<<numToString(-19323984);
    return 0;
}

March 29, 2012 Posted by | C++ | , , , | Leave a comment

Bit Set

#include <iostream>
using namespace std;
class BitSet
{
    int *bitset;
public:
    BitSet(int size)
    {
        bitset = new int[size>>5];
        for(int i=0; i<size>>5; i++)
        {
            bitset[i] = 0;
        }
    }
    bool get(int k)
    {
        return (bitset[k>>5] & (1<<(k&0x1F)))!=0;
    }
    void set(int k)
    {
        bitset[k>>5]|=1<<(k&0x1F);
    }
};
int main()
{
    BitSet s(100);
    int a[] = {1,5,1,7,8,3,7,8,7,2,67,9,54};
    int size = sizeof(a)/sizeof(a[0]);
    for(int i=0; i<size; i++)
    {
        if(s.get(a[i])) cout<<a[i]<<" ";
        else s.set(a[i]);
    }
    return 0;
}

March 29, 2012 Posted by | C++ | , , , , | Leave a comment