有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中将给定的数字转换为幂

我有一个main函数,它调用另一个函数并将给定的数字作为参数传递。在另一个函数中,我想把给定的数分解成幂和。如何做到这一点

我的代码如下所示:

public static void main()
{
String hello=raisetopower(in.nextInt());
}

public String raisetopower(int n)
{
// do the logic
}

说明:

假设数字为25:函数应返回5^2+0^2 如果是26:5^2+1^2


共 (3) 个答案

  1. # 2 楼答案

    我同意Richard的评论,这更多的是数学而不是Java

    这个问题需要更多的信息来提供代码来辅助,比如你是否考虑最大的碱基10,以及是否所有碱基都应该在输出中,即使它们的功率为0,因为你的例子令人困惑:

    "say if the number is 25: the function should return 5^2 + 0^2 and if it is 26: 5^2+1^2"

    然而,考虑到这一点,希望这个例子能够有所帮助


    假设您确实考虑了一个最大基数,您可以从最高基数开始到最低基数,并使用日志

    如果日志的结果大于或等于1,那么这个值应该在输出中,所以减去该值,继续下一个基数

    继续,直到你的值正好为1,这应该是你的最终条件

    例如,假设本例的最大基数为5,输入为27

    log5 27 = 2.04781858346 
    

    所以我们在输出中有5^2,从输入中减去它,你可以用floor来提取'2'

    log4 2 = 0.5 
    

    小于1所以不是整数次幂

    log3 2 = 0.630929753571 
    

    小于1所以不是整数次幂

    log2 2 = 1 
    

    当大于或等于1时添加到输出,当正好为1时终止

    如果你只显示大于或等于1的基数(这还不清楚),此时你的输出将是:

    27 = 25^2 + 2^1
    

    你可以使用一个StringBuilder或一个有序的自定义对象集合来保存你的基础和能力,从而更容易生成输出


    下面是我上述算法的SSCCE
    import java.util.Scanner;
    
    public class NumberToPowerConversion {
        public static void main(String[] args) {
            int maxBaseToConsider = 5;
            System.out.println("Input number to convert: ");
            Scanner input = new Scanner(System.in);
            int number = input.nextInt();
            StringBuilder output = new StringBuilder("Represented as powers: " + number + " = ");
    
            for(int base = maxBaseToConsider; base >= 1; base ){
                //Prevent division by 0 (log 1)
                double logResult = base > 1 ? Math.log(number) / Math.log(base) : 1;
                int floorResult = (int)Math.floor(logResult);
                if(number == 1 || logResult == 1.0){
                    output.append(base + "^" + floorResult);
                    number -= Math.pow(base, floorResult);
                    if(number != 0){
                        //If the number couldn't be broken down completely, add the remainder to the output
                        output.append(" + " + number + "^1"); //To keep format consistent
                    }
                    break; //end condition reached
                }
                else if(floorResult >= 1){
                    output.append(base + "^" + floorResult);
                    number -= Math.pow(base, floorResult);
                    if(number == 0){ break; }
                    else{ output.append(" + "); }
                }
            }
            System.out.println(output.toString());
        }
    }
    


    示例输出:
    Represented as powers: 27 = 5^2 + 2^1
    Represented as powers: 77 = 5^2 + 4^2 + 3^3 + 2^3 + 1^1
    Represented as powers: 234 = 5^3 + 4^3 + 3^3 + 2^4 + 1^1 + 1^1
    Represented as powers: 99 = 5^2 + 4^3 + 3^2 + 2^0
    Represented as powers: 1 = 5^0
    
  2. # 3 楼答案

    /* Algorithm :
       i <- square root of n
       while n<1
         n <- n-square of i
         r <- r concatenate with i^2
         if n=0 r concatenate with i^0
         if n=1 r concatenate with i^1
         i <- square root of n
       return r
    */    
    
    public static String raisetopower(int n){
        String raised="";
        int i=(int)Math.sqrt(n);
    
        while(n>1){
            n=n-(int)Math.pow(i,2);
            raised+=String.valueOf(i)+"^2+"; 
    
            if(n==0)
                raised+="0^2";
            if(n==1)
                raised+="1^2";
            i=(int)Math.sqrt(n);
        }
        return raised;
    }