| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 10039 | I2P homework1 |
|
| 10066 | I2P homework2a |
|
| 10067 | I2P homework2b |
|
| 10074 | I2P homework3a |
|
| 10075 | I2P homework3b |
|
| 10098 | I2P homework4a |
|
| 10099 | I2P homework4b |
|
| 10161 | I2P homework5 |
|
| 10218 | I2P homework6 |
|
| 10238 | I2P homework7 |
|
| 10265 | I2P homework8 |
|
| 10295 | I2P homework9 |
|
| 10313 | I2P homework10 |
|
| 10359 | I2P homework11 |
|
Description
The total number of chickens and rabbits is N, and overall they have M feet. How many chickens and how many rabbits are there?
Hint:
You might need to use the arithmetic operators + - * / for addition, subtraction, multiplication, and division.
Input
Two integers M and N, where 0<=N,M<=1000.
Output
Number of chickens and number of rabbits
Sample Input Download
Sample Output Download
Tags
Discuss
Description
The input is a three-digit integer N that consists of digits 1-9 except 0. For example, 489 is such a number. The task is to reverse the order of the digits of N to get a new three-digit number M, and compute the average of the N and M. For example, if N is 489, then M is 984, and the answer should be 736.5.
Input
A three-digit integer consisting of 1-9 except 0
Output
The average of the input number and its reversal
The answer should be expressed as a floating point number with precision to the first decimal place. For example, 333.0 or 736.5
Note that you do not need to print ‘\n’ at the end of the output.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Suppose that we have an encoding scheme defined by the following mapping:
1->'A', 2->'B', 3->'C', ..., 9->'I'
Given a three-digit number N as the input, use the above mapping to encode N.
Input
A three-digit integer N
Output
The encoding result
Note that you do not need to print ‘\n’ at the end of the output.
Sample Input Download
Sample Output Download
Tags
Discuss
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 (i 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
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.
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.
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
Given a vector of dimension N, use it to create an N-by-N circulant matrix. An example of circulant matrix:
1 5 4 3 2
2 1 5 4 3
3 2 1 5 4
4 3 2 1 5
5 4 3 2 1
Input
The first line contains a positive integer N indicating the size of the matrix. (0
Output
The N-by-N circulant matrix. Print the elements of the matrix using the format "%2d". Each row is ended with a newline character ' '.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Complete the following program of car and jewels.
map[][] is a two-dimensional array representing the content of the map.
map_reset() is to clear the map and reset the boundary.
map_show() is to display the map.
blocks[][] is also a two-dimensional array that indicating the locations of brick walls.
We may use blocks_read() to read the locations of brick walls from the input, and then use blocks_put_on_map() to place the bricks at the indicated locations in the map.
Note that the car will stop moving if it runs into a wall.
jewels[][] is a two-dimensional array recording the locations of jewels.
We use jewels_read() to read the jewel locations from the input, and then use jewels_put_on_map() to place the jewels at the indicated locations in the map.
Note that if the car’s location overlaps the locations of some jewels, those jewels will be picked up into the car.
The car’s shape is also represented by a two-dimensional array. The two symbols @ @ denoting the head lights, so we can decide which direction the car is heading by the two symbols @ @. The starting position of the car is row 3 column 4 (the center of the car). We use two variables car_row and car_col to store the car’s current position. The variable car_direction denotes the car’s direction. The variable car_earnings denotes the number of picked jewels in the car.
Note that we can use 0, 1, 2, 3 to represent the car’s direction as right, down, left, and up. That is, if the value of car_direction is 2, it means that the car is moving toward the left. The initial value of car_direction is 3 (heading upward).
Our task is to complete the two functions car_rotate90() and car_move()
The function car_rotate90() is to rotate the car (2D array) clockwise by 90 degrees and update the value of car_direction.
The function car_move() is to move the car forward by one step according to the current direction of the car. We need to check if the car is going to hit the wall before we let it move forward. We need to pick up the jewel underneath the car before we move. Once the jewels are picked up, they will not be available on the map anymore.
The input contains two types of commands, corresponding to two actions.
'R' represents rotating clockwise by 90 degrees
'F' represents moving forward by one step.
We need to show the final state of the car after carrying out all of commands. The output should include car_earnings, car_row, car_col, and car_direction. Note that the size of the map is fixed to 12.
The following is an example of a possible game state at some moment.
HHHHHHHHHHHH
H....@[email protected]
H.#..OOO.#.H
H....OOO...H
H..........H
H..........H
H..........H
H..........H
H.......$..H
H.#......#.H
H...$......H
HHHHHHHHHHHH
The symbol 'H' denotes a wall at the map border, the symbol '#' denotes a brick wall, the symbol '$' denotes a jewel, and the car is
@O@
OOO
OOO
In this case car_row is 2, car_col is 6 and car_direction is 3.
Note that when you test your program on your pc, you should press control+Z to send an EOF to the program and thus exit the main loop.
The code template:
#include
#define MAP_SIZE 12
#define CAR_SIZE 3
int map[MAP_SIZE][MAP_SIZE];
void map_reset(void);
void map_show(void);
int blocks[MAP_SIZE][MAP_SIZE];
void blocks_read(void);
void blocks_put_on_map(void);
int jewels[MAP_SIZE][MAP_SIZE];
void jewels_read(void);
void jewels_put_on_map(void);
int car[CAR_SIZE][CAR_SIZE] = {{'@', 'O', '@'}, {'O', 'O', 'O'}, {'O', 'O', 'O'}};
int car_row = 3, car_col = 4;
int car_direction = 3;
int car_earnings;
void car_rotate90(void);
void car_put_on_map(void);
void car_move(void);
int main(void)
{
int ch;
char dirs[] = "RDLU";
jewels_read();
blocks_read();
while ((ch=getchar()) != EOF) {
if (ch=='R') {
car_rotate90();
}
if (ch=='F') {
car_move();
}
map_reset();
jewels_put_on_map();
blocks_put_on_map();
car_put_on_map();
/* You may call map_show() to print the current state.
Be sure to remove it before you submit the code */
/*map_show();*/
}
printf("%d\n", car_earnings);
printf("%d %d\n", car_row, car_col);
printf("%c\n", dirs[car_direction]);
return 0;
}
void blocks_read(void)
{
int n, i;
int row, col;
scanf("%d", &n);
for (i=0; i
Input
The input contains several lines. The first line contains an integer N indicating the number of jewels. The next N lines are the locations of the N jewels. The next line contains an integer M indicating the number of brick walls. The next M lines are the locations of the M brick walls. The last line of the input contains a sequence of commands for the car’s actions.
Note that the jewels may appear underneath the car in the beginning and should be picked up.
Output
The output contains three lines displayed using
printf("%d\n", car_earnings);
printf("%d %d\n", car_row, car_col);
printf("%c\n", dirs[car_direction]);
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Suppose we have a 3x3 map. The map consists of numbers 1, 9 and 0. For example,
1 0 0
0 0 0
9 0 0
Now, start walking from the position of number 1 to 9. Note that we can only move toward four directions (Right, Down, Left, Up) and each position should be visited exact once. We want to know if we can reach the position of number 9 at the end.
As the previous example, number 9 can be reached because
1 4 5
2 3 6
9 8 7
Your program should print YES if number 9 can be reached, or NO otherwise.
hint:
1. You can open a 5x5 map, and place -1 on the boarder of the map to distinguish zeros and those -1.
2. You can try to travel the map random, the method is described as follows:
Start from number 1, choose one of the 4 directions at random. If this move is valid, then move to the new position and mark the position as 2.
Repeat this step (find the position that can be marked as 3, 4, …). If the chosen move is invalid, then pick another direction.
If the step was tried 10 times and failed (you cannot make a move after ten tries), then restore the map and start over again from number 1.
If you start over a million times and still cannot reach number 9 then print NO and exit the program.
The code rolling the dice is:
#include#include int main(void) { int i, x; for (i=0; i<100; i++) { x = rand()%6 + 1; printf("%3d", x); } return 0; }
Input
A 3x3 map
Output
YES / NO
Note that there is a newline character at the end of the string
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Suppose that you have a lot of coins and bills of different values. Given an amount of money, you need to find the best way to get the change. By ‘best’, we mean that we the total number of coins should be as small as possible. For example, assume that we have coins in $1 and $5, and bills in $10. To get the change for $17, we need 2 coins of $1, 1 coin of $5, and 1 bill of $10. Another example: assume that we have coins in $1 and $5, and bills in $10 and $50. To get the change for $57, we need 1 bill of $50, 1 coin of $5, and 2 coins of $1.
The following is an excerpt of incomplete code:
#include <stdio.h>
#define MAXNV 5
int DONE = 0;
int values[MAXNV];
int numbers[MAXNV];
void show(int nv);
void change(int amount, int cur, int nv);
int main(void)
{
int nv, i;
int money;
scanf("%d", &nv);
for (i=0; i<nv; i++) {
scanf("%d", &values[i]);
}
scanf("%d", &money);
change(money, 0, nv);
return 0;
}
void show(int nv)
{
int i;
printf("(%d", numbers[0]);
for (i=1; i<nv; i++) {
printf(",%d", numbers[i]);
}
printf(")\n");
}
void change(int amount, int cur, int nv)
{
/* your code */
}
Input
The input contains three lines. The first line contains an integer N (0 The result can be displayed by calling the function show()Output
Only the best way for change should be displayed.Sample Input Download
Sample Output Download
Tags
Discuss
Description
The input is a sequence of positive fractions e.g. 1/2, 2/3, 3/4, and the output is their sum expressed also as a simple fraction, which is 23/12 in this case. Note that the answer should be expressed in simplest terms, for example, the output of 1/3 plus 1/6 should be displayed as 1/2 not 3/6. If the answer is an integer then the denominator should be 1, for example, the output of 1/2 plus 1/2 should be 1/1.
Note that the input may not be simple fractions.
hints:
1. Calculate the least common multiple of the denominators.
2. You may implement the following two functions for computing the greatest common divisor and the least common multiple:
int gcd(int a, int b);
int lcm(int a, int b);
Input
The first line is an integer N (0
Output
The output is a pair of numerator and denominator, ended with a newline character.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
The game of 2048 is very simple. Given a 4x4 map containing tiles and spaces. The values are displayed on the tiles, and the spaces are displayed as 0. The values of tiles are always the power of 2. For example,
2 2 64 128
4 0 4 16
4 4 4 4
0 0 2 0
is a valid map.
We are able to slide the tiles in 4 directions(right, down, left and up). The rules of sliding is defined as follows:
1. All tiles slide past spaces. If you move the puzzle to the left, all spaces will end up on the right.
2. After sliding to collapse spaces, any tiles with the same value combine and the remainder of the row or column slides to fill in the hole.
3. One tile can only combine(be combined) once.
4. The combination always starts from the side of the direction. For example, after sliding left of 2, 2, 2, 0, it should be 4, 2, 0, 0.
5. Contrary to the famous game of 2048, there are no new tiles generated.
The direction is represented by a digit d(0 = right, 1 = down, 2 = left, 3 = up). So for instance if we have the map above and d = 2, after we slide the tiles, the map becomes
4 64 128 0
8 16 0 0
8 8 0 0
2 0 0 0
Your job is to slide the tiles correctly given a map and a sequence of directions.
Hint:
#include <stdio.h>
#define WIDTH 4
#define HEIGHT 4
int map[HEIGHT][WIDTH];
void collapse(int dir){
/* your code here */
}
void slide(int dir){
/* your code here */
}
void show(){
int i, j;
for(i = 0; i < HEIGHT; i++){
for(j = 0; j < WIDTH; j++){
printf(" %4d", map[i][j]);
}
printf("\n");
}
}
int main()
{
int N, d;
int i, j;
scanf("%d", &N);
for(i = 0; i < HEIGHT; i++){
for(j = 0; j < WIDTH; j++){
scanf("%d", &map[i][j]);
}
}
for(i = 0; i < N; i++){
scanf("%d", &d);
slide(d);
}
show();
return 0;
}
Input
N(the count of slides)
The map
The sliding sequence
Output
The final map
Note that the value of tiles will not exceed 8192.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Suppose we have 100 variables and a series of equivalence relations. For instance, 2 3 represents that variable 2 and 3 are equivalent.
If variable 2 is set to a value, then variable 3 is set to that value. Similarly, variable 2 is set when variable 3 is set.
After a series of settings, given a sequences of queries, your job is to output the values of the variables.
Note that if a variable be queried is not set, then output the value as 0.00.
Input
M(the count of equivalence relations)
Relation 1
Relation 2
…
Relation M
N(the count of settings)
Setting 1
Setting 2
…
Setting N
P(the count of queries)
Query 1
Query 2
…
Query P
Output
The content of queried variables. Each answer is printed using ‘%.2f\n ’.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Given a list of English names, use ‘qsort’ to sort the names in lexicographic order.
Please see our course webpage for the information about using ‘qsort’, or see the link
http://www.cplusplus.com/reference/cstdlib/qsort/
The detailed rules of sorting are:
1. Sort the strings in alphabetical order. E.g. “abc” is prior to “abd”.
2. The uppercase alphabet has top priority. E.g. “AA” is prior to “e”, or “Ef” is prior to “e”.
3. The string with smaller length is prior to larger length. E.g. “abc” is prior to “abcd”.
Input
Number of names
Name 1
Name 2
Name 3
…
Output
Print the sorted names line by line. Each line contains one name and ends with a newline character.