如果结点号可以写成至少两个K的K + SumOfDIgits(k),则结点号为N。
101, 103, 105, 107, 109, 111, 113, 115….
检查数字N是否为交点数
给定数字N ,任务是检查N是否为交点号。如果N是连接点编号,则打印“是”,否则打印“否” 。
例子:
Input: N = 1106
Output: Yes
Explanation:
1106 = 1093 + (1+0+9+3) = 1102 + (1+1+0+2)
Input: N = 11
Output: No
方法:由于结点号如果可以写为至少两个K的K + SumOfDIgits(k),则它是数字N。因此,对于循环中从1到N的所有数字,我们将检查i + sumOfdigitsof(i)是否等于否。如果等于N,则打印“是”,否则打印“否” 。
下面是上述方法的实现:
C++
// C++ implementation for the
// above approach
#include
using namespace std;
// Function to find the
// sum Of digits of N
int sum(int n)
{
// To store sum of N and
// sumOfdigitsof(N)
int sum = 0;
while (n != 0) {
// extracting digit
int r = n % 10;
sum = sum + r;
n = n / 10;
}
return sum;
}
// Function to check Junction numbers
bool isJunction(int n)
{
// To store count of ways n can be
// represented as i + SOD(i)
int count = 0;
for (int i = 1; i <= n; i++) {
if (i + sum(i) == n)
count++;
}
return count >= 2;
}
// Driver Code
int main()
{
int N = 111;
// Function Call
if (isJunction(N))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program for above approach
class GFG{
// Function to find the
// sum Of digits of N
static int sum(int n)
{
// To store sum of N and
// sumOfdigitsof(N)
int sum = 0;
while (n != 0)
{
// extracting digit
int r = n % 10;
sum = sum + r;
n = n / 10;
}
return sum;
}
// Function to check Junction numbers
static boolean isJunction(int n)
{
// To store count of ways n can be
// represented as i + SOD(i)
int count = 0;
for (int i = 1; i <= n; i++)
{
if (i + sum(i) == n)
count++;
}
return count >= 2;
}
// Driver Code
public static void main(String[] args)
{
int N = 111;
// Function Call
if (isJunction(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
# Function to find the
# sum Of digits of N
def sum1(n):
# To store sum of N and
# sumOfdigitsof(N)
sum1 = 0
while (n != 0):
# extracting digit
r = n % 10
sum1 = sum1 + r
n = n // 10
return sum1
# Function to check Junction numbers
def isJunction(n):
# To store count of ways n can be
# represented as i + SOD(i)
count = 0
for i in range(1, n + 1):
if (i + sum1(i) == n):
count = count + 1
return count >= 2
# Driver Code
if __name__=='__main__':
# Given Number
n = 111
# Function Call
if (isJunction(n) == 1):
print("Yes")
else:
print("No")
# This code is contributed by rock_cool
C#
// C# program for above approach
using System;
class GFG{
// Function to find the
// sum Of digits of N
static int sum(int n)
{
// To store sum of N and
// sumOfdigitsof(N)
int sum = 0;
while (n != 0)
{
// extracting digit
int r = n % 10;
sum = sum + r;
n = n / 10;
}
return sum;
}
// Function to check Junction numbers
static bool isJunction(int n)
{
// To store count of ways n can be
// represented as i + SOD(i)
int count = 0;
for (int i = 1; i <= n; i++)
{
if (i + sum(i) == n)
count++;
}
return count >= 2;
}
// Driver Code
public static void Main()
{
int N = 111;
// Function Call
if (isJunction(N))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Nidhi Biet
Javascript
输出:
Yes
时间复杂度: O(n)
参考:http://www.numbersaplenty.com/set/junction_number/