PAT B 1017 A divided by B (C language), test point 012 can not pass

1017 A divided by B (20 minutes)

This problem requires calculation of A / B, where A is not more than 1000 bit positive integer, B is a positive integer. You need to output quotient Q and a remainder R, such that A = B × Q + R established.

Input format:
input sequence is given in row A and B, separated by an intermediate space.

Output format:
sequentially outputs Q and R in a row, separated by an intermediate space.

Sample input:

123456789050987654321 7

Sample output:

17636684150141093474 3

The title three pit
pit 1: 1000 even with a positive integer unsigned long long long is not enough, can only be solved with a string
Pit 2: When the business for more than the first and is 0 (test point 0 and 2), separate output (not the first place to the output is also 0)
pit 3: when a business is and is 0 (test point 1), normal output (output to 0) to

#include <stdio.h>
int main()
{
    char a[1001],c[1001];
    int b,n=1,yu=0;
    scanf("%s %d",&a,&b);
    c[0]=(a[0]-'0')/b+'0';
    yu=(a[0]-'0')%b;
    while(a[n]!='\0')
    {
        c[n]=(a[n]-'0'+yu*10)/b+'0';
        yu=(a[n]-'0'+yu*10)%b;
        n++;
    }
	c[n]='\0';
    if(c[0]=='0' && c[1]!='\0')  //注意商为0或商首位为0的情况
        for(int i=1;i<n;i++)
            printf("%c",c[i]);
    else    
        printf("%s",c);
    printf(" %d",yu);
    return 0;
}

/*
本题三个坑
坑1:1000位的正整数即使使用unsigned long long 也不够长,只能用字符串解决
坑2:当商为多位并且第一位为0时(测试点0和2),分开输出(不能把首位的0也输出来了)
坑3:当商为一位且为0时(测试点1),正常输出(把0输出)即可
*/
Published 15 original articles · won praise 0 · Views 283

Guess you like

Origin blog.csdn.net/weixin_44562957/article/details/104115736