High-precision Computing-Large integer division

Source: Internet
Author: User
Tags integer division

Problem Description

A quotient that divides two large positive integers
input Data
Line 1th is the number of groups of test data N, each group of test data accounted for 2 rows, the 1th row is the divisor, and the 2nd line is the divisor.
There is a blank line between each set of test data, with no more than 100 characters per row of data
Output Requirements
n rows, each set of test data has a row of output is the corresponding integer quotient

Thinking of solving problems

The basic idea is to do the subtraction repeatedly, to see how many divisor can be subtracted from the divisor, quotient is how much. A one-off is obviously too slow, how to reduce it faster? Take 7546 divided by 23 For example: Starting quotient is 0. Minus 23 of 100 times times, is 2300, found enough to reduce 3 times, the remaining 646. The quotient value is increased by 300. Then 646 minus 230, found to reduce 2 times, the remaining 186, so the value of the quotient increased by 20. Finally, 186 minus 23, 8 times, so the final quotient is 328. So the core of the subject is to write a subtraction function of a large integer, and then repeatedly call the function to do subtraction

Code Implementation
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdbool.h>#define Max_lenintX[max_len + -], Y[max_len + -], Z[max_len + -];CharA[max_len + -], B[max_len + -];voidDebug_print (Const intX[],intLen) {intI for(i =0; i < Len; i++)printf("%d", X[i]);printf("\ n");}voidChar_to_int (Const CharS[],intX[]) {inti =0;intLen =strlen(s);memset(X,0, Max_len + -);intj =0; for(i = len-1; I >=0; i--) {x[j++] = s[i]-' 0 '; }}intCal_length (Const intX[]) {inti =0;intCount =0; for(i = Max_len-1; I >=0; i--) {if(X[i]) {count = i +1; Break; }    }returnCount;}intBig_int_sub (intX[],Const intY[]) {inti =0;Const intLenx = Cal_length (x);Const intLeny = Cal_length (y);//Determine if x is greater than Y    if(Lenx < Leny)return-1;Else if(Lenx = = Leny) { for(i = Lenx-1; I >=0; i--) {if(X[i] < y[i])return-1;Else if(X[i] = = Y[i])Continue;Else                 Break; }    }//bit-wise to reduce     for(i =0; i < Max_len; i++) {X[i] = X[i]-y[i];if(X[i] <0) {X[i] = X[i] +Ten; X[i +1] --; }    }return 1;}voidBig_int_div (intX[],Const intY[],intZ[]) {intI, j =0;int*copy_y = NULL;memset(Z,0, Max_len + -);Const intLenx = Cal_length (x);intLeny = Cal_length (y);inttimes = Lenx-leny; Copy_y = (int*)malloc(sizeof(int) * Max_len);memcpy(Copy_y, Y,sizeof(int) * Max_len);//Move the copied copy_y to the right times, equal to the length of x     for(i = Lenx-1; I >=0; i--) {if(I >= times) copy_y[i] = Copy_y[i-times];ElseCopy_y[i] =0;//Low fill 0}//x start to reduce copy_y, minus enough to reduce, and then subtract copy_y right shift times-1 bit until the end is not reducedLeny = Lenx; for(i =0; I <= times; i++) { while(Big_int_sub (x, copy_y) >=0) Z[times-i] + +;//copy_y left shift one is y right shift times-1 bit         for(j =1; J < Leny; J + +) Copy_y[j-1] = Copy_y[j]; Copy_y[--leny] =0; } for(i =0; i < Max_len; i++) {if(Z[i] >=Ten) {z[i +1] + = Z[i]/Ten; Z[i] = z[i]%Ten; }    }}voidBig_int_print (Const intZ[]) {BOOLFlag =0;inti =0; for(i = Max_len; I >=0; i--) {if(flag)printf("%d", Z[i]);Else if(Z[i]) {printf("%d", Z[i]); Flag =1; }    }if(!flag)printf("0");printf("\ n");}intMain () {intNscanf("%d", &n); while(n--) {scanf('%s ', a);scanf('%s ', b);intLen =0;intLenx =strlen(a);intLeny =strlen(b); len = Lenx-leny;if(Len <0)printf("0\n");        Char_to_int (A, x);        Char_to_int (b, y);        Big_int_div (x, y, z);    Big_int_print (z); }return 0;}

High-precision Computing-Large integer division

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.