Dado un número N, la tarea es encontrar el N-ésimo término de la serie:
1, 2, 11, 12, 21…
Ejemplos:
Input : N = 2 Output : 2 Input : N = 5 Output : 21
Planteamiento:
La idea se basa en que el valor del último dígito se alterna en la serie. Por ejemplo, si el último dígito del número i es 1, entonces el último dígito de los números (i-1) y (i+1) debe ser 2.
Por lo tanto, cree una array de tamaño (n+1) y empuje 1 y 2 (Estos dos son siempre los dos primeros elementos de la serie) a él.
Por tanto, el i-ésimo término del arreglo es:
1) Si i es impar,
arr[i] = arr[i/2]*10 + 1;
2) Si i es par,
arr[i] = arr[(i/2)-1]*10 + 2;
Por fin regresa arr[n].
A continuación se muestra la implementación de la idea anterior:
C++
// C++ program to find // the N-th term in the series // 1, 2, 11, 12, 21... #include <bits/stdc++.h> using namespace std; // Function to find N-th number in series int printNthElement(int N) { // create an array of size (N+1) int arr[N + 1]; arr[1] = 1; arr[2] = 2; for (int i = 3; i <= N; i++) { // If i is odd if (i % 2 != 0) { arr[i] = arr[i / 2] * 10 + 1; } else { arr[i] = arr[(i / 2) - 1] * 10 + 2; } } return arr[N]; } // Driver code int main() { // Get N int N = 5; // Get Nth term cout << printNthElement(N); return 0; }
C
// C program to find // the N-th term in the series // 1, 2, 11, 12, 21... #include <stdio.h> // Function to find N-th number in series int printNthElement(int N) { // create an array of size (N+1) int arr[N + 1]; arr[1] = 1; arr[2] = 2; for (int i = 3; i <= N; i++) { // If i is odd if (i % 2 != 0) { arr[i] = arr[i / 2] * 10 + 1; } else { arr[i] = arr[(i / 2) - 1] * 10 + 2; } } return arr[N]; } // Driver code int main() { // Get N int N = 5; // Get Nth term printf("%d",printNthElement(N)); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java program to find // the N-th term in the series // 1, 2, 11, 12, 21... class FindNth { // Function to find n-th number in series static int printNthElement(int n) { // create an array of size (n+1) int arr[] = new int[n + 1]; arr[1] = 1; arr[2] = 2; for (int i = 3; i <= n; i++) { // If i is odd if (i % 2 != 0) arr[i] = arr[i / 2] * 10 + 1; else arr[i] = arr[(i / 2) - 1] * 10 + 2; } return arr[n]; } // main function public static void main(String[] args) { int n = 5; System.out.println(printNthElement(n)); } }
Python3
# Python3 program to find # the N-th term in the series # 1, 2, 11, 12, 21... # Return n-th number in series def printNthElement(n) : # create an array of size (n + 1) arr =[0] * (n + 1); arr[1] = 1 arr[2] = 2 for i in range(3, n + 1) : # If i is odd if (i % 2 != 0) : arr[i] = arr[i // 2] * 10 + 1 else : arr[i] = arr[(i // 2) - 1] * 10 + 2 return arr[n] # Driver code n = 5 print(printNthElement(n))
C#
// C# program to find // the N-th term in the series // 1, 2, 11, 12, 21... using System; class GFG { // Function to find n-th // number in series static int printNthElement(int n) { // create an array of size (n+1) int []arr = new int[n + 1]; arr[1] = 1; arr[2] = 2; for (int i = 3; i <= n; i++) { // If i is odd if (i % 2 != 0) arr[i] = arr[i / 2] * 10 + 1; else arr[i] = arr[(i / 2) - 1] * 10 + 2; } return arr[n]; } // Driver Code public static void Main() { int n = 5; Console.WriteLine(printNthElement(n)); } } // This code is contributed // by inder_verma
PHP
<?php // PHP program to find // the N-th term in the series // 1, 2, 11, 12, 21... // Function to find N-th // number in series function printNthElement($N) { // create an array of size (N+1) $arr = array($N + 1); $arr[1] = 1; $arr[2] = 2; for ( $i = 3; $i <= $N; $i++) { // If i is odd if ($i % 2 != 0) { $arr[$i] = $arr[$i / 2] * 10 + 1; } else { $arr[$i] = $arr[($i / 2) - 1] * 10 + 2; } } return $arr[$N]; } // Driver code $N = 5; // Get Nth term echo printNthElement($N); // This code is contributed // by Mahadev99 ?>
Javascript
<script> // Javascript program to find // the N-th term in the series // 1, 2, 11, 12, 21... // Function to find n-th number in series function printNthElement(n) { // create an array of size (n+1) let arr = new Array(n + 1); arr[1] = 1; arr[2] = 2; for (let i = 3; i <= n; i++) { // If i is odd if (i % 2 != 0) arr[i] = arr[parseInt(i / 2, 10)] * 10 + 1; else arr[i] = arr[parseInt(i / 2, 10) - 1] * 10 + 2; } return arr[n]; } let n = 5; document.write(printNthElement(n)); // This code is contributed by vaibhavrabadiya117. </script>
21
Publicación traducida automáticamente
Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA