📜  总共n个所需的最小字母数

📅  最后修改于: 2021-04-23 21:31:50             🧑  作者: Mango

给定整数n ,令a = 1,b = 2,c = 3,…..,z = 26 。任务是找到总共n个所需的最小字母
例子:

方法:有两种可能的情况:

  1. 如果n可被26整除,则答案将为n / 26
  2. 如果n不能被26整除,那么答案将是(n / 26)+1

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimum letters
// required to make a total of n
int minLettersNeeded(int n)
{
    if (n % 26 == 0)
        return (n / 26);
    else
        return ((n / 26) + 1);
}
 
// Driver code
int main()
{
    int n = 52;
    cout << minLettersNeeded(n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function to return the minimum letters
    // required to make a total of n
    static int minLettersNeeded(int n)
    {
        if (n % 26 == 0)
            return (n / 26);
        else
            return ((n / 26) + 1);
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 52;
        System.out.print(minLettersNeeded(n));
    }
}


Python3
# Python3 implementation of the approach
 
# Function to return the minimum letters
# required to make a total of n
def minLettersNeeded(n):
 
    if n % 26 == 0:
        return (n//26)
    else:
        return ((n//26) + 1)
 
# Driver code
n = 52
print(minLettersNeeded(n))


C#
// C# implementation of the approach
using System;
class GFG {
 
    // Function to return the minimum letters
    // required to make a total of n
    static int minLettersNeeded(int n)
    {
        if (n % 26 == 0)
            return (n / 26);
        else
            return ((n / 26) + 1);
    }
 
    // Driver code
    public static void Main()
    {
        int n = 52;
        Console.Write(minLettersNeeded(n));
    }
}


PHP


Javascript


输出:
2

时间复杂度: O(1)