A除以B

链接:http://www.nowcoder.com/pat/6/problem/4043

题目描述

本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数。你需要输出商数Q和余数R,使得A = B * Q + R成立。

 

输入描述:

输入在1行中依次给出A和B,中间以1空格分隔。



输出描述:

在1行中依次输出Q和R,中间以1空格分隔。

 

输入例子:

123456789050987654321 7

 

输出例子:

17636684150141093474 3

 1 #include "iostream"
 2 #include <iomanip>
 3 #include <string.h>
 4 #include <string>
 5 #include <vector>
 6 #include <cmath>
 7 #include <cctype>
 8 #include <algorithm>
 9 using namespace std;
10 
11 int d, r, i;
12 int num1[1000];
13 int num2[1000];
14 char str[1000];
15 int main()
16 {    
17     cin >>str >>d;
18     for(i=0; i<strlen(str); ++i)
19     {
20         num1[i] = str[i]-'0';
21     }
22     i=1;
23     int k=0, tmp=num1[0];
24     while(i < strlen(str))
25     {
26         tmp = tmp*10+num1[i++];
27         if(tmp > d) break;
28     }
29     while(i <= strlen(str))
30     {
31         num2[k++] = tmp/d;
32         tmp %= d;
33         tmp = tmp*10+num1[i++];
34     }
35     r = tmp/10;
36     for(int i=0; i<k; ++i)
37     {
38         cout <<num2[i];
39     }
40     cout <<" " <<r <<endl;
41     return 0;
42 }

 

posted @ 2015-07-04 08:40  皮蛋儿丶  阅读(195)  评论(0编辑  收藏  举报