📌  相关文章
📜  最小和最大N位数字,以N开头和结尾

📅  最后修改于: 2021-04-24 21:21:52             🧑  作者: Mango

给定整数N ,任务是找到以数字N开头和结尾的最小和最大的N位数。
例子:

方法:
我们知道,最大和最小的N位数字是9999…9 ,其中9重复N次并重复1000…。 0 ,其中0分别重复N-1次。
我们得到的最小和最大的n位数字开始和结束与N,我们需要更换第一和最小的最后一个数字,并用N最大的n个位数
我们必须注意拐角处的情况,即当N = 1时,最大和最小数目均为1
下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find n digit
// largest number starting
// and ending with n
string findNumberL(int n)
{
      
    // Corner Case when n = 1
    if (n == 1)
        return "1";
  
    // Result will store the
    // n - 2*length(n) digit
    // largest number
    string result = "";
  
    // Find the number of
    // digits in number n
    int length = (int)floor(log10(n) + 1);
  
    // Append 9
    for(int i = 1; i <= n - (2 * length); i++)
    {
        result += '9';
    }
  
    // To make it largest n digit
    // number starting and ending
    // with n, we just need to
    // append n at start and end
    result = to_string(n) + result + to_string(n);
  
    // Return the largest number
    return result;
}
  
// Function to find n digit
// smallest number starting
// and ending with n
string findNumberS(int n)
{
  
    // Corner Case when n = 1
    if (n == 1)
        return "1";
  
    // Result will store the
    // n - 2*length(n) digit
    // smallest number
    string result = "";
  
    // Find the number of
    // digits in number n
    int length = (int)floor(log10(n) + 1);
    for (int i = 1; i <= n - (2 * length); i++)
    {
        result += '0';
    }
  
    // To make it smallest n digit
    // number starting and ending
    // with n, we just need to
    // append n at start and end
    result = to_string(n) + result + to_string(n);
  
    // Return the smallest number
    return result;
}
 
// Driver code
int main()
{
   
    // Given number
    int N = 3;
  
    // Function call
    cout << "Smallest Number = " << findNumberS(N) << endl;
    cout << "Largest Number = " << findNumberL(N);
    return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Function to find n digit
    // largest number starting
    // and ending with n
    static String findNumberL(int n)
    {
        // Corner Case when n = 1
        if (n == 1)
            return "1";
 
        // Result will store the
        // n - 2*length(n) digit
        // largest number
        String result = "";
 
        // Find the number of
        // digits in number n
        int length
            = (int)Math.floor(
                Math.log10(n) + 1);
 
        // Append 9
        for (int i = 1;
             i <= n - (2 * length); i++) {
            result += '9';
        }
 
        // To make it largest n digit
        // number starting and ending
        // with n, we just need to
        // append n at start and end
        result = Integer.toString(n)
                 + result
                 + Integer.toString(n);
 
        // Return the largest number
        return result;
    }
 
    // Function to find n digit
    // smallest number starting
    // and ending with n
    static String findNumberS(int n)
    {
 
        // Corner Case when n = 1
        if (n == 1)
            return "1";
 
        // Result will store the
        // n - 2*length(n) digit
        // smallest number
        String result = "";
 
        // Find the number of
        // digits in number n
        int length
            = (int)Math.floor(
                Math.log10(n) + 1);
        for (int i = 1; i <= n - (2 * length); i++) {
            result += '0';
        }
 
        // To make it smallest n digit
        // number starting and ending
        // with n, we just need to
        // append n at start and end
        result = Integer.toString(n)
                 + result
                 + Integer.toString(n);
 
        // Return the smallest number
        return result;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Number
        int N = 3;
 
        // Function Call
        System.out.println(
            "Smallest Number = "
            + findNumberS(N));
        System.out.print(
            "Largest Number = "
            + findNumberL(N));
    }
}


Python3
# Python3 program for the
# above approach
import math
 
# Function to find n digit
# largest number starting
#and ending with n
def findNumberL(n):
   
    # Corner Case when n = 1
    if (n == 1):
        return "1"
 
    # Result will store the
    # n - 2*length(n) digit
    # largest number
    result = ""
 
    # Find the number of
    # digits in number n
    length  = math.floor(math.log10(n) + 1)
 
    # Append 9
    for i in range(1, n - (2 *
                   length) + 1):
        result += '9'
         
    # To make it largest n digit
    # number starting and ending
    # with n, we just need to
    # append n at start and end
    result = (str(n) + result +
              str(n))
 
    # Return the largest number
    return result
 
# Function to find n digit
# smallest number starting
# and ending with n
def findNumberS(n):
 
    # Corner Case when n = 1
    if (n == 1):
            return "1"
 
    # Result will store the
    # n - 2*length(n) digit
    # smallest number
    result = ""
 
    # Find the number of
    # digits in number n
    length = math.floor(math.log10(n) + 1)
     
    for i in range(1, n -
                   (2 * length) + 1):
        result += '0'
 
    # To make it smallest n digit
    # number starting and ending
    # with n, we just need to
    # append n at start and end
    result = (str(n) + result +
              str(n))
 
    # Return the smallest number
    return result
 
# Driver Code
if __name__ == "__main__":
 
    # Given Number
    N = 3
 
    # Function Call
    print("Smallest Number = " + findNumberS(N))
    print("Largest Number = "+ findNumberL(N))
 
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find n digit
// largest number starting
// and ending with n
static String findNumberL(int n)
{
     
    // Corner Case when n = 1
    if (n == 1)
        return "1";
 
    // Result will store the
    // n - 2*length(n) digit
    // largest number
    String result = "";
 
    // Find the number of
    // digits in number n
    int length = (int)Math.Floor(
                      Math.Log10(n) + 1);
 
    // Append 9
    for(int i = 1;
            i <= n - (2 * length); i++)
    {
        result += '9';
    }
 
    // To make it largest n digit
    // number starting and ending
    // with n, we just need to
    // append n at start and end
    result = n.ToString() + result +
             n.ToString();
 
    // Return the largest number
    return result;
}
 
// Function to find n digit
// smallest number starting
// and ending with n
static String findNumberS(int n)
{
 
    // Corner Case when n = 1
    if (n == 1)
        return "1";
 
    // Result will store the
    // n - 2*length(n) digit
    // smallest number
    String result = "";
 
    // Find the number of
    // digits in number n
    int length = (int)Math.Floor(
                      Math.Log10(n) + 1);
    for (int i = 1;
             i <= n - (2 * length); i++)
    {
        result += '0';
    }
 
    // To make it smallest n digit
    // number starting and ending
    // with n, we just need to
    // append n at start and end
    result = n.ToString() + result +
             n.ToString();
 
    // Return the smallest number
    return result;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given number
    int N = 3;
 
    // Function call
    Console.WriteLine("Smallest Number = " +
                       findNumberS(N));
    Console.Write("Largest Number = " +
                   findNumberL(N));
}
}
 
// This code is contributed by Amit Katiyar


输出:
Smallest Number = 303
Largest Number = 393

时间复杂度: O(N)