이 논리를 사용하고 있습니다. 내가 10에서 3의 수를 찾아야한다고 가정하자!. 그런 다음 여기에 이미지 설명을 입력하십시오.
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);
}
}
if 조건에서 <를>로 변경하십시오.
n >(Math.pow(3,i))
또는 pow를 한 번만 계산하는 아래 코드를 사용하십시오.
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);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다