我正在使用这个逻辑。假设我必须在10中找到3个数字!然后enter image description here
import java.util.*;
import java.io.*;
class testing
{
public static void main(String args[])
{
int temp=0,a;
Scanner obj = new Scanner(System.in);
System.out.print("Enter the number ");
int n = obj.nextInt();
int i=1;
while(n<(Math.pow(3,i)))
{
a= n/(int)(Math.pow(3,i));
temp=temp+a;
i++;
}
System.out.println("Answer: " +temp);
}
}
答案 0 :(得分:0)
更改&lt;到&gt;如果条件为
n >(Math.pow(3,i))
或使用以下只计算一次电力的代码
int temp = 0, a;
Scanner obj = new Scanner(System.in);
System.out.print("Enter the number ");
int n = obj.nextInt();
int i = 1;
while (true) {
int pow = (int) Math.pow(3, i);
if (pow > n) {
break;
} else {
temp = temp + n / pow;
}
i++;
}
System.out.println("Answer: " + temp);