递归分割方法 [英] recursive division method

查看:196
本文介绍了递归分割方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个递归方法,将第一个值(基数为10)转换为第二个值的数字。这是我到目前为止,但由于某种原因,我无法让递归函数正常工作。谢谢。

  package lab06250; 

import java.util.Scanner;

public class Main {

public static void main(String [] args){
Number newNumber;
newNumber = new Number();
扫描仪kbd =新扫描仪(System.in);
int数字;
int余数= 0;
int base;

System.out.println(Enter number:);
number = kbd.nextInt();
System.out.println(Enter base);
base = kbd.nextInt();
kbd.nextLine();

System.out.println(Division(number,base));

$ b public static int Division(int n,int b){
int result;
if(n == 1)
result = 1;
else
result = Division(b,(n / b));

return n;
}

}


解决方案

首先,我认为如果你想改变基数(在这里猜数字系统),你的逻辑中有一个流程:

的工作原理如下:



十进制11(数字系统10)然后

  1 *(power(10 ,1))+ 1 *(power(10,0)

2基地)

1011

  1 *(power(2 * 3))+ 0 *(power(2 * 2))+ 1 *(power(2 * 1))+ 1 *(power(2 * 0))

相当于辛烷值(8个基数中最高的7个)

13

  1 *(power(8 * 1))+ 3 *(power(8 * 0))

您需要根据上述内容编写一些内容并纠正您的逻辑。只有一个建议使用%而不是除法并尝试添加结果。


I need to create a recursive method that will convert the first value (base 10) into a number in the base of the second. This is what I have so far, but for some reason I cannot get the recursive function to work properly. Thank you.

package lab06250;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
      Number newNumber;
      newNumber = new Number();
      Scanner kbd = new Scanner(System.in);
      int number;
      int remainder = 0;
      int base;

      System.out.println("Enter number:");
      number = kbd.nextInt();
      System.out.println("Enter base");
      base = kbd.nextInt();
      kbd.nextLine();

     System.out.println(Division(number, base));

    }
    public static int Division(int n, int b){
        int result;
        if (n == 1)
            result = 1;
            else
               result = Division(b, (n / b));

        return n;
    }

}

解决方案

First of all I think there is a flow in your logic if you want to change base (guess number system here)

works as follows:

11 in decimal (numeric system 10) then

1*(power(10,1)) + 1*(power(10,0)

Its binary equivalent (1 highest in 2 base)

1011

1*(power(2*3)) + 0*(power(2*2)) + 1*(power(2*1)) + 1*(power(2*0))

Its octane equivalent (7 highest in 8 base)

13

1*(power(8*1)) + 3*(power(8*0))  

You need to write something on the bases of above and rectify your logic. Just one suggestion use % instead of division and try adding the result.

这篇关于递归分割方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