atoi()的递归实现

这个 atoi() 函数将字符串(表示整数)作为参数并返回其值。

null

我们讨论过 atoi()的迭代实现 .如何递归计算? 我们强烈建议您尽量减少浏览器,并先自己尝试 其思想是分离最后一个数字,递归计算剩余n-1个数字的结果,将结果乘以10,并将获得的值与最后一个数字相加。 下面是这个想法的实施。

C++

// Recursive C program to compute atoi()
#include <stdio.h>
#include <string.h>
// Recursive function to compute atoi()
int myAtoiRecursive( char *str, int n)
{
// Base case (Only one digit)
if (n == 1)
return *str - '0' ;
// If more than 1 digits, recur for (n-1), multiply result with 10
// and add last digit
return (10 * myAtoiRecursive(str, n - 1) + str[n-1] - '0' );
}
// Driver Program
int main( void )
{
char str[] = "112" ;
int n = strlen (str);
printf ( "%d" , myAtoiRecursive(str, n));
return 0;
}


JAVA

// Recursive Java program to compute atoi()
class GFG{
// Recursive function to compute atoi()
static int myAtoiRecursive(String str, int n)
{
// Base case (Only one digit)
if (n == 1 )
{
return str.charAt( 0 ) - '0' ;
}
// If more than 1 digits, recur for (n-1),
// multiply result with 10 and add last digit
return ( 10 * myAtoiRecursive(str, n - 1 ) +
str.charAt(n - 1 ) - '0' );
}
// Driver code
public static void main(String[] s)
{
String str = "112" ;
int n = str.length();
System.out.println(myAtoiRecursive(str, n));
}
}
// This code is contributed by rutvik_56


Python3

# Python3 program to compute atoi()
# Recursive function to compute atoi()
def myAtoiRecursive(string, num):
# base case, we've hit the end of the string,
# we just return the last value
if len (string) = = 1 :
return int (string) + (num * 10 )
# add the next string item into our num value
num = int (string[ 0 : 1 ]) + (num * 10 )
# recurse through the rest of the string
# and add each letter to num
return myAtoiRecursive(string[ 1 :], num)
# Driver Code
string = "112"
print (myAtoiRecursive(string, 0 ))
# This code is contributed by Frank-Hu-MSFT


C#

// Recursive C# program to compute atoi()
using System;
class GFG{
// Recursive function to compute atoi()
static int myAtoiRecursive( string str, int n)
{
// Base case (Only one digit)
if (n == 1)
{
return str[0] - '0' ;
}
// If more than 1 digits, recur for (n-1),
// multiply result with 10 and add last digit
return (10 * myAtoiRecursive(str, n - 1) +
str[n - 1] - '0' );
}
// Driver code
public static void Main()
{
string str = "112" ;
int n = str.Length;
Console.Write(myAtoiRecursive(str, n));
}
}
// This code is contributed by Nidhi_Biet


Javascript

<script>
// Recursive Javascript program to compute atoi()
// Recursive function to compute atoi()
function myAtoiRecursive(str, n)
{
// Base case (Only one digit)
if (n == 1)
{
return str[0].charCodeAt() - '0' .charCodeAt();
}
// If more than 1 digits, recur for (n-1),
// multiply result with 10 and add last digit
return (10 * myAtoiRecursive(str, n - 1) + str[n - 1].charCodeAt() - '0' .charCodeAt());
}
let str = "112" ;
let n = str.length;
document.write(myAtoiRecursive(str, n));
// This code is contributed by decode2207.
</script>


输出:

112

本文由 纳伦德拉·康拉尔卡 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享