给定正整数N和基数B ,任务是找到十进制形式的基数B的最大N位数字。
例子:
Input: N = 2, B = 10
Output: 99
Input: N = 2, B = 5
Output: 24
方法:
由于基数B中有B个数字,因此我们可以使用这些数字创建长度为N的B N个字符串。它们表示范围为0到B N – 1的整数
因此,在小数形式基B的最大的N-数位数目由B n的给定– 1。
下面是上述方法的实现:
C++
// C++ program for the approach
#include
using namespace std;
// Function to print the largest
// N-digit numbers of base b
void findNumbers(int n, int b)
{
// Find the largest N digit
// number in base b using the
// formula B^N - 1
int largest = pow(b, n) - 1;
// Print the largest number
cout << largest << endl;
}
// Driver Code
int main()
{
// Given Number and Base
int N = 2, B = 5;
// Function Call
findNumbers(N, B);
return 0;
}
Java
// Java program for the approach
import java.util.*;
class GFG{
// Function to print the largest
// N-digit numbers of base b
static void findNumbers(int n, int b)
{
// Find the largest N digit
// number in base b using the
// formula B^N - 1
double largest = Math.pow(b, n) - 1;
// Print the largest number
System.out.println(largest);
}
// Driver Code
public static void main(String []args)
{
// Given Number and Base
int N = 2, B = 5;
// Function Call
findNumbers(N, B);
}
}
// This code is contributed by Ritik Bansal
Python3
# Python3 program for the above approach
# Function to print the largest
# N-digit numbers of base b
def findNumbers(n, b):
# Find the largest N digit
# number in base b using the
# formula B^N - 1
largest = pow(b, n) - 1
# Print the largest number
print(largest)
# Driver Code
# Given number and base
N, B = 2, 5
# Function Call
findNumbers(N, B)
# This code is contributed by jrishabh99
C#
// C# program for the approach
using System;
class GFG{
// Function to print the largest
// N-digit numbers of base b
static void findNumbers(int n, int b)
{
// Find the largest N digit
// number in base b using the
// formula B^N - 1
double largest = Math.Pow(b, n) - 1;
// Print the largest number
Console.Write(largest);
}
// Driver Code
public static void Main(String []args)
{
// Given Number and Base
int N = 2, B = 5;
// Function Call
findNumbers(N, B);
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
24
时间复杂度: O(1)
辅助空间: O(1)