给定范围[L,R] 。任务是找到写该范围内每个数字所需的十六进制字母总数。
十六进制字母是[A,F]范围内的字母,代表[10,15]范围内的十进制数字
例子:
Input: L = 10, R = 15
Output: 6
All the numbers from 10 to 15 contain a hexadecimal alphabet.
Input: L = 15, R = 16
Output: 1
15 and 16 are represented in hexadecimal as F and 10 respectively.
方法:
- 首先,检查num≥10和num≤15 。如果是,则将计数递增,因为十进制数从10到15包含一个十六进制字母。
- 如果num> 15 ,则将数字更新为num = num%16 。如果大于10,则增加计数。
- 重复第二步,直到数字(每个数字)大于0。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that will count
// total hexadecimal alphabet
int countHexadecimal(int L, int R)
{
int count = 0;
for (int i = L; i <= R; i++) {
// All the numbers from 10 to 15
// contain a hexadecimal alphabet
if (i >= 10 && i <= 15)
count++;
// If i > 15 then perform mod by 16 repeatedly
// till the number is > 0
// If number % 16 > 10 then increase count
else if (i > 15) {
int k = i;
while (k != 0) {
if (k % 16 >= 10)
count++;
k = k / 16;
}
}
}
return count;
}
// Driver code
int main()
{
int L = 5, R = 100;
cout << countHexadecimal(L, R);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function that will count
// total hexadecimal alphabet
static int countHexadecimal(int L, int R)
{
int count = 0;
for (int i = L; i <= R; i++)
{
// All the numbers from 10 to 15
// contain a hexadecimal alphabet
if (i >= 10 && i <= 15)
count++;
// If i > 15 then perform mod by 16
// repeatedly till the number is > 0
// If number % 16 > 10 then increase count
else if (i > 15)
{
int k = i;
while (k != 0)
{
if (k % 16 >= 10)
count++;
k = k / 16;
}
}
}
return count;
}
// Driver code
public static void main(String args[])
{
int L = 5, R = 100;
System.out.print(countHexadecimal(L, R));
}
}
// This code is contributed
// by Akanksha Rai
Python3
# Python3 implementation of the approach
# Function that will count
# total hexadecimal alphabet
def countHexadecimal(L, R) :
count = 0;
for i in range(L, R + 1) :
# All the numbers from 10 to 15
# contain a hexadecimal alphabet
if (i >= 10 and i <= 15) :
count += 1;
# If i > 15 then perform mod by 16
# repeatedly till the number is > 0
# If number % 16 > 10 then
# increase count
elif (i > 15) :
k = i;
while (k != 0) :
if (k % 16 >= 10) :
count += 1;
k = k // 16;
return count;
# Driver code
if __name__ == "__main__" :
L = 5; R = 100;
print(countHexadecimal(L, R));
# This code is contributed by Ryuga
C#
// C# implementation of the approach
using System;
class GFG
{
// Function that will count
// total hexadecimal alphabet
static int countHexadecimal(int L, int R)
{
int count = 0;
for (int i = L; i <= R; i++)
{
// All the numbers from 10 to 15
// contain a hexadecimal alphabet
if (i >= 10 && i <= 15)
count++;
// If i > 15 then perform mod by 16 repeatedly
// till the number is > 0
// If number % 16 > 10 then increase count
else if (i > 15)
{
int k = i;
while (k != 0)
{
if (k % 16 >= 10)
count++;
k = k / 16;
}
}
}
return count;
}
// Driver code
public static void Main()
{
int L = 5, R = 100;
Console.Write(countHexadecimal(L, R));
}
}
// This code is contributed
// by Akanksha Rai
PHP
= 10 && $i <= 15)
$count++;
// If i > 15 then perform mod by 16
// repeatedly till the number is > 0
// If number % 16 > 10 then increase count
else if ($i > 15)
{
$k = $i;
while ($k != 0)
{
if ($k % 16 >= 10)
$count++;
$k = $k / 16;
}
}
}
return $count;
}
// Driver code
$L = 5;
$R = 100;
echo countHexadecimal($L, $R);
// This code is contributed by Ita_c
?>
Javascript
输出:
36
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。