El número de unión es un número N si se puede escribir como K + SumOfDIgits(k) para al menos dos K.
101, 103, 105, 107, 109, 111, 113, 115….
Comprobar si un número N es un número de cruce
Dado un número N , la tarea es comprobar si N es un número de unión o no. Si N es un número de cruce, escriba «Sí» , de lo contrario, escriba «No» .
Ejemplos:
Entrada: N = 1106
Salida: Sí
Explicación:
1106 = 1093 + (1+0+9+3) = 1102 + (1+1+0+2)
Entrada: N = 11
Salida: No
Enfoque: dado que el número de unión es un número N si se puede escribir como K + SumOfDIgits(k) para al menos dos K. Entonces, para todos los números del 1 al N en un ciclo, verificaremos si i + sumOfdigitsof(i) es igual a N o no. Si es igual a N, escriba «Sí» , de lo contrario, escriba «No» .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation for the // above approach #include <bits/stdc++.h> 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
<script> // Javascript program for above approach // Function to find the // sum Of digits of N function sum( n) { // To store sum of N and // sumOfdigitsof(N) let sum = 0; while (n != 0) { // extracting digit let r = n % 10; sum = sum + r; n = parseInt(n / 10); } return sum; } // Function to check Junction numbers function isJunction( n) { // To store count of ways n can be // represented as i + SOD(i) let count = 0; for ( i = 1; i <= n; i++) { if (i + sum(i) == n) count++; } return count >= 2; } // Driver Code let N = 111; // Function Call if (isJunction(N)) document.write("Yes"); else document.write("No"); // This code contributed by Rajput-Ji </script>
Yes
Complejidad de tiempo: O(n)
Complejidad espacial: O(1)
Referencia : http://www.numbersaplenty.com/set/junction_number/