1017. A除以B (20)

来源:互联网 发布:埃及金字塔谁建的知乎 编辑:程序博客网 时间:2024/04/29 18:15

如题:http://www.patest.cn/contests/pat-b-practise/1017

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

输入格式:

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

输出格式:

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

输入样例:

123456789050987654321 7

输出样例:

17636684150141093474 3

 

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;


int main()
{
 string a;
 int b;
 cin>>a>>b;
 int m=0;
 int d=0;
 int i;
 int first=0;
 for(i=0;i<a.length();i++)
 {
  d=d*10+a[i]-'0';
  if(d>=b)
  {
   first=1;
   cout<<d/b;
  }
  else if(first)
   cout<<0;
  d=d%b;
 }
 if(!first)
  cout<<0;
 printf(" %d\n",d);
 return 0;
}

 

0 0