標籤:
連結: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 }
A除以B