Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use java discrete structure book ( DStructureBook ) In the Discrete Structures classroom Mali's teacher told the students to open the book to page p

Use java
discrete structure book (DStructureBook)
In the Discrete Structures classroom Mali's teacher told the students to open the book to page p. At that time, Mali might
The book is still open at page k, so Mali can choose to open the book by starting at the first page, the last page, or starting at the next k page, but
You can open one page at a time until you reach the desired page. But if Mali opens the book and is stuck at page k, if you have to flip to start at page 1 or the last page, count the
Flip this time to add 1 more time as well.
In the books that Mali's teachers let students use, page 1 is always on the right side. And when Mali turns over page 1, you will see pages 2 and 3 as shown in the picture.
The bottom and each sheet of a book are always printed on two sides, except for the last page, where the other side may be blank.
If a book has N pages and Mali's teacher tells her to open page p, and the page Mali left open is page k, find the number of times in
Minimum number of page flips up to page p. Mali can choose to start at the first page, last page, or page k to get the number of flips.
Minimal page turns If Mali cannot open to page p that the teacher specified. Find the value for the least number of flips and enter the result as the value.
negative instead
input data
The input has 3 lines.
1. The first line consists of one positive integer representing the total number of pages of the book pages, given that
1<=<=10,000, where N is always the correct value.
2. The second line contains one positive integer representing the page number p that you want to open.
3. The third line contains one positive integer representing the number k that is left open. If k =0, it means
The book is closed.
Export data
The output data is 1 line. Print the result that is the minimum number of page turns.
Example 1
data input data output
6
2
4
Export data
1
If you open from the first page, you must flip 2 times. But if you open from the last page, you must flip 3 times. And if you open from page 4, you will flip only 1.
times, so the answer is 1
Example 2
If you open it from the first page, you have to flip it 3 times. But if you open it from the last page, you have to flip it 0 times. And if you open it from page 4, you have to flip it 0 times.
Likewise, so the answer is 0.
Example 3
Because the book is closed If opening from the first page and opening from page 0 to page 8, it will be the same, meaning you will have to flip 5 times. But if opening from page
In the end, it had to be flipped 2 times, but because page 8 couldn't be opened, a smaller number of times was chosen and a minus sign was put in front of the answer.
is -2
Example 4
Because the book is stuck on page 8, which is the same page as the page you want to open. So there is no need to turn the page. But because page 8 doesn't
can be opened because it exceeds the bounds of the book page So put a minus sign. The answer page is -0.
input data
5
4
5
0
input data
6
8
0
Export data
-2
input data
6
8
8
Export data
-0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Discuss communication challenges in a global environment.

Answered: 1 week ago