Problems:
CS100 Homework 2 (Spring 2023)
Deadline: 2023-03-08 (Wed) 23:59:59
Late submission will open for 24 hours after the deadline, with 50% point deduction.
If you get full marks in this assignment by no more than 40 submission attempts, you can earn special OJ displays and a "1-case protection" that can be used in further assignments to cancel one testcase failure. See Piazza or OJ dashboard for more information.
Problem 1: Happy Coding
Given integers, output the square of positive numbers in reversed order.
Input
The first line contains a non-negative integer .
Then lines follow, the -th line containing an integer .
Output
Your output should consist of lines, where is the number of positive numbers in . The first lines contain the square of positive numbers in reversed order, one for each line. Then print on the last line.
Sample
Input
1 | 6 |
Output
1 | 144 |
Notes
.
for every
Test cases
Problem 2: Quadratic Equation
Solving a linear equation is too hard, so we will solve a quadratic equation. A quadratic equation is of the form , where are constant real numbers and the unknown .
Given the coefficients and , your task is to solve the quadratic equation .
To make the problem easier, may be zero. To make the problem harder, it is guaranteed that .
Solve the equation and output the solution(s).
- If the equation is a quadratic one, your output should be of one of the following forms (where
???
is replaced with the exact solution):x1 = x2 = ???
x1 = ???, x2 = ???
whereNo solution.
- Otherwise, your output should be of one of the following forms (where
???
is replaced with the exact solution):x = ???
No solution.
x\in\mathbb{R}
, indicating in
All the output numbers should be rounded to three decimal places.
It is guaranteed that .
Samples
Sample 1
Input
1 | 1 2 1 |
Output
1 | x1 = x2 = -1.000 |
Sample 2
Input
1 | 1 0 1 |
Output
1 | No solution. |
Sample 3
Input
1 | 0 0 0 |
Output
1 | x\in\mathbb{R} |
Sample 4
Input
1 | 1 3 2 |
Output
1 | x1 = -2.000, x2 = -1.000 |
Test cases
Problem 3: Hexadecimal Calculator
You are given two integers and in hexadecimal representation. Your task is to perform addition or subtraction in hexadecimal using the vertical method (竖式).
Input
Three lines in total.
The first line contains a character that is either '+'
or '-'
, which indicates the operation you need to perform.
The next two lines are the integers and respectively, in hexadecimal representation.
Output
Show how you do the vertical addition/subtraction. Your output should be of one of the following forms:
1 | xxxxxxxx |
1 | xx |
where the x
's and y
's represent the input numbers and , and p
is either +
or -
depending on the input.
Samples
Sample 1
Input
1 | + |
Output
1 | 1ab |
Sample 2
Input
1 | - |
Output
1 | abc |
Notes
Let be the length of the hexadecimal representation (i.e. the number of digits required) of . Let be the result (either or ). It is guaranteed that
- .
- .
- .
- The letters in the hexadecimal representations are all in lower-case.
Test cases
Problem 4: Bit Operation
Background (You can skip this entire section)
In the year 1979, a young astronomer named Ye Wenjie is working on a project that aims to send radio signals to outer space in search of extraterrestrial intelligence. Ye Wenjie is a survivor of the Chinese Cultural Revolution, a period of political turmoil and violence that killed her father and traumatized her. She has lost faith in humanity and hopes to find a better civilization in the stars.
Ye Wenjie’s project is supported by a secret military base called Red Coast. The base has a powerful antenna that can transmit and receive signals from distant galaxies. Ye Wenjie uses the antenna to send messages to various star systems, hoping to get a response. She also receives messages from other human radio stations around the world.
One day, she receives a message from an unknown source that gives her a coding problem as a way of testing her intelligence and curiosity. The problem is in the discription.
Ye Wenjie solves the problem using her knowledge of binary arithmetic and sends her code back to the source. She thinks that by doing so she can establish contact with an alien civilization and learn more about their culture and technology. She doesn’t know that by doing so she has also invited a hostile invasion from a ruthless enemy…
Description
In this problem, your task is to split a number into two parts by binary digits and reassemble them to form a new number.
You will recieve an unsigned integer whose binary representation consists of digits (You need to find on yourself!), and a non-negative integer . First, you need to split out the rightmost digits to form an integer , and the rest digits form an integer . Then, place on the left of (in binary representation) to obtain a new number.
Formally, write as
where and . Then
The reassembled integer should be
Then, you need to calculate the "lowbit" of . The "lowbit" of an integer is the smallest index such that .
For example, if and , should be and is in binary representation. So the reassembled integer is . The "lowbit" of is since the rightmost bit of is .
Input
One line containing two integers and , separated by a space.
Output
Two integers and on the same line, separated by a space. is the reassembled integer (in decimal representation) and is the lowbit of .
Samples
Sample 1
Input
1 | 114514 4 |
Output
1 | 77306 1 |
Sample 2
Input
1 | 2333333 17 |
Output
1 | 3778904 3 |
Notes
It is guaranteed that .
Test cases
Problem 5: No-Horse Sudoku
"No-horse sudoku", as interesting as its name sounds (especially in Chinese), is a kind of irregular sudoku with new rules. In this problem, your goal is to judge whether a given board of "no-horse sudoku" is valid.
Rules
A regular sudoku has 3 rules:
- Every number should appear exactly once in a column.
- Every number should appear exactly once in a row.
- Every number should appear exactly once in a "palace" (any of the nine equally divided regions).
For "no-horse sudoku", we have an additional rule related to a concept "horse step".
Similar to the rules of horses in chess, we define that two grids share a "horse step" if they lie diagonally in a or rectangle (The pair (E5, C4), for example).
Now here comes the additional rule:
- Any two grids sharing a "horse step" should not be filled in with the same number.
Your Task
Write a program that reads in an entire sudoku board and judges whether the board is valid under the "no-horse sudoku" rules. The whole board is valid if and only if all the numbers in the grids are valid.
It is recommended that you implement such a function (or anything similar to this), which can help you make your code clear and readable.
1 | bool checkOneNumber(int (*board)[9], int row, int col); |
- Brief: Judges whether the number in the grid
board[row][col]
is valid under the "no-horse sudoku" rules. - Parameters:
board
: a array representing the sudoku boardrow
: the row index of the target gridcol
: the column index of the target grid
- Return value:
true
if the number in that grid is valid,false
otherwise.
Input:
lines, each line containing numbers separated by space, representing a row of the sudoku board. The -th line of the input represents the -th row (top to bottom) in the sudoku board.
Output:
1
or 0
. Output 1
if the board is valid, and 0
otherwise.
Important Note
We will check whether you earned your score for this problem in a proper way. Some tricks may get you a high score on OJ, but it will be in vain in the end.
Samples
Some examples of valid boards:
1 | 8 6 4 3 1 5 2 9 7 |
1 | 1 4 3 7 8 9 6 2 5 |
Some examples of boards that are valid under normal rules but invalid with the "no-horse" rule:
1 | 6 8 4 5 1 7 3 9 2 |
1 | 5 3 2 8 7 1 4 6 9 |
Test cases
Reference solutions
Remember, you should always solve all the problems yourself and passed all the testcases first.