PAT_B_1017 A除以B (20 分)【测试点3无法通过】

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

输入格式:

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

输出格式:

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

输入样例:

123456789050987654321 7

输出样例:

17636684150141093474 3

#include <iostream>

#include <string.h>

using namespace std;

int main()

{

    char A[1001];

    int B;

    cin>>A>>B;

    int length=strlen(A);

    char Q[length];

    int count=0;

    int tempa=0,R=0,isfirst=1;

    for(int i=0;i<length;i++)

    {

        tempa=R*10+(A[i]-'0');

        R=tempa%B;

        Q[count++]='0'+tempa/B;

    }

    for(int i=0;i<count;i++)

    {

        if(count<2)

        {

            cout<<"0";                   //如果没有这条, 1/7则没有商输出。此为博主出错缘由

        }

        if(isfirst&&Q[i]=='0') continue;

        cout<<Q[i];

        isfirst=0;

    }

    cout<<" "<<R<<endl;

    return 0;

}

猜你喜欢

转载自blog.csdn.net/id33749110/article/details/85707991