本题要求计算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); } } }
1017. A除以B (20),布布扣,bubuko.com
原文地址:http://blog.csdn.net/jason_wang1989/article/details/25612231