1017. A除以B (20)
版权声明: https://blog.csdn.net/oFengWuYu1/article/details/25612231
本题要求计算A/B。当中A是不超过1000位的正整数,B是1位正整数。你须要输出商数Q和余数R,使得A = B * Q + R成立。
输入格式:
输入在1行中依次给出A和B。中间以1空格分隔。
输出格式:
在1行中依次输出Q和R,中间以1空格分隔。
输入例子:123456789050987654321 7输出例子:
17636684150141093474 3
import java.util.Scanner;
/**
* @author jwang1 Success Factors
*/
public class Main {
public static void main(String[] args) {
String a;
StringBuffer q_res = new StringBuffer();
int b, r, q;
Scanner cin = new Scanner(System.in);
a = cin.next();
b = cin.nextInt();
int len = a.length();
int curr = a.charAt(0) - '0';
if (curr >= b) {
q_res.append(curr / b);
}
for (int i = 1; i < len; i++) {
q = curr % b;
curr = q * 10 + a.charAt(i) - '0';
q_res.append(curr / b);
}
q = curr % b;
r = q;
if (len == 1 && a.charAt(0) - '0' < b) {
System.out.print("0 " + (a.charAt(0) - '0'));
} else {
System.out.print(q_res + " " + r);
}
}
}
//用JAVA做比較简单
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigInteger a = cin.nextBigInteger();
BigInteger b = cin.nextBigInteger();
BigInteger q = a.divide(b);
BigInteger r = a.mod(b);
System.out.println(q+" "+r);
}
}