AcWing 794. 高精度除法-java
原题链接
简单
作者:
ice_73
,
2025-01-17 22:36:20
,
所有人可见
,
阅读 1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author iceewei
* @Description: 大整数算法 acwing-ycx
* @date 2025/1/17
*/
public class Main {
private static int r;
public static void main(String[] args) throws IOException {
String[] s = doInput();
ArrayList<Integer> A = new ArrayList<>();
ArrayList<Integer> B = new ArrayList<>();
for (int i = s[0].length() - 1; i >= 0 ; i--) A.add(s[0].charAt(i) - '0');
int b = 0;
for (int i = 0; i < s[1].length() ; i++) {
b = b*10 + s[1].charAt(i) - '0';
B.add(s[1].charAt(i) - '0');
}
r = 0;
ArrayList<Integer> C = div(A, b);
for (int i = 0; i < C.size(); i++) System.out.print(C.get(i));
System.out.println();
System.out.println(r);
}
// 还是把b当做一个整体,需要一个余数
private static ArrayList<Integer> div(ArrayList<Integer> A, int b) {
ArrayList<Integer> C = new ArrayList<>();
for (int i = A.size() - 1; i >= 0; i--) {
r = r*10 + A.get(i);
C.add(r/b);
r -= (r/b)*b;
}
int i = 0;
while (C.size() > 1 && C.get(i) == 0) C.remove(i);
return C;
}
private static String[] doInput() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer st = new StreamTokenizer(br);
st.ordinaryChars(0, 255);
st.wordChars(33, 255);
String[] s = new String[2];
int counter = 0;
while (st.nextToken()!= StreamTokenizer.TT_EOF) {
if (st.ttype == StreamTokenizer.TT_WORD) {
s[counter++] = st.sval;
}
}
return s;
}
}