1017 A divided by B (C language)

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 formats:

Given in one row sequentially input A and B, separated by an intermediate space.

Output formats:

Sequentially outputs Q and R in a row, separated by an intermediate space.

Sample input:

123456789050987654321 7

Sample output:

17636684150141093474 3

Firstly, this question is so 1000 can not be calculated directly, we can be calculated in accordance with the division of human computing habits, this time should be read into the numbers using an array of characters to read more convenient.
After reading habits array according to human calculation, in addition to more than enough even number, and stores the result in an array of providers. Continues to calculate the remainder borrow.

Hang Summary: After compiling test point 1 all the way, however, after debugging found that if the dividend on one, but less than the dividend when it will draw the wrong answer, after modification, the output alone like this situation.

#include<stdio.h>
#include<string.h>
int main()
{
    char a[1000];
    int b[1000];
    int div;             //除数 
    int lo,i,j=0;        //lo为余数 
    int num=0;
    scanf("%s %d",a,&div);
    for(i=0; i<strlen(a); i++){
        num=num*10+(a[i]-'0');
        if( num<div && i == 0){
            continue;					//此处因为是被除数小于除数的情况直接用continue跳过 
        }								//所以后来检查发现当被除数就只有一位的时候会导致结果错误。 
        else{
            lo= num% div;
            b[j++]= num/ div;
            num= lo;
        }
    }
    if(i==1){
    	printf("0 %d",a[0]-'0');          //如果输入的是一位的话,第一个for循环会直接跳过去,
    									 //所以需要特殊处理,比如8/9的情况应该输出 0 8;
	}
    else {
    	for(i=0;i<j;i++){
       		printf("%d",b[i]);   //输出商
    	}
    	printf(" %d",lo);		//输出余数
	}
    return 0;
}
 
Released five original articles · won praise 0 · Views 58

Guess you like

Origin blog.csdn.net/Czrobe/article/details/104909579