如何在 java 中的递归期间将数据保存为整数
how to save data in an integer during recursion in java
我想写一个递归方法来接收一个数字和一个数字。该方法应该 return 第二个数字仅包含可被第二个参数整除的第一个参数的数字。
例如,如果使用数字 = 5369405 和数字 = 5 调用该方法,则它应该 return 505。
到目前为止,我已经编写了以下方法,但它不起作用,因为索引保持在 -1,我想在每次递归期间保存它的值。我的想法是保存值以构建 returned 数字。
谁能告诉我如何在每次递归期间保存索引以防止它 returning -1?
这是我写的代码:
public static int subNumber(int num, int digit)
{
int index=-1;
if(num <10)
{
if(num%digit==0){
index++;
return num*(int)Math.pow(10,index);
}
else{
return 0;
}
}
int mod=(num%10);
if(mod % digit ==0)
{
index++;
return mod*(int)Math.pow(10,index)+subNumber(num/10,digit);
}
else{
return 0+subNumber(num/10,digit);
}
}
正如@markspace 在评论中建议的那样,您还应该将索引传递给您的方法以跟踪每个可整除数字的位置,以便正确计算它们在最终数字中的权重。
此外,在设计递归方法时,您应该首先确定您的基本情况。在这种情况下,您的基本情况是:
传递的数字小于要除的数字
传递的数字等于要除的数字。
此外,用户可以传递不一致的索引(除零以外的任何索引)。通常在这些情况下,当用户应该传递仅用于帮助您进行递归计算的参数时,您应该隐藏实际的递归方法并仅提供它的客户端版本。基本上,此客户端版本仅使用正确的参数调用正确的递归方法以避免误用。
一种方法解决方案
public static int subNumber(int num, int digit, int index) {
//Base case where num is smaller than than the digit to divide with
if (num < digit) {
return 0;
}
//Base case where num corresponds to digit and therefore is divisible
if (num == digit) {
return num * ((int) Math.pow(10, index));
}
//Retrieving the less significant digit
int modDigit = (num % 10);
//If the current mod digit is divisible by digit then its i-th position is defined and the later call results are added to it
if (modDigit % digit == 0) {
return modDigit * ((int) Math.pow(10, index)) + subNumber(num / 10, digit, index + 1);
}
//Proceeding with the recursion without adding a value to sum (conceptually should be return 0 + subNumber(...))
return subNumber(num / 10, digit, index);
}
客户端方法解决方案
public static int subNumberClient(int num, int digit){
return subNumber(num, digit, 0);
}
private static int subNumber(int num, int digit, int index) {
//same implementation but with private access modifier
}
我想写一个递归方法来接收一个数字和一个数字。该方法应该 return 第二个数字仅包含可被第二个参数整除的第一个参数的数字。
例如,如果使用数字 = 5369405 和数字 = 5 调用该方法,则它应该 return 505。
到目前为止,我已经编写了以下方法,但它不起作用,因为索引保持在 -1,我想在每次递归期间保存它的值。我的想法是保存值以构建 returned 数字。
谁能告诉我如何在每次递归期间保存索引以防止它 returning -1?
这是我写的代码:
public static int subNumber(int num, int digit)
{
int index=-1;
if(num <10)
{
if(num%digit==0){
index++;
return num*(int)Math.pow(10,index);
}
else{
return 0;
}
}
int mod=(num%10);
if(mod % digit ==0)
{
index++;
return mod*(int)Math.pow(10,index)+subNumber(num/10,digit);
}
else{
return 0+subNumber(num/10,digit);
}
}
正如@markspace 在评论中建议的那样,您还应该将索引传递给您的方法以跟踪每个可整除数字的位置,以便正确计算它们在最终数字中的权重。
此外,在设计递归方法时,您应该首先确定您的基本情况。在这种情况下,您的基本情况是:
传递的数字小于要除的数字
传递的数字等于要除的数字。
此外,用户可以传递不一致的索引(除零以外的任何索引)。通常在这些情况下,当用户应该传递仅用于帮助您进行递归计算的参数时,您应该隐藏实际的递归方法并仅提供它的客户端版本。基本上,此客户端版本仅使用正确的参数调用正确的递归方法以避免误用。
一种方法解决方案
public static int subNumber(int num, int digit, int index) {
//Base case where num is smaller than than the digit to divide with
if (num < digit) {
return 0;
}
//Base case where num corresponds to digit and therefore is divisible
if (num == digit) {
return num * ((int) Math.pow(10, index));
}
//Retrieving the less significant digit
int modDigit = (num % 10);
//If the current mod digit is divisible by digit then its i-th position is defined and the later call results are added to it
if (modDigit % digit == 0) {
return modDigit * ((int) Math.pow(10, index)) + subNumber(num / 10, digit, index + 1);
}
//Proceeding with the recursion without adding a value to sum (conceptually should be return 0 + subNumber(...))
return subNumber(num / 10, digit, index);
}
客户端方法解决方案
public static int subNumberClient(int num, int digit){
return subNumber(num, digit, 0);
}
private static int subNumber(int num, int digit, int index) {
//same implementation but with private access modifier
}