📜  B的最大偶数和奇数N位数字

📅  最后修改于: 2021-05-04 09:43:51             🧑  作者: Mango

给定整数N和基数B ,任务是找到十进制形式的基数B的最大偶数和奇数N位数字。
例子:

方法:
为了获得最大的十进制形式的基数B的偶数和奇数N位,由下式给出:

  1. 如果基数B是偶数,则:
    • 最大的N位偶数是(B N – 2)
    • 最大的N位奇数是(B N – 1)
  2. 如果基数B为奇数,则:
    • 最大N位偶数为(B N – 1)
    • 最大N位奇数为(B N – 2)

下面是上述方法的实现:

C++
// C++ implementation of the
// above approach
#include 
using namespace std;
 
// Function to print the largest
// N-digit even and odd numbers
// of base B
void findNumbers(int n, int b)
{
    // Intialise the Number
    int even = 0, odd = 0;
 
    // If Base B is even, then
    // B^n will give largest
    // Even number of N+1 digit
    if (b % 2 == 0) {
 
        // To get even number of
        // N digit subtract 2 from
        // B^n
        even = pow(b, n) - 2;
 
        // To get odd number of
        // N digit subtract 1 from
        // B^n
        odd = pow(b, n) - 1;
    }
 
    // If Base B is odd, then
    // B^n will give largest
    // Odd number of N+1 digit
    else {
 
        // To get even number of
        // N digit subtract 1 from
        // B^n
        even = pow(b, n) - 1;
 
        // To get odd number of
        // N digit subtract 2 from
        // B^n
        odd = pow(b, n) - 2;
    }
    cout << "Even Number = " << even << '\n';
    cout << "Odd Number = " << odd;
}
 
// Driver's Code
int main()
{
    int N = 2, B = 5;
 
    // Function to find the
    // numbers
    findNumbers(N, B);
    return 0;
}


Java
// Java implementation of the
// above approach
import java.util.*;
 
class GFG{
  
// Function to print the largest
// N-digit even and odd numbers
// of base B
static void findNumbers(int n, int b)
{
    // Intialise the Number
    double even = 0, odd = 0;
  
    // If Base B is even, then
    // B^n will give largest
    // Even number of N+1 digit
    if (b % 2 == 0) {
  
        // To get even number of
        // N digit subtract 2 from
        // B^n
        even = Math.pow(b, n) - 2;
  
        // To get odd number of
        // N digit subtract 1 from
        // B^n
        odd = Math.pow(b, n) - 1;
    }
  
    // If Base B is odd, then
    // B^n will give largest
    // Odd number of N+1 digit
    else {
  
        // To get even number of
        // N digit subtract 1 from
        // B^n
        even = Math.pow(b, n) - 1;
  
        // To get odd number of
        // N digit subtract 2 from
        // B^n
        odd = Math.pow(b, n) - 2;
    }
    System.out.println("Even Number = " +  (int)even );
    System.out.print("Odd Number = " +  (int)odd);
}
  
// Driver's Code
public static void main(String[] args)
{
    int N = 2, B = 5;
  
    // Function to find the
    // numbers
    findNumbers(N, B);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python implementation of the
# above approach
 
# Function to print the largest
# N-digit even and odd numbers
# of base B
def findNumbers(n, b):
    # Intialise the Number
    even = 0;
    odd = 0;
 
    # If Base B is even, then
    # B^n will give largest
    # Even number of N+1 digit
    if (b % 2 == 0):
 
        # To get even number of
        # N digit subtract 2 from
        # B^n
        even = pow(b, n) - 2;
 
        # To get odd number of
        # N digit subtract 1 from
        # B^n
        odd = pow(b, n) - 1;
     
 
    # If Base B is odd, then
    # B^n will give largest
    # Odd number of N+1 digit
    else:
 
        # To get even number of
        # N digit subtract 1 from
        # B^n
        even = pow(b, n) - 1;
 
        # To get odd number of
        # N digit subtract 2 from
        # B^n
        odd = pow(b, n) - 2;
     
    print("Even Number = ",int(even));
    print("Odd Number = ", int(odd));
 
# Driver's Code
if __name__ == '__main__':
    N = 2;
    B = 5;
 
    # Function to find the
    # numbers
    findNumbers(N, B);
     
# This code is contributed by 29AjayKumar


C#
// C# implementation of the
// above approach
using System;
 
class GFG{
   
// Function to print the largest
// N-digit even and odd numbers
// of base B
static void findNumbers(int n, int b)
{
    // Intialise the Number
    double even = 0, odd = 0;
   
    // If Base B is even, then
    // B^n will give largest
    // Even number of N+1 digit
    if (b % 2 == 0) {
   
        // To get even number of
        // N digit subtract 2 from
        // B^n
        even = Math.Pow(b, n) - 2;
   
        // To get odd number of
        // N digit subtract 1 from
        // B^n
        odd = Math.Pow(b, n) - 1;
    }
   
    // If Base B is odd, then
    // B^n will give largest
    // Odd number of N+1 digit
    else {
   
        // To get even number of
        // N digit subtract 1 from
        // B^n
        even = Math.Pow(b, n) - 1;
   
        // To get odd number of
        // N digit subtract 2 from
        // B^n
        odd = Math.Pow(b, n) - 2;
    }
    Console.WriteLine("Even Number = " +  (int)even );
    Console.Write("Odd Number = " +  (int)odd);
}
   
// Driver's Code
public static void Main(String[] args)
{
    int N = 2, B = 5;
   
    // Function to find the
    // numbers
    findNumbers(N, B);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
Even Number = 24
Odd Number = 23