SFML for Codeblocks Setup Tutorial

What is SFML?

SFML is a simple to use and portable API written in C++. You can think of it as an object oriented SDL. SFML is made of modules in order to be as useful as possible for everyone. You can use SFML as a minimalist window system in order to use OpenGL, or as a complete multimedia library full of features to build video games or multimedia softwares.

Homepage: http://www.sfml-dev.org/

Installation:

This page provides detailed installation steps. But here i will try to explain.

  1. Since we are setting up codeblocks download the SFML TDM(SJLJ) version. Download 32 bit if you want maximum support. 32 bits run on both 32 and 64 bit systems.
  2. Now unzip and keep in your folder of choice. It’s best to use a path like, c:\sfml\
  3. Go to Settings -> Compiler -> Search Directories -> compiler ( sub tab ) then click Add. Now locate the folder where you unzipped sfml and find the include folder. For example, C:\SFML-2.2\include 

    compiler settings
    compiler settings
  4. Next goto Linker ( sub tab ) click Add and find the lib folder of unzipped sfml. For example, C:\SFML-2.2\lib

    linker lib
    linker lib
  5. Now open codeblocks. Go to Settings -> Compiler -> Linker Settings we can see anadd button here. If we click it we are shown .a files.

    linker settings
    linker settings
  6. File names are prefixed with lib also with .a extension. We do not need that. For example, if the library you want to add is named as, libsfml-graphics.a we can just type in sfml-graphics after clicking on add button. We must omit lib and .a part in the name.
  7. We also need to maintain order of libraries. For example, we add sfml-graphics, sfml-window, then sfml-system.
  8. Also if you are wondering what those .a files with -s-d stand for. -s stands for static and -d stands for debug. I’ve setup the version with out static. But if want to use static linkers settings for example, sfml-graphics-s then we need another step. Goto compiler settings tab, then goto #defines sub tab and write the following, SFML_STATIC.
  9. Now a crucial step when you create a project or a file copy all the dll files located in your unzipped sfml folder bin directory to your executable directory. Means basically copy dll files to where your cpp file is located. 

    add dll to cpp location
    add dll to cpp location
  10. Now create a cpp file and copy the codes located below this page to your cpp and Run. If it runs then you have successfully setup sfml.  Now start creating apps and games. Happy coding 🙂

 

UVA Problem 10935 – Throwing cards away I Solution

UVA Problem 10935 – Throwing cards away I Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

It is possible possible to solve this problem in many ways. Using STL queue is one way. But I try to avoid built-in functions just so I can make my base stronger and learn new things. Here I have written my own queue implementation.

The problem says we have a deck of card which is ordered 1 to n. n is the input. Ex, if 7 is input then we have 1,2,3,4,5,6,7 cards on deck.

Next, it says discard the top card, so card after him is now topmost. Then move the topmost card of the deck to the end of the deck. Keep continuing this until two card are left of deck. The process happens like this, if n = 5 is given,

1 2 3 4 5 /* Initial array enqueued 1 through 5 */

/* First Iteration */
2 3 4 5   /* 1 is discarded and printed */
3 4 5 2   /* 2 is moved to end of queue */

/* Next Iteration */
4 5 2    /* 3 discarded and printed */
5 2 4    /* 4 moved to end of queue */

/* Next Iteration */
2 4     /* 5 discarded and printed */
4 2     /* 4 is also discarded since we want to find the last card and so we print the last card reaming on deck which is 2 */

Notice even though we discard 1 card we move the other one to the end of the deck. So the deck size is decreasing by one ( don’t get confused here ).

Since one card is thrown away and another is moved to the end i thought hey its a queue. Why? queue has a function called dequeue which discards the topmost ( or actually the element in front ). Now another card is moved to end of the deck. Queue has a similar function called enqueue which adds an element to the end of the array or queue. Also since Queue is a FIFO ( FIRST IN FIRST OUT ) data structure. So what is on the front of deck is processed first and that is what we need.

Think of it like a game where each cards are aligned horizontally and each card is a person. Now we tell the player who is in front of the line to move away so the player that was after him is in front of the line. This time we tell the player to move to the end of line. We continue this discard and adding the next one to the end  until there are only two player left. So of these two player the last player is the remaining cards and the players that were moved from line are the discarded cards.

