给定整数n ,令a = 1,b = 2,c = 3,…..,z = 26 。任务是找到总共n个所需的最小字母数。
例子:
Input: n = 48
Output: 2
48 can be written as z + v, where z = 26 and v = 22
Input: n = 23
Output: 1
方法:有两种可能的情况:
- 如果n可被26整除,则答案将为n / 26 。
- 如果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)