| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11126 | Binary representation |
|
| 11127 | Binary representation&sum |
|
| 11134 | Swap number |
|
| 11135 | I2P(I)2016_Yang_Hw3-4 |
|
| 11136 | I2P(I)2016_Yang_Hw3-5 |
|
Description
Consider the following program:
#include
#include
int main(void)
{
char str[65] = {0};
int bit[65];
int i, n;
unsigned long long x;
scanf("%64s", str); /* read the input bit string */
n = strlen(str); /* get the length of the string */
i = 0;
x = 0;
while ...
...
If the input string is 1101, the program will print 13. If the input is 100000000000000000000000000000000000001, the output should be 274877906945. Now, your task is to modify the program so that it can do the opposite. That is, given a non-negative decimal number, print its binary representation. For example, if the input is 14, the output should be 1110.
Input
An integer that can be read by scanf using the format "%llu" and can be stored in a variable of type unsigned long long
Output
The binary representation of the input number. Note that you need to print a '\n' at the end of the bit string.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Problem Description
Given a positive integer N, transform it to its unsigned binary representation (e.g. 10 => 1010). Your program needs to output the binary representation of N+1 and the number of carries during the addition in binary representation.
For example, if the input is 11 (in decimal), your program needs to output 1100, because it is the binary representation of 11+1=12. Also your program needs to output 2, because during the binary addition of 11+1, there are two carries generated.
1011 (11 in binary)
+ 0001 (1 in binary)
---------------------------------
1100 (12 in binary)
Input
The input consist of an integer N (0 <= N <= 1024)
Output
The binary representation of N+1 and the number of carries during the binary addition of N+1. Those two numbers are separated by a space. Note that you do not need to print ‘\n’ at the end of the output.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
In a shell game, balls with different numbers are hidden beneath identical containers, which are placed in a line. After the containers are shuffled, players need to guess the order of balls.
In this problem, there are five balls, numbered 0-4, and placed under five identical containers. The containers are placed in a straight line, and the initial positions of balls are from 0-4. The problem will then give k swaps of containers. Each swap is represented by a pair of integers, (a,b), meaning the container in position a is swapped with the container in position b. Your program needs to print the final ordering of balls.
For example, k=3, and three swaps are
0 4
2 3
1 2
Initially, the order of balls is: 0 1 2 3 4. After the swap (0,4), the order of balls becomes: 4 1 2 3 0; After the swap (2,3), the order of balls becomes: 4 1 3 2 0; and after the swap (1,2), the order of balls becomes:
4 3 1 2 0. So your program needs to print
4 3 1 2 0
hint:
To swap two numbers, a and b, you can use following code:
int temp;
temp = a;
a = b;
b = temp;
Input
The first line of the input is an integer k (0 < k < 25), which specifies the number of swaps. In the next k lines, each line contains a pair of integers, specifying the two positions to be swapped.
Output
The order of balls after swaps. Note that you do not need to print ‘\n’ at the end of the output.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
As described in Wikipedia, Pascal's triangle is a triangular array of the binomial coefficients. The element k on row n of Pascal’s triangle can be calculated by the following relation:
C(n, 1) = 1, for n = 1, 2, …
C(n, n) = 1, for n = 1, 2, …
C(n, k) = C(n-1, k-1) + C(n-1, k), for k = 2, 3, … and for n = 2, 3, …
Given a nonnegative integer M, display the Pascal’s triangle from row 1 to row M.
Use '%10d' to print each element. Print a newline ' ' at the end of each row.
(Note that the sample output print only 1 blank within each element, which is wrong. You should use '%10d'.)
Input
A positive integer M (1<=M<=30)
Output
Print the Pascal triangle from level 1 to level M.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
The input contains nine different numbers, which are 1 to 9 in some random order, for example, 4 1 5 9 8 7 3 6 2. Now, start from the first place, we get the number 4. It indicates that the next number we need to pick is at the 4th place, which is 9. Likewise, we then go the 9th place and get the number 2. We repeat this process until we go to a place that has already been visited. In this example, we will stop at the second place and get the number 1, because we have found a circle. Therefore, along the way we pick four numbers, 4 9 2 1, and the output is the sum of these numbers, which is 16.
Note that we always start from the first place.
Other examples: Consider the input 2 3 4 5 6 7 8 9 1, the output should be 45 since we will visit the numbers one by one and go back to the beginning. Consider the input 1 9 8 7 6 5 4 3 2, the output should be 1 since we stop immediately and cannot go anywhere. Consider the input 9 2 3 4 5 6 7 8 1, the output should be 10.
Input
A random sequence of the nine different numbers of 1 to 9.
Output
The sum of the numbers being visited. Remember to print a '\n' at the end of the sum.