Our input terminates with 0 ( zero ). I have explained the code below with comments.

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.


Input:

7
19
10
6
0

Output:

Discarded cards: 1, 3, 5, 7, 4, 2
Remaining card: 6
Discarded cards: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 4, 8, 12, 16, 2, 10, 18, 14
Remaining card: 6
Discarded cards: 1, 3, 5, 7, 9, 2, 6, 10, 8
Remaining card: 4
Discarded cards: 1, 3, 5, 2, 6
Remaining card: 4

Code:

/*
 * @author quickgrid ( Asif Ahmed )
 * @link   https://quickgrid.wordpress.com
 */

#include<stdio.h>
#define size 128

/*
 * unsigned since no item in our array is negative
 */
unsigned a[size], f = 0, r = 0;

void enqueue(unsigned k){
    /*
     * get a valid index in circular queue array
     */
    unsigned s = (r + 1) % (size + 1);

    /*
     * if true queue full, this check is for safety and can be deleted
     */
    if (f == s)
        return;

    /*
     * set the last element to given card
     */
    a[s] = k;
    /*
     * update the rear position marker
     */
    r = s;
}

unsigned dequeue(){
    /*
     * if true then queue empty, this check is for safety and can be deleted
     */
    if (f == r)
        return 0;

    /*
     * move the front marker one position in circular queue array
     */
    f = (f + 1) % (size + 1);
    /*
     * keep the value of last card for returning
     */
    unsigned k = a[f];
    a[f] = 0;
    return k;
}

int main(){
    register unsigned n, i;

    while (scanf("%u", &n) && n){
        /*
         * since there is one card there is no discarded card
         */
        if (n == 1){
            printf("Discarded cards:\n");
            printf("Remaining card: 1\n");
            continue;
        }

        f = 0, r = 0;
        for (i = 1; i <= n; ++i){
            /*
             * it says 1 to n cards on deck so i enqueue all cards serially
             */
            enqueue(i);
        }

        printf("Discarded cards: ");
        /*
         * stop when 2 cards on deck
         */
        while (f + 2 != r){
            printf("%u, ", dequeue());
            unsigned k = dequeue();
            enqueue(k);
        }

        /*
         * get position of last discardable card
         */
        f = (f + 1) % (size + 1);

        /*
         * print the last discardable card
         */
        printf("%u\n", a[f]);
        /*
         * now print the last remaining card on deck
         */
        printf("Remaining card: %u\n", a[r]);
    }
    return 0;
}

UVA Problem 10878 – Decode the tape Solution

UVA Problem 10878 – Decode the tape Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

The problem may seem easy to some and maybe hard for some. There are multiple ways of solving this. Please take a look at this if don’t have a good understanding if binary numbers. Also learn Bitwise operations. Do not print a new line for this problem.

At first glance it doesn’t make sense. But trying to match characters we see a pattern. Like whenever we see

|  o  .   |

we see there is a space. Each line represents a character we can find this easily by counting characters in the given output and counting lines on input tape. This we can match every character to its corresponding line on the tape. But in the last line we see there is an extra line on the ( not the blank line ) tape. That represents the newline.

Now there are only two types of data on the tape a space character and a hole ( o character ). One example for problem like this when writing in the exams we fill in ID box with pencils by circling the numbers that match our id number.

So since there are only two types ( Forget . and | ) of data it is either 0 or 1 meaning binary. So now when we assign a value to each binary digits we can see that when we add values for 1 we get ASCII value of the character.  

Now that we understand the problem we can solve this in different ways. One approach can be for every loop we can check the value of that character. It is possible since the tape length is always same. if it is 1 we add the corresponding decimal number for that binary digit. Ex:

/* I skip s[0] because it is not a space or o character in tape */
if(s[1] == 'o') sum = sum + 128;
if(s[2] == 'o') sum = sum + 64;
/* keep going like this for each hole or space character */

My approach is bit shifting. By bit shifting we get the same result. Since the string consisting of ( space and hole ) is always 8 characters long. Starting from left we can assign values by increasing power by 1 for each value to left.

128  64  32  16  8   4   2   1
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
 1   1   1   1   1   1   1   1

If I search the string from left then I need set bit to 128 and shift right ( making them smaller for each values to right ). That what I did here. So here values are decreasing right to left. 

