📜  转到所需页面的最小页面翻转数

📅  最后修改于: 2021-04-27 20:16:27             🧑  作者: Mango

给定一本书N页,任务是计算获得给定所需页面K的最小页数。我们可以从书的正面(即从第1页开始)或书的背面(即,第N页)开始翻页。除了第一页(只有背面)和最后一页(只有背面)(取决于书的页数)外,每页都有正面和背面两面。
例子 :

Input : N = 6 and K = 2.
Output : 1.
From front, (1) -> (2, 3), page turned = 1.
From back, (6) -> (4, 5) -> (2,3), page turned = 2.
So, Minimum number of page turned = 1.

Input : N = 5 and K = 4.
Output : 1.
From front, (1) -> (2, 3) -> (4,5), page turned = 2.
From back, (4, 5) page turned = 1. From back, it is 2nd page, since 4 is on other side of page 5 and page 5 is the first one from back
So, Minimum number of page turned = 1.

这个想法是要计算所需书页从书的正面和背面的距离,这是必需的答案。
现在,考虑到页面0,它位于第一页的前面。如果N是偶数,则考虑存在最后一页的N + 1页,因此总页数为N + 1。
要计算距离,
1.如果K为偶数,则前距=(K – 0)/ 2,后距=(N – 1 – K)/ 2。
2.如果K为奇数,则前距=(K – 1)/ 2,后距=(N – K)/ 2。

C++
// C++ program to find minimum number of page
// turns to reach a page
#include
using namespace std;
 
int minTurn(int n, int k)
{
    // Considering back of last page.
    if (n%2 == 0)
        n++;
 
    // Calculating Distance from front and
    // back of the book and return the min
    return min((k + 1)/2, (n - k + 1)/2);
}
 
// Driven Program
int main()
{
    int n = 6, k = 2;
    cout << minTurn(n,k) << endl;
    return 0;
}
 
// This code is modified by naveenkonda


Java
// Java program to find minimum
// number of page turns to
// reach a page
import java.io.*;
 
public class GFG
{
 
// Function to calculate
// minimum number of page
// turns required
static int minTurn(int n, int k)
{
     
    // Considering back of last page.
    if (n % 2 == 0)
        n++;
 
    // Calculating Distance from front and
    // back of the book and return the min
    Math.min((k + 1) / 2, (n - k + 1) / 2);
}
 
    // Driver Code
    static public void main (String[] args)
    {
        int n = 6, k = 2;
        System.out.println(minTurn(n, k));
    }
}
 
// This code is contributed by vt_m.


Python3
# Python3 program to find minimum number
# of page turns to reach a page
def minTurn(n, k):
     
    # Considering back of last page.
    if (n % 2 == 0):
        n += 1
 
    // Calculating Distance from front and
    // back of the book and return the min
    return min((k + 1) / 2, (n - k + 1) / 2)
 
# Driver Code
if __name__ == '__main__':
    n = 6
    k = 2
    print(int(minTurn(n, k)))
     
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find minimum
// number of page turns to
// reach a page
using System;
 
public class GFG
{
 
// Function to calculate
// minimum number of page
// turns required
static int minTurn(int n, int k)
{
     
    // Considering back of last page.
    if (n % 2 == 0)
        n++;                       
 
    // Calculating Distance from front and
    // back of the book and return the min
    return Math.Min((k + 1) / 2,
                    (n - k + 1) / 2);
}
 
    // Driver Code
    static public void Main (String[] args)
    {
        int n = 6, k = 2;
        Console.WriteLine(minTurn(n, k));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

1