题目链接:1017 A除以B
分析
读取输入的A和B后,保存为字符串。模拟除法运算过程。不要用BigInteger,因为会超时。另外字符串经常要扩展(例如:append())的话,不要用String(非常慢),用StringBuilder是上上策。最后要注意,如果结果的第一位是0,并且只有一位,那么输出0,否则输出去掉首位后的结果。
import java
.io
.BufferedReader
;
import java
.io
.IOException
;
import java
.io
.InputStreamReader
;
public class Main {
public static void main(String
[] args
) throws IOException
{
BufferedReader br
= new BufferedReader(new InputStreamReader(System
.in
));
String
[] ab
= br
.readLine().split(" ");
br
.close();
int b
= Integer
.parseInt(ab
[1]);
int temp
= 0;
StringBuilder sb
= new StringBuilder();
for (int i
= 0; i
< ab
[0].length(); i
++) {
temp
= temp
* 10 + ab
[0].charAt(i
) - '0';
sb
.append((char) (temp
/ b
+ '0'));
temp
= temp
% b
;
}
String result
= sb
.toString();
if (result
.charAt(0) == '0' && result
.length() != 1) {
System
.out
.print(result
.substring(1) + " " + temp
);
} else {
System
.out
.print(result
+ " " + temp
);
}
}
}
转载请注明原文地址: https://yun.8miu.com/read-1307.html