Another approach is start from the end of string and set the bit to 1. Now we shift bits to left every time we find a space or a hole. So, here values are increasing left to right.

for(i = strlen(s) - 1; i >= 0; --i){
        if(s[i] == ' '){
            bit <<= 1;
        }else if(s[i] == 'o'){
            sum += bit;
            bit <<= 1;
        }
}

Important:  The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.


Input:

___________
| o   .  o|
|  o  .   |
| ooo .  o|
| ooo .o o|
| oo o.  o|
| oo  . oo|
| oo o. oo|
|  o  .   |
| oo  . o |
| ooo . o |
| oo o.ooo|
| ooo .ooo|
| oo o.oo |
|  o  .   |
| oo  .oo |
| oo o.ooo|
| oooo.   |
|  o  .   |
| oo o. o |
| ooo .o o|
| oo o.o o|
| ooo .   |
| ooo . oo|
|  o  .   |
| oo o.ooo|
| ooo .oo |
| oo  .o o|
| ooo . o |
|  o  .   |
| ooo .o  |
| oo o.   |
| oo  .o o|
|  o  .   |
| oo o.o  |
| oo  .  o|
| oooo. o |
| oooo.  o|
|  o  .   |
| oo  .o  |
| oo o.ooo|
| oo  .ooo|
|  o o.oo |
|    o. o |
___________

Output:

A quick brown fox jumps over the lazy dog.

Code:

/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 */

#include<stdio.h>

static char s[64];

int main(){
    register unsigned int i;
    unsigned sum, found;

    while(gets(s)){
        unsigned int bit = 128;     /* Since the binary form is 8 characters long ( not including . or | ) 2^7 = 128 ( 7 but not 8 starts from 0. 2^0....2^7 total 8 digits ) we keep left shifting and adding only if we find a hole */

        i = sum = found = 0;
        if(s[i] != '|') continue;   /* We don't want to print anything for lines not starting with | */

        for(; s[i]; ++i){
            if(s[i] == ' ')
                bit >>= 1;          /* keep shifting bits right ( makes them smaller. Ex, 128 = 2^7. So, 128 >> 1 = 64, 2^7 >> 1 = 2^6 = 64 ) OR, you can shift bits to left make them bigger but then you have search the string in reverse order */
            else if(s[i] == 'o'){
                sum += bit;         /* Add the bits where we find a hole */
                bit >>= 1;
            }
        }

        printf("%c", sum);          /* Print the sum. No need to print newline. The last line of tape represents a newline character |    o. o | */
    }
    return 0;
}

UVA Problem 10924 – Prime Words Solution

UVA Problem 10924 – Prime Words Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

This problem is easy we just need to read it carefully.

The problem assigns values for 1 to 26 for (a-z) and 27 – 52 for (A-Z). The input is a stringWe are asked to find the sum of every character with given value and check if the sum is a prime number or not.  

Summing is actually easy. For lower case when we sum, we just subtract the character from ASCII value of a or the ‘a’ character. This way we get 1 to 25 for ( a to z ). Since the number that was assigned was 1 more, so we add 1 to every value in loop. We do the same thing for upper case letters ( subtract ‘A’ from each character ). So we get 0 to 25 again. This time we add 27 to every value in loop so we get 27 to 52 for ( A to Z ).

One important thing to note is although 1 is not a prime. In this problem it is specified that it is prime. We output according to given specification.  So if the sum of all given word is 1 then the output is prime. Ex, a ( since a is 1 ).

The given word length is 1 to 20. So an array of 21 is enough. We should check for EOF to terminate the program.

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.


Critical Inputs:

UFRN
contest
AcM
a

Output:

It is a prime word.
It is not a prime word.
It is not a prime word.
It is a prime word.

Code (using lookup table, Sieve of Eratosthenes):

/**
 * @author  Quickgrid ( Asif Ahmed )
 * @link    https://quickgrid.wordpress.com
 */

#include<stdio.h>
#include<math.h>

/**
 * Since there is max of 20 characters in a single line
 * Calculating with biggest value 'Z' gives a max of 1040
 */
#define N 1041

static int table[128];
static unsigned primes[N];

