Sale!

Algorithmic Problem Solving Problem Set 2 solution

Original price was: $35.00.Current price is: $30.00. $25.50

Description

5/5 - (2 votes)

All computer memory is organized into 8-bit bytes. For integer values that require a type with more than 8-bits, such as the
typical 2-byte, 4-byte, and 8-byte integral types available on most modern hardware, there is no universal agreement as to
how to order those bytes and two incompatible storage schemes exist.
The first stores integers as groups of consecutive 8-bit bytes with the least significant byte occupying the lowest memory
address within the group and the most significant byte occupying the highest memory address.
The second is just the reverse; the least significant byte is stored in the highest memory address within the group, and the
most significant byte is stored in the lowest memory address.

Those schemes have been dubbed Little Endian and Big Endian, respectively.
We assume that signed integers are stored using “two’s complement” representation.
When binary integer data is shared between a Little Endian and Big Endian machine, a data conversion must be performed
which involves reversing the bytes of the data. Once the bytes have been reversed the integer is then correctly interpreted
by the hardware as the original value from the opposite-endian machine. Your task is to write a program that will read a list
of integers and report the integers that the binary representations of the input integers would represent on an oppositeendian machine.

Input

A number n , -2147483648 <= n <= 2147483647 .
Output
The number n followed by the phrase “converts to” followed by the other endian-value (single space between the three
parts).
Example 1
Input:
123456789
Output:
123456789 converts to 365779719
Example 2
Input:
-123456789

Output:

-123456789 converts to -349002504
Example 3
Input:
1
Output:
1 converts to 16777216
Example 4
Page 2/2
Input:
16777216
Output:
16777216 converts to 1
Page 1/1

Base Conversion

Write a program to convert a whole number specified in any base (2…16) to a whole number in any other base (2…16).
Digits above 9 are represented by single capital letters: 10 by A, 11 by B, 12 by C, 13 by D, 14 by E and 15 by F.
Input
The input consists of three values:
a positive integer indicating the base of the number
a positive integer indicating the base we wish to convert to
the actual number in the firs base that we wish to convert to the second base (this number will have letters
representing any digits higher than 9; it may have invalid digits)

Output

The output should consist of the original number followed by the string “base”, followed by the original base number,
followed by the string “=” followed by the converted number, followed by the string “base” followed by the new base.
If the original number is invalid, output the statement:
originalValue is an illegal base originalBase number
where originalValue is replaced by the value to be converted and originalBase is replaced by the original base
value.
Example 1
Input:
2 10 10101
Output:
10101 base 2 = 21 base 10
Example 2
Input:
5 3 126
Output:
126 is an illegal base 5 number
Example 3
Input:
15 11 A4C
Output:
A4C base 15 = 1821 base 11
Page 1/1

Chess Championship

In the World Chess Championship of 2035, 2^N contestants were supposed to participate. Because of sudden turn of the
hurricane Alice, many flights were canceled last minute and many of the contestants could not arrive on time to participate
in the championship.
With all the matches pre-arranged ahead of time, the organizer decide to keep the schedule as is and allow walkover
matches in case when one of the players is missing.
If both players are available, then there will be a normal match.
If only one player is available, then this is a walkover match and the player automatically advances to the next level.
If no player is available, then there is no match.
In the figure on the right, the players 3 and 4 could not arrive. Their match is canceled. Players 1 and 2, play their regular
match and the winner advances to the next level match. But there is no second player there, so it is a walkover match.
Given the list of players who could not arrive on time, calculate the number of the walkover matches in the whole
championship.

Input

Each test begins with two integers: 1 <= N <= 10 and 0 <= M <= 2^N . 2^N is the number of players scheduled
for the championship. M is the number of players who could not arrive on time due to the canceled flights. The next line
contains M integers, denoting the players who have not arrived. The players are numbered 1 to 2^N .

Output

The number of walkover matches.
Example 1
Input:
2 2
3 4
Output:
1
Example 2
Input:
3 5
1 2 3 4 5
Output:
2
Example 3
Input:
2 1
2
Output:
1
Page 1/1

Negative base

Have you ever heard of base-2 (binary) numbers? Well, of course you did!!! You are a computer scientist. How about base-
(-2) numbers? Yes, you read it right we are talking about a base that is negative!!!
An integer n writte in in base-(-2) is a sequence of bits ( b_i ), written right to left. Each of which is either 0 or 1 (no
negative bits are allowed) and the following must hold:
n = b_0 + b_1 * (-2) + b+2 * (-2)^2 + b_3 * (-2)^3 + …
Every integer in base-10 has a unique representation in base-(-2) and no negative signs are ever needed.
Your task is to write a program that given a number in base-10 finds its base-(-2) representation.

Input

The number in base-10, -1,000,000,000 <= N <= 1,000,000,000.

Output

The corresponding number in base-(-2) with no leading zeros.
Example 1
Input:
1
Output:
1
Example 2
Input:
7
Output:
11011
Example 3
Input:
-2
Output:
10
Example 4
Input:
0
Output:
0
Page 1/1

Ayu’s Wall

Ayu loves cakes and she likes to design things with boxes in
which she buys the cakes. One day Ayu decided to build a wall
with them but she was not good at construction and thus built an
very uneven wall.
She wants to rearrange the boxes one by one so that all columns
have the same height. She is asking you to help her figure out the
smallest number of boxes that need to be moved to accomplish
that.

Input

The first line of input contains an integer N (1 <= N <= 50) representing the number of columns. The next line contains N
numbers, the number of boxes (1 <= #boxes <= 100) in each column.
The total number of boxes is divisible by the number of columns and thus it is always possible to rearrange the boxes such
that all columns have the same height.

Output

You should print the line The minimum number of moves is k. , where k is the minimum number of boxes to be
moved in order for all the columns to have the same height.
Example 1
Input:
6
5 2 4 1 7 5
Output:
The minimum number of moves is 5.