给定一个数字H ,该数字代表孔的总数。任务是找到具有那么多孔的最小数量。
笔记:
- 0、4、6、9每个都有1个孔,而8个则有2个孔。
- 该数字不应包含前导零。
例子:
Input: H = 1
Output: 0
Input: H = 5
Output: 488
Explanation:
Number which has 5 holes in it is 488. i.e (1 + 2 + 2)
参考:计算整数中的孔数
方法:
- 首先,检查给定的孔数是0还是1,如果为0,则打印1;如果为1,则打印0。
- 如果给定的孔数大于1,则将孔数除以2,并将剩余的数存储在‘rem’变量中。和‘quo’变量中的商。
- 现在,如果rem变量的值等于1,则首先打印4次,然后打印8次。
- 否则仅打印8次。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function that will find out
// the number
void printNumber(int holes)
{
// If number of holes
// equal 0 then return 1
if (holes == 0)
cout << "1";
// If number of holes
// equal 0 then return 0
else if (holes == 1)
cout << "0";
// If number of holes
// is more than 0 or 1.
else {
int rem = 0, quo = 0;
rem = holes % 2;
quo = holes / 2;
// If number of holes is
// odd
if (rem == 1)
cout << "4";
for (int i = 0; i < quo; i++)
cout << "8";
}
}
// Driver code
int main()
{
int holes = 3;
// Calling Function
printNumber(holes);
return 0;
}
Java
// Java implementation of the above approach
import java.io.*;
class GFG
{
// Function that will find out
// the number
static void printNumber(int holes)
{
// If number of holes
// equal 0 then return 1
if (holes == 0)
System.out.print("1");
// If number of holes
// equal 0 then return 0
else if (holes == 1)
System.out.print("0");
// If number of holes
// is more than 0 or 1.
else
{
int rem = 0, quo = 0;
rem = holes % 2;
quo = holes / 2;
// If number of holes is
// odd
if (rem == 1)
System.out.print("4");
for (int i = 0; i < quo; i++)
System.out.print("8");
}
}
// Driver code
public static void main (String[] args)
{
int holes = 3;
// Calling Function
printNumber(holes);
}
}
// This code is contributed by Sachin.
Python3
# Python3 implementation of
# the above approach
# Function that will find out
# the number
def printNumber(holes):
# If number of holes
# equal 0 then return 1
if (holes == 0):
print("1")
# If number of holes
# equal 0 then return 0
elif (holes == 1):
print("0", end = "")
# If number of holes
# is more than 0 or 1.
else:
rem = 0
quo = 0
rem = holes % 2
quo = holes // 2
# If number of holes is
# odd
if (rem == 1):
print("4", end = "")
for i in range(quo):
print("8", end = "")
# Driver code
holes = 3
# Calling Function
printNumber(holes)
# This code is contributed by Mohit kumar
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function that will find out
// the number
static void printNumber(int holes)
{
// If number of holes
// equal 0 then return 1
if (holes == 0)
Console.Write ("1");
// If number of holes
// equal 0 then return 0
else if (holes == 1)
Console.Write ("0");
// If number of holes
// is more than 0 or 1.
else
{
int rem = 0, quo = 0;
rem = holes % 2;
quo = holes / 2;
// If number of holes is
// odd
if (rem == 1)
Console.Write ("4");
for (int i = 0; i < quo; i++)
Console.Write ("8");
}
}
// Driver code
static public void Main ()
{
int holes = 3;
// Calling Function
printNumber(holes);
}
}
// This code is contributed by jit_t
Javascript
输出:
48