void PreSum(){
    register unsigned i, j = 1;

    /**
     * Set 1 to 26 for 'a' to 'z'
     */
    for (i = 'a'; i <= 'z'; ++i, ++j)
        table[i] = j;

    /**
     * Set 27 to 52 for 'a' to 'z'
     */
    for (i = 'A'; i <= 'Z'; ++i, ++j)
        table[i] = j;
}

void SieveOfEratosthenes(){
    register unsigned i, j;

    /**
     * For this problem though 0 and 1 are primes
     */
    for (i = 0; i < N; ++i)
        primes[i] = 1;

    unsigned len = sqrt(N);

    for (i = 2; i <= len; ++i){
        if (primes[i]){
            for (j = i * i; j <= N; j += i)
                primes[j] = 0;
        }
    }
}

int main(){
    /**
     * Pre calculate character value table
     * Pre calculate prime values using Sieve of Eratosthenes
     */
    PreSum();
    SieveOfEratosthenes();

    char s[32];
    register unsigned i;

	while (gets(s)){
        unsigned sum = 0;

        /**
         * Get character value from pre calculated character value table and sum them
         */
        for (i = 0; s[i]; ++i)
            sum += table[s[i]];

        /**
         * Get primality from pre calculated prime table
         */
        if (primes[sum])
            printf("It is a prime word.\n");
        else
            printf("It is not a prime word.\n");
	}
	return 0;
}

Code (without using table):

/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 */

#include<stdio.h>
#include<math.h>

static char s[32];

int main(){
    while (gets(s)){
        unsigned int i, sum = 0;

        for (i = 0; s[i]; ++i){
            if (s[i] >= 'a' && s[i] <= 'z')
                sum += s[i]-'a'+1;
            else
                sum += s[i]-'A'+27;
        }

        if (sum <= 2)
            printf("It is a prime word.\n");
        else if (sum % 2 == 0)
            printf("It is not a prime word.\n");
        else{
            unsigned int prime = 1, len = sqrt(sum);
            for(i = 3; i <= len; i += 2){
                if(sum % i == 0){
                    printf("It is not a prime word.\n");
                    prime = 0;
                    break;
                }
            }
            if (prime)
                printf("It is a prime word.\n");
        }
    }
    return 0;
}

UVA Problem 11636 – Hello World! Solution

UVA Problem 11636 – Hello World! Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

Here i have given Three solution one is Hybrid ( pre-calculated values with little calculations, Rank 1821 ) , another is with pre-caculated values stored in table ( see Memoization and Look up table ) ,last one is pure calculation. For this problem i have found out all answer combinations. So you can use your logic to get your code faster.

The problem requires us to find minimum number of copy commands to reach input lines.

For example, if there is 1 line and we copy whats on-screen 4 times we get 16 lines. How?

1 line copy paste 2 lines (since there was 1 line and we copied 1 more line)
2 line copy paste 4 lines (since there was 2 line and we copied 2 more line)
4 line copy paste 8 lines (since there was 4 line and we copied 4 more line)
8 line copy paste 16 lines (since there was 8 line and we copied 8 more line)

As we can what we copy is twice ( 2 times ) the amount of original.

Now we can use this knowledge to find the copy commands required. We can just loop until we find that our line number exceeds the given line. Now as many times as we looped is the copy command required.

Because we can copy 11 lines, 10 lines or 9 lines in 4 commands. We weren’t given any restriction on this. Just looping until our copy lines for the loop exceed the given line is enough.

Also Critical thing is input can be 0, 1 both for these the output is 0. For 1 the answer is 0 because. There is already a line so it takes 0 command to get 1 line.

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.


Critical Inputs:

21
99
0
1
1001
101
11
-1

Output:

Case 1: 5
Case 2: 7
Case 3: 0
Case 4: 0
Case 5: 10
Case 6: 7
Case 7: 4

Code Easier:

/**
 * @author Quickgrid ( Asif Ahmed )
 * @link https://quickgrid.wordpress.com
 */

#include<stdio.h>

int main(){
    int n;
    register unsigned c = 1;
    while(scanf("%d", &n) && n>-1){
        register unsigned i = 1, p = 0;
        while(i < n){
            /*
             * In the equation 2^p > n, for which value is greater than input, Here p is the answer
             */
            i <<= 1;
            ++p;
        }
        printf("Case %u: %u\n", c, p);
        ++c;
    }
    return 0;
}

Faster Solution ( Rank: 1821, Run time: 9 ms  ):

/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 */
#include<stdio.h>
int main(){
    int n;
    register unsigned int c = 1;
	while(scanf("%d", &n) && n>-1){
        if(n>1){
            if(n > 8192)
                printf("Case %u: 14\n", c);
            else if(n >= 4097)
                printf("Case %u: 13\n", c);
            else if(n >= 2049)
                printf("Case %u: 12\n", c);
            else if(n >= 1025)
                printf("Case %u: 11\n", c);
            else{
                register unsigned int i = 1, lines = 1;
                for(; i<=n; ++i){
                    lines <<= 1;
                    if(lines >= n){
                        break;
                    }
                }
                printf("Case %u: %u\n", c, i);
            }
        }else printf("Case %u: 0\n", c);
        ++c;
	}
	return 0;
}

Another Solution ( with Look Up Table ):

/*
 * @author Quickgrid ( Asif Ahmed )
 * @link   https://quickgrid.wordpress.com
 */

#include<stdio.h>
static unsigned arr[10010];

void precalculate(){
    register unsigned n = 10001;
    arr[0] = 0;
    arr[1] = 0;
    arr[2] = 1;
    arr[3] = 2;
    arr[4] = 2;
    arr[5] = 3;
    arr[6] = 3;
    arr[7] = 3;
    arr[8] = 3;

    /**
     * If you look carefully there is a nice pattern here
     * This code can be shorted with a nested loop
     * 2^N <= Loop <= 2^(N-1) + 1
     */
    for(; n >= 8193; --n)
        arr[n] = 14;
    for(; n >= 4097; --n)
        arr[n] = 13;
    for(; n >= 2049; --n)
        arr[n] = 12;
    for(; n >= 1025; --n)
        arr[n] = 11;
    for(; n >=  513; --n)
        arr[n] = 10;
    for(; n >=  257; --n)
        arr[n] = 9;
    for(; n >=  129; --n)
        arr[n] = 8;
    for(; n >=   65; --n)
        arr[n] = 7;
    for(; n >=   33; --n)
        arr[n] = 6;
    for(; n >=   17; --n)
        arr[n] = 5;
    for(; n >=    9; --n)
        arr[n] = 4;
}

int main(){
    precalculate();
    int n;
    register unsigned c = 1;

    while (scanf("%d", &n) && n > -1){
        printf("Case %u: %u\n", c, arr[n]);
        ++c;
    }

    return 0;
}

UVA Problem 1124 – Celebrity jeopardy Solution

UVA Problem 1124 – Celebrity jeopardy Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

Another super easy problem. The instructions on the top are just to distract us or make us thinks it is a tough problem.

I have made a video tutorial ( English ) for this code. I will also upload a Bengali version soon.

Problem statement requires us to find the equation whose transformation into the desired answer requires the least effort. So the least effort for a celebrity is print the equation that was given.

So for this problem we just input a string and print it. That all.

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.


Input:

Y = 3 
X=9

Output:

Y = 3 
X=9

Code:

/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 */
#include<stdio.h>
int main(){
    char s[128];
    while(gets(s)){
        puts(s);
    }
    return 0;
}

UVA Problem 113 – Power of Cryptography Solution

UVA Problem 113 – Power of Cryptography Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

This is a very easy mathematical problem. The formula is given. kn = p. We can solve this with taking ln on each sides and calculating for k using k = e(ln(p)/n). But the formula below is easier.

Simplify kn = p,

kn = p
(kn)(1/n) = p(1/n)
k = p(1/n)       /* k = pow(p,1/n) */

or, just using power formula below. We are asked to find the value of k given n and p. Also The formula for calculating k is given n√p.

For this problem taking n and p is double is enough. Also for this problem there is no digits after decimal. For c double printf to discard values after decimal point we can use,

%.0lf

Also we should keep scanning until EOF is encountered.

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.


Input:

2
16
3
27
7
4357186184021382204544

Output:

4
3
1234

Code:

/*
 * @author Quickgrid ( Asif Ahmed )
 * @link   https://quickgrid.wordpress.com
 */

#include<stdio.h>
#include<math.h>

int main(){
    double n,p;
    while (scanf("%lf%lf", &n, &p) == 2)
        printf("%.0lf\n", pow(p, 1 / n));
    return 0;
}

UVA Problem 12700 – Banglawash Solution

UVA Problem 12700 – Banglawash Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

This article gives some good code optimization tips.

First of all don’t get confused by the poorly formatted output ( But i have not tried if it works without newline ) . There is a newline after each output.

Now ( except for the last paragraph ) anything above the input label is not necessary to solve this problem.

The first input is number of test cases. Next input is the size of the string ( meaning how many matches have been played ). Next input is a string. There only 4 types of letters in the string. Also we know the number of test cases ( T < 101 ) and array size ( N < 11 ).

These letters will be either `B’ or `W’ or `T’ or `A’. Here, `B’ means the match was won by Bangladesh, `W’ means the match was won by WWW, `T’ means the match was a tie and `A’ means the match was abandoned.

In order to solve this we can just count the number of characters.

Below is just the steps to solve not the expected output:

If all character are A then match is Abandoned.

if Bangladesh won more then print Bangladesh and Bangladesh win count then WWW win count.

if WWW won more then print WWW and WWW win count then Bangladesh win count.

If win count for both team are same or all matches are tied then print Draw then print win count for any team then print tie count.

If Bangladesh won all matches except for the Abandoned matches then print Banglawash.

If WWW won all matches except for the Abandoned matches then print Whitewash.

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.


Input:

6
3
BBB
3
WWW
3
BWB
4
BWWT
3
BTW
2
AA

Output:

Case 1: BANGLAWASH
Case 2: WHITEWASH
Case 3: BANGLADESH 2 - 1
Case 4: WWW 2 - 1
Case 5: DRAW 1 1
Case 6: ABANDONED

Code:

/*
 * @author  Quickgrid ( Asif Ahmed )
 * @link    https://quickgrid.wordpress.com
 */

#include<stdio.h>

static char s[16];

int main(){
    register unsigned n, c = 1;
    scanf("%u", &n);

        while (n--){
            register unsigned m, i;
            unsigned A = 0, B = 0, T = 0, W = 0;
            scanf("%u%s", &m, &s);

            for (i = 0; i < m; ++i){
                switch(s[i]){
                case 65:    /* or use 'A' instead of 65 (ASCII) but using 65 is faster */
                    ++A;
                    break;
                case 66:    /* or use 'B' instead of 66 (ASCII) but using 66 is faster */
                    ++B;
                    break;
                case 84:    /* or use 'T' instead of 84 (ASCII) but using 84 is faster */
                    ++T;
                    break;
                case 87:    /* or use 'W' instead of 87 (ASCII) but using 87 is faster */
                    ++W;
                    break;
            }
        }

        if (A == m)                             /* All matches are abandoned */
            printf("Case %u: ABANDONED\n",c);
        else if (B + A == m)                    /* If Bangladesh won all the matches, except for Abandoned ones */
            printf("Case %u: BANGLAWASH\n",c);
        else if (W + A == m)                    /* If WWW won all the matches, except for Abandoned ones */
            printf("Case %u: WHITEWASH\n",c);
        else if (B > W)                         /* Bangladesh won more matches then WWW */
            printf("Case %u: BANGLADESH %u - %u\n",c,B,W);
        else if (B < W)                         /* WWW won more matches then Bangladesh */
            printf("Case %u: WWW %u - %u\n",c,W,B);
        else                                   /* Win count for both team is tied */
            printf("Case %u: DRAW %u %u\n",c,B,T);

        ++c;
    }
    return 0;
}

UVA Problem 12461 ( Airplane ) Solution

UVA Problem 12461 ( Airplane ) Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

Run time 9 ms ( 0.009 s ), Rank 226.

Just look at the output. That’s all if you just want the answer. No need to see the code 🙂

Also take a look at probability.

If you want to understand the problem:

Our input is n (  The number of passenger on-board). It says that n people board an airplane with n seats. Meaning if 6 people board the plane there are 6 seats.

So, Now it says First passenger lost ticket and he sits in a random seat. Also Each subsequent passenger sits in his own seat if it’s available or takes a random unoccupied seat if it’s not.

Now it asks for the probability that nth passengers seat is occupied. The nth passenger is obviously the last passenger. So if 5 people board a plane, there are 5 seats then the nth passenger is 5. There is only 1 passenger is not seated at a time.

