Gapful Number是至少3位数字的N ,因此可以通过将其第一位和最后一位数字串联而将其整除。
很少有缺口的数字是:
100, 105, 108, 110, 120, 121, 130, 132, 135, 140,…
检查N是否为空位编号
给定一个整数N ,任务是检查N是否为空缺数。如果N是一个空位数字,则打印“是”,否则打印“否” 。
例子:
Input: N = 108
Output: Yes
Explanation:
108 is divisible by 18
Input: N = 112
Output: No
方法:想法是使用给定数字的第一位和最后一位创建一个数字(例如num ),并检查N是否可被num整除。如果N可被num整除,则它是一个有空数并打印“是” ,否则打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Find the first digit
int firstDigit(int n)
{
// Find total number of digits - 1
int digits = (int)log10(n);
// Find first digit
n = (int)(n / pow(10, digits));
// Return first digit
return n;
}
// Find the last digit
int lastDigit(int n)
{
// return the last digit
return (n % 10);
}
// A function to check Gapful numbers
bool isGapful(int n)
{
int first_dig = firstDigit(n);
int last_dig = lastDigit(n);
int concatenation = first_dig * 10
+ last_dig;
// Return true if n is gapful number
return (n % concatenation == 0);
}
// Driver Code
int main()
{
// Given Number
int n = 108;
// Function Call
if (isGapful(n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program for the above approach
class GFG{
// Find the first digit
static int firstDigit(int n)
{
// Find total number of digits - 1
int digits = (int)(Math.log(n) /
Math.log(10));
// Find first digit
n = (int)(n / Math.pow(10, digits));
// Return first digit
return n;
}
// Find the last digit
static int lastDigit(int n)
{
// Return the last digit
return (n % 10);
}
// A function to check Gapful numbers
static boolean isGapful(int n)
{
int first_dig = firstDigit(n);
int last_dig = lastDigit(n);
int concatenation = first_dig * 10 +
last_dig;
// Return true if n is gapful number
return (n % concatenation == 0);
}
// Driver code
public static void main(String[] args)
{
// Given number
int n = 108;
// Function call
if (isGapful(n))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by Pratima Pandey
Python3
# Python3 program for the above approach
import math
# Find the first digit
def firstDigit(n):
# Find total number of digits - 1
digits = math.log10(n)
# Find first digit
n = (n / math.pow(10, digits))
# Return first digit
return n
# Find the last digit
def lastDigit(n):
# return the last digit
return (n % 10)
# A function to check Gapful numbers
def isGapful(n):
concatenation = (firstDigit(n) * 10) +\
lastDigit(n)
# Return true if n is gapful number
return (n % concatenation)
# Driver Code
if __name__=='__main__':
# Given Number
n = 108
# Function Call
if (isGapful(n)):
print("Yes")
else:
print("No")
# This code is contributed by Ritik Bansal
C#
// C# program for the above approach
using System;
class GFG{
// Find the first digit
static int firstDigit(int n)
{
// Find total number of digits - 1
int digits = (int)(Math.Log(n) /
Math.Log(10));
// Find first digit
n = (int)(n / Math.Pow(10, digits));
// Return first digit
return n;
}
// Find the last digit
static int lastDigit(int n)
{
// Return the last digit
return (n % 10);
}
// A function to check Gapful numbers
static bool isGapful(int n)
{
int first_dig = firstDigit(n);
int last_dig = lastDigit(n);
int concatenation = first_dig * 10 +
last_dig;
// Return true if n is gapful number
return (n % concatenation == 0);
}
// Driver code
public static void Main()
{
// Given number
int n = 108;
// Function call
if (isGapful(n))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Code_Mech
Javascript
输出:
Yes
时间复杂度: O(1)
参考: https : //oeis.org/A108343