https://jaimemin.tistory.com/1076
https://github.com/kks227/BOJ/blob/master/10800/10827.cpp
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String arg[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
String a = st.nextToken();
int b = Integer.parseInt(st.nextToken());
int finder = a.indexOf('.'); //소수점 위치
int idx = 0;
idx = a.length() -1 - finder;
a = a.substring(0,finder) + a.substring(finder+1); //소수점 제거
idx *= b; //계산 후 소수점이 추가될 위치
String result = power(a,b);
String re = result.substring(0, result.length()-idx) +"."+result.substring(result.length()-idx);
System.out.println(re);
}
static String power(String s1, int p) {
if(p == 1) //지수가 1이면
return s1;
String C = power(s1, p-1);
C = multiply(C, s1);
return C;
}
static String multiply(String s1, String s2) {
String C = "0";
for(int i = 0; i < s2.length(); i++) { //입력받은 소수
char[] line = s1.toCharArray(); //곱셈 값을 담을 배열
int carry = 0;
for(int j = s1.length()-1; j>=0; j--) {
int temp = carry;
carry = 0;
temp += (s1.charAt(j) - '0') * (s2.charAt(s2.length()-1-i) - '0'); //뒤에 있는 수부터 곱해준다
if(temp >= 10) {
carry = temp / 10; //다음 자리로 올라가는 수
temp %= 10; //남는 자리
}
line[j] = (char) (temp+'0'); //뒤 부터 채워준다
}
String D = new String(line); //배열을 합쳐주고
if(carry > 0) { //마지막 셈이 10을 초과해 자리수를 올려줘야 한다면
char tmp = (char) (carry+'0');
D = tmp + D;
}
for(int k = 0; k < i; k++) { //자리수만큼 뒤에 0을 붙여준다
D += "0";
}
C = add(C, D);
}
return C;
}
static String add(String s1, String s2) {
char[] chararr; //결과를 담을 배열
if(s1.length() > s2.length()) {
chararr = new char[s1.length()];
}
else {
chararr = new char[s2.length()];
}
int carry = 0;
for(int i = 0; i < chararr.length; i++) {
int temp = carry;
carry = 0;
if(i < s1.length()) {
temp += s1.charAt(s1.length() - 1 - i) - '0'; //뒤의 정수부터 더해준다
}
if(i < s2.length()) {
temp += s2.charAt(s2.length() - 1 - i) - '0';
}
if(temp >= 10) { //넘어가면
carry = 1; //다음 자리수에 더해줌
temp -= 10;
}
chararr[chararr.length - 1 - i] = (char) (temp + '0');
}
String re = new String(chararr);
if(carry == 1) {
re = "1" + re;
}
return re;
}
}
'백준' 카테고리의 다른 글
[백준] 1033 칵테일 [자바] (0) | 2022.03.03 |
---|---|
[백준] 9742 순열 [자바] (0) | 2022.03.02 |
[백준] 9251 LCS [자바] (0) | 2022.01.28 |
[백준] 15829 Hashing [자바] (0) | 2022.01.26 |
[백준] 3090 차이를 최소로 [자바] (0) | 2022.01.25 |