Dada una array que representa un número, agregue 1 a la array. Supongamos que una array contiene elementos [1, 2, 3, 4], entonces la array representa el número decimal 1234 y, por lo tanto, agregar 1 a esto daría como resultado 1235. Entonces, la nueva array será [1, 2, 3, 5].
Ejemplos:
Input : [1, 2, 3, 4] Output : [1, 2, 3, 5] Input : [1, 2, 9, 9] Output : [1, 3, 0, 0] Input: [9, 9, 9, 9] Output: [1, 0, 0, 0, 0]
Acercarse :
- Agregue 1 al último elemento de la array, si es menor que 9.
- Si es 9, conviértalo en 0 y repita para el elemento restante de la array.
Implementación:
C++
// C++ Program to add 1 to an array // representing a number #include <bits/stdc++.h> using namespace std; // function to add one and print the array void sum(int arr[], int n) { int i = n; // if array element is less than 9, then // simply add 1 to this. if(arr[i] < 9) { arr[i] = arr[i] + 1; return; } // if array element is greater than 9, // replace it with 0 and decrement i arr[i] = 0; i--; // recursive function sum(arr, i); } // driver code int main() { // number of elements in array int n = 4; // array elements, index of array // should be 1 based, hence, 0 is // added here at arr[0] int arr[] = {0, 1, 9, 3, 9}; // function calling sum(arr, n); // If 1 was appended at head // of array then, print it if(arr[0] > 0) cout << arr[0] << ", "; // print the array elements // after adding one for(int i = 1; i <= n; i++) { cout << arr[i]; if(i < n) cout << ", "; } return 0; }
Java
// Java Program to add 1 to an array // representing a number import java.io.*; public class GFG { // function to add one and print the array void sum(int[] arr, int n) { int i = n; // if array element is less than 9, then // simply add 1 to this. if (arr[i] < 9) { arr[i] = arr[i] + 1; return; } // if array element is greater than 9, // replace it with 0 and decrement i arr[i] = 0; i--; // recursive function sum(arr, i); } // driver code static public void main(String[] args) { GFG obj = new GFG(); // number of elements in array int n = 4; // array elements, index of array // should be 1 based, hence, 0 is // added here at arr[0] int arr[] = {0, 1, 9, 3, 9}; obj.sum(arr, n); // If 1 was appended at head // of array then, print it if (arr[0] > 0) System.out.print(arr[0] + ", "); int i; for (i = 1; i <= n; i++) { System.out.print(arr[i]); if (i < n) System.out.print(", "); } } } // This code is contributed by vt_m.
Python 3
# Python 3 Program to add 1 to an # array representing a number # function to add one and print # the array def sum(arr, n): i = n # if array element is less than # 9, then simply add 1 to this. if(arr[i] < 9): arr[i] = arr[i] + 1 return # if array element is greater # than 9, replace it with 0 # and decrement i arr[i] = 0 i -= 1 # recursive function sum(arr, i) # driver code # number of elements in array n = 4 # array elements, index of array # should be 1 based, hence, 0 is # added here at arr[0] arr = [0, 1, 9, 3, 9] # function calling sum(arr, n) # If 1 was appended at head # of array then, print it if(arr[0] > 0): print(arr[0], ", ", end="") # print the array elements # after adding one for i in range(1, n+1): print(arr[i], end="") if(i < n): print(", ", end="") # This code is contributed by # Smitha Semwal
C#
// C# Program to add 1 to an array // representing a number using System; public class GFG { // function to add one and print the array void sum(int []arr, int n){ int i = n; // if array element is less than 9, then // simply add 1 to this. if(arr[i] < 9) { arr[i] = arr[i] + 1; return; } // if array element is greater than 9, // replace it with 0 and decrement i arr[i] = 0; i--; // recursive function sum(arr, i); } // driver code static public void Main () { GFG obj =new GFG(); // number of elements in array int n = 4; // array elements, index of array // should be 1 based, hence, 0 is // added here at arr[0] int []arr = {0, 1, 9, 3, 9}; // function calling obj.sum(arr, n); // If 1 was appended at head // of array then, print it if(arr[0] > 0) Console.Write(arr[0] + ", "); int i; // print the array elements // after adding one for( i = 1; i <= n; i++) { Console.Write(arr[i]); if(i < n) Console.Write(", "); } } }
Javascript
<script> // JavaScript Program to add 1 to an array // representing a number // function to add one and print the array function sum(arr, n) { var i = n; // if array element is less than 9, then // simply add 1 to this. if(arr[i] < 9) { arr[i] = arr[i] + 1; return; } // if array element is greater than 9, // replace it with 0 and decrement i arr[i] = 0; i--; // recursive function sum(arr, i); } // Driver code // number of elements in array var n = 4; // array elements, index of array // should be 1 based, hence, 0 is // added here at arr[0] arr = [0, 1, 9, 3, 9] // function calling sum(arr, n); // If 1 was appended at head // of array then, print it if(arr[0] > 0) document.write(arr[0] + ", "); // print the array elements // after adding one for(var i = 1; i <= n; i++) { document.write(arr[i]); if(i < n) document.write(", "); } // This code is contributed by rdtank. </script>
Producción
1, 9, 4, 0
Publicación traducida automáticamente
Artículo escrito por shubham_rana_77 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA