I don't want to be embarassed #2

Not again! This time, the Mathematician was tasked to perform in front of children. The mathematician had to think of a new trick but this time, his trick requires programming rather than mathematical knowledge.

This is the classic trick of cups and a ball. The cups are placed such that Cup 0 is placed at Position 0, Cup 1 placed at Position one...Cup n n is placed at Position n n . You are given that there are a total of 8 cups and 8 positions. The Mathematician wanted to confuse the children by changing the positions of the cups 41 times.

In the attached link, there are 41 lines of 2 integers(space separated). The first integer, x x represents Postion x x and the second integer y y represents Position y y . This line means that the cup at Position x x changes positions with the cup at Position y y . Therefore Cup originally at Position x x is now at Position y y and vice versa.

Given that the ball was originally under Cup 0 (i.e. Position 0). What position would it be in after the 41 swaps.

Example If we have 4 cups, Cup 0, Cup 1, Cup 2, Cup 3, and we are given 3 swaps:

0 2

1 3

2 3

The order now of the four cups are: 2 3 1 0

After swap 1: 2 1 0 3

After swap 2: 2 3 0 1

After swap 3: 2 3 1 0

Remember: The position is of the first cup is 0!

Attached link : Click here

0 4 5 7

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Daniel Lim
Aug 4, 2014

Here's my code in

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

int main(){

    freopen("problem.txt", "r", stdin);
    freopen("answer.txt", "w", stdout);

    vector <int> cups(41);
    for (int i = 0; i < 41; i++){
        cups[i] = i;
    }

    for (int i = 0; i < 41; i++){
        int x, y; cin >> x >> y;

        swap(cups[x], cups[y]);
    }

    for (int i = 0; i < 41; i++){
        if (cups[i] == 0){
            cout << i;
            break;
        }
    }
    return 0;
}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...