Table of Contents
Example
Home Backend Development C++ Add 1 to the number represented by the array (recursive method)

Add 1 to the number represented by the array (recursive method)

Aug 28, 2023 pm 05:17 PM
number array recursion

Add 1 to the number represented by the array (recursive method)

Given an array that is a collection of numbers represented by non-negative numbers, add 1 to the number (increment the number represented by the number). Numbers are stored in such a way that the highest digit is the first element of the array.

To add a number by 1 to the number represented by the number

  • Starting from the end of the array, addition means rounding the last number 4 to 5.

  • If the last element is 9, change it to 0 and carry = 1.

  • For the next iteration, check the carry and if it adds to 10, do the same as step 2.

  • After adding the carry, set the carry to 0 for the next iteration.

  • If vectors are added and the vector size is increased, append 1 at the beginning.

Suppose an array contains elements [7, 6, 3, 4], then the array represents the decimal number 1234, so adding 1 to this number will give 7635. So the new array will be [7, 6, 3, 5].

Example

Input: [7, 6, 9, 9]
Output: [7, 7, 0, 0]
Input: [4, 1, 7, 8, 9]
Output: [4, 1, 7, 9, 0]
Copy after login

Explanation Add 1 to the last element of the array if it is less than 9. If the element is 9, change it to 0 and recurse for the remaining elements of the array.

Example

Explanation If the last element of the array is less than 9, add 1 to it. If the element is 9, change it to 0 and do a recursive operation on the remaining elements of the array.

Example

#include <iostream>
using namespace std;
void sum(int arr[], int n) {
   int i = n;
   if(arr[i] < 9) {
      arr[i] = arr[i] + 1;
      return;
   }
   arr[i] = 0;
   i--;
   sum(arr, i);
   if(arr[0] > 0) {
      cout << arr[0] << ", ";
   }
   for(int i = 1; i <= n; i++) {
      cout << arr[i];
      if(i < n) {
         cout << ", ";
      }
   }
}
int main() {
   int n = 4;
   int arr[] = {4, 1, 7, 8, 9};
   sum(arr, n);
   return 0;
}
Copy after login

The above is the detailed content of Add 1 to the number represented by the array (recursive method). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

Detailed explanation of C++ function recursion: tail recursion optimization Detailed explanation of C++ function recursion: tail recursion optimization May 03, 2024 pm 04:42 PM

Recursive definition and optimization: Recursive: A function calls itself internally to solve difficult problems that can be decomposed into smaller sub-problems. Tail recursion: The function performs all calculations before making a recursive call, which can be optimized into a loop. Tail recursion optimization condition: recursive call is the last operation. The recursive call parameters are the same as the original call parameters. Practical example: Calculate factorial: The auxiliary function factorial_helper implements tail recursion optimization, eliminates the call stack, and improves efficiency. Calculate Fibonacci numbers: The tail recursive function fibonacci_helper uses optimization to efficiently calculate Fibonacci numbers.

What are the debugging techniques for recursive calls in Java functions? What are the debugging techniques for recursive calls in Java functions? May 05, 2024 am 10:48 AM

The following techniques are available for debugging recursive functions: Check the stack traceSet debug pointsCheck if the base case is implemented correctlyCount the number of recursive callsVisualize the recursive stack

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

Realme GT Neo6 is scheduled to be released on May 9th! The first AI digital human conference in the computer industry Realme GT Neo6 is scheduled to be released on May 9th! The first AI digital human conference in the computer industry May 08, 2024 pm 12:49 PM

On May 7, our mobile phone manufacturer officially announced that our company’s GTNeo6 launch conference is scheduled for May 9. GTNoe6 is positioned as a "performance storm", aiming to stir up the mid-range machine situation. In addition, this conference will also be the first AI digital human conference in the mobile phone industry. At that time, Realme Vice President, Global Marketing President, and China President Xu Qi will appear at the press conference in the form of a digital human. Digital man Xu Qi According to the official introduction, Realme GTNoe6, codenamed "Hurricane", is faster and stronger. It will challenge the strongest third-generation Snapdragon 8s flagship and the strongest product in its class. Recently, the Realme GTNeo6 was found to be directly on the e-commerce platform. Some core configurations were exposed, showing that the machine is not only equipped with a Snapdragon 8s processor, but also supports 120W flash charging.

What are the advantages of recursive calls in Java functions? What are the advantages of recursive calls in Java functions? May 03, 2024 am 09:09 AM

Answer: The advantages of using recursive calling Java functions include: clarity and conciseness, efficiency, maintainability, simple modeling and practical cases. Clear and concise: Recursive code is simpler and easier to understand than iterative methods, reducing the level of code nesting. Efficient: In some cases, recursion is more efficient than iteration because the overhead of creating and destroying new function calls is eliminated. Maintainability: Recursive code is easier to maintain than code using loops because recursive methods have clear termination conditions. Simple modeling: Recursion provides a natural way to model problems with a recursive structure. Practical case: The factorial evaluation function demonstrates the implementation and advantages of recursion.

Detailed explanation of C++ function recursion: application of recursion in programming competitions Detailed explanation of C++ function recursion: application of recursion in programming competitions May 04, 2024 pm 09:48 PM

Recursion is a function self-calling technique that solves a problem based on smaller instances and then combines the results to solve the original problem. Its advantages include code simplicity and the ability to solve self-similar problems, but the disadvantage is that it may lead to stack overflow. Problems such as the Fibonacci sequence can be easily calculated using recursive functions. In programming competitions, recursion is used in problems such as solving mazes, finding shortest paths, and sorting tree structures. For example, the Tower of Hanoi problem can be solved using a recursive function, which involves moving the disks in the tower to another column, one disk at a time.

See all articles