Dado un número entero N , la tarea es comprobar si N es un número ondulante no trivial.
Número ondulante no trivial son números en base 10 > 100 que son de la forma aba, abab, ababa, …, donde a != b.
Ejemplos:
Entrada: N = 121
Salida: Sí
Explicación:
121 tiene la forma abaEntrada: N = 123
Salida: No
Enfoque: La idea es convertir el número en la string. Si la longitud de la string es par, comprobaremos si la primera mitad de la string es igual a la segunda mitad o no. Si la longitud es impar, agregaremos un segundo carácter de la string en el último de la string para que su longitud sea uniforme. Finalmente, verifique que la primera mitad de la string sea igual a la segunda mitad o no.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to check if N // is a Nontrivial undulant number #include<bits/stdc++.h> using namespace std; // Function to check if a string // is double string or not bool isDouble(int num) { string s = to_string(num); int l = s.length(); // a and b should not be equal if(s[0] == s[1]) return false; // Condition to check // if length is odd // make length even if(l % 2 == 1) { s = s + s[1]; l++; } // first half of s string s1 = s.substr(0, l/2); // second half of s string s2 = s.substr(l/2); // Double string if first // and last half are equal return s1 == s2; } // Function to check if N is an // Nontrivial undulant number bool isNontrivialUndulant(int N) { return N > 100 && isDouble(N); } // Driver Code int main() { int n = 121; if (isNontrivialUndulant(n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation to check if N // is a Nontrivial undulant number class GFG{ // Function to check if a string // is double string or not static boolean isDouble(int num) { String s = Integer.toString(num); int l = s.length(); // a and b should not be equal if(s.charAt(0) == s.charAt(1)) return false; // Condition to check if length // is odd make length even if(l % 2 == 1) { s = s + s.charAt(1); l++; } // First half of s String s1 = s.substring(0, l / 2); // Second half of s String s2 = s.substring(l / 2); // Double string if first // and last half are equal return s1.equals(s2); } // Function to check if N is an // Nontrivial undulant number static boolean isNontrivialUndulant(int N) { return N > 100 && isDouble(N); } // Driver code public static void main(String[] args) { int n = 121; if (isNontrivialUndulant(n)) { System.out.println("Yes"); } else { System.out.println("No"); } } } // This code is contributed by shubham
Python3
# Python3 implementation to check if N # is a Nontrivial undulant number # Function to check if a string # is double string or not def isDouble(num): s = str(num) l = len(s) # a and b should not be equal if(s[0] == s[1]): return False # Condition to check # if length is odd # make length even if(l % 2 == 1): s = s + s[1] l += 1 # First half of s s1 = s[:l // 2] # Second half of s s2 = s[l // 2:] # Double string if first # and last half are equal return s1 == s2 # Function to check if N is an # Nontrivial undulant number def isNontrivialUndulant(N): return N > 100 and isDouble(N) # Driver Code n = 121 if (isNontrivialUndulant(n)): print("Yes") else: print("No") # This code is contributed by vishu2908
C#
// C# implementation to check if N // is a Nontrivial undulant number using System; class GFG{ // Function to check if a string // is double string or not static bool isDouble(int num) { String s = num.ToString(); int l = s.Length; // a and b should not be equal if(s[0] == s[1]) return false; // Condition to check if length // is odd make length even if(l % 2 == 1) { s = s + s[1]; l++; } // First half of s String s1 = s.Substring(0, l / 2); // Second half of s String s2 = s.Substring(l / 2); // Double string if first // and last half are equal return s1.Equals(s2); } // Function to check if N is an // Nontrivial undulant number static bool isNontrivialUndulant(int N) { return N > 100 && isDouble(N); } // Driver code public static void Main(String[] args) { int n = 121; if (isNontrivialUndulant(n)) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } } // This code is contributed by gauravrajput1
Javascript
<script> // JavaScript implementation to check if N // is a Nontrivial undulant number // Function to check if a string // is double string or not function isDouble(num) { let s = num.toString(); let l = s.length; // a and b should not be equal if (s[0] == s.charAt[1]) return false; // Condition to check if length // is odd make length even if (l % 2 == 1) { s = s + s[1]; l++; } // First half of s let s1 = s.substr(0, l / 2); // Second half of s let s2 = s.substr(l / 2); // Double string if first // and last half are equal return (s1 == s2); } // Function to check if N is an // Nontrivial undulant number function isNontrivialUndulant(N) { return N > 100 && isDouble(N); } // Driver Code let n = 121; if (isNontrivialUndulant(n)) { document.write("Yes"); } else { document.write("No"); } // This code is contributed by susmitakundugoaldanga </script>
Yes
Complejidad de tiempo: O(|N|)
Referencias: OEIS