Point to note is all other passengers except nth passenger are seated after taking seats of their choice. So when the last passenger tries to seat there are two things that he may find. Either, his seat is occupied or, it is empty. So there is total two outcomes. 

So the probability of finding seat occupied is = desired outcome count / total outcomes. Only one of two things happen at a time. Another passenger can seat in nth passenger seat or not seat there. There is 50% chance of finding the seat occupied.

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.


Input:

2
3
4
9
0

Output:

1/2
1/2
1/2
1/2

Code:

/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 */
#include<stdio.h>
int main(){
	unsigned int n;
	while(scanf("%u", &n) == 1 && n!=0){
        printf("1/2\n");
	}
	return 0;
}

UVA Problem 444 ( Encoder and Decoder ) Solution

UVA Problem 444 ( Encoder and Decoder ) Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

Seems like a medium level problem ( at least for me ).

Here the input is either mix of characters (A-Z) or (a-z) or,

!  ,  .  :  ;  ?

These characters. Or, Integers only.

If the input is a decoded string such as, abc?.  We need to encode it. The encode method is to reverse the Ascii values. For example, If decoded string was input

Input: abc?d
Just Convert to Ascii: 97989963100
Reverse the Converted Ascii: 00136998979 /*This is our output*/

If the input was an encoded integer string, to decode it follow the procedure below,

Input: 00136998979
Reverse the input: 97989963100
Convert to Character: abc?d /*This is our output*/

Another important thing any characters from ‘d’ to ‘z’ has ASCII value of 100 or more. So In that case we need to reverse 3 characters to find its corresponding ASCII character ( in other words to decode properly ).

Also after encoding a string if there are 0’s in the front we should print them like in the example above.

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.


Code Explanation:

The code below makes cin, cout as fast as or maybe even faster then scanf, printf. Reference code forces blog.

std::ios_base::sync_with_stdio(false);
cin.tie(0);

It is also advised to use new line character instead of endl.


Input:

abc
798999
Have a Nice Day !

Output:

998979
cba
332312179862310199501872379231018117927

Code:

/*
 * @author Quickgrid ( Asif Ahmed )
 * @link   https://quickgrid.wordpress.com
 */

#include<iostream>
#include<stdio.h>

using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int i, j, a;
    string s;

    while (getline(cin, s)){
        /*
         * Find out the length of string
         */
        for (i = 0; s[i]; ++i);

        /*
         * Start processing the string/input backward
        */
        for (j = i - 1; j >= 0;){
            /*
             * Check if the input is encoded integer. Also checking with s[0] or the first character is enough since it can either be numbers or letters
            */
            if (s[0] >= '0' && s[0] <= '9'){
                /*
                 * Check if the character is of length 2 because No input below 100 Ascii value of (d) contains integer length 2
                 */
                if (s[j] != '1'){
                    /*
                     * Make the last digits 10s and 1st digit in 1s place. Ex: 79 = 7+(9*10) = 97
                     */
                    a = (s[j-1] - '0') + (s[j] - '0') * 10;
                    /*
                     * Decrease the loop counter by two since we have processed two digits. Meaning the integer was 99 or below
                     */
                    j -= 2;
                }else{
                    /*
                     * Make the last digits 100s, middle 10s and 1st digit in 1s place. Ex: 101 = 1+(0*10)+(1*100) = 101
                     */
                    a = s[j-2] - '0' + (s[j-1] - '0') * 10 + (s[j] - '0') * 100;
                    /*
                     * Decrease the loop counter by Three since we have processed Three digits. Meaning the integer was 100 or above
                     */
                    j-=3;
                }
                /*
                 * Since the input was a number and we decoded it to a character now just print that character
                 */
                cout << (char)a;
            }
            /*
             * If not encoded integer it is obviously a string
             */
            else{
                /*
                 * Store the ASCII integer value of the character
                 */
                a = s[j];

                while (a){
                    /*
                     * Print the last integer digit of (a)
                     */
                    cout << a % 10;
                    /*
                     * Discard the last digit
                     */
                    a /= 10;
                }
                /*
                 * Decrease the loop counter since One character is processed
                 */
                --j;
            }
        }
        cout << endl;
    }
    return 0;
}