Clip Address: http://hzwer.com/3023.html
Calculate the greatest common divisor of two super-large numbers:
Examples:
111111111111111111111
22222222222222222222222222222
Output:
I don't know, either.
Code
#include <iostream> #include <cstdio> #include <cstring> #define INF 1000000000using namespace std; Char ch1[10005],ch2[10005];int la,lb,cnt;struct data{int a[1205],l;} A,b;bool COM () {if (A.L<B.L) return 0;if (A.L>B.L) return 1;for (int i=a.l;i>0;--i) if (A.a[i]>b.a[i]) return 1; else if (A.a[i]<b.a[i]) return 0;return 1;} void print (Data a) {while (a.a[a.l]==0) a.l--;for (int i=a.l;i>0;--i) if (I==A.L) printf ("%d", a.a[i]); else printf ("%09d", A.a[i]);} Inline Data sub (data A,data b) {int k; Data c;for (int i=1;i<=1200;++i) {if (I<=B.L) c.a[i]=a.a[i]-b.a[i]; else if (I<=A.L) c.a[i]=a.a[i]; else c.a[i]=0; if (c.a[i]<0) {c.a[i]+=inf; a.a[i+1]--; }} C.L=A.L; while (C.A[C.L]==0&&C.L) c.l--; return c;} void Diva () {for (int i=1;i<=a.l;i++) {if (a.a[i]&1) A.A[I-1]+=INF/2; A.a[i]>>=1;} if (!A.A[A.L]) a.l--;} void Divb () {for (int i=1;i<=b.l;i++) {if (b.a[i]&1) B.A[I-1]+=INF/2; B.a[i]>>=1;}if (!B.A[B.L]) b.l--;} void Mul () {for (int i=a.l;i>0;i--) {a.a[i]<<=1; A.a[i+1]+=a.a[i]/inf; A.a[i]%=inf; } while (a.a[a.l]>0) a.l++; for (int i=b.l;i>0;i--) {b.a[i]<<=1; B.a[i+1]+=b.a[i]/inf; B.a[i]%=inf; } while (b.a[b.l]>0) b.l++;} int main () {scanf ("%s%s", ch1+1,ch2+1), La=strlen (ch1+1); Lb=strlen (ch2+1); if (la%9) A.l=la/9+1;else a.l=la/9;if (lb%9) B.l=lb/9+1;else b.l=lb/9;for (int i=1;i<=a.l;++i) {int K1=max (1,la-i*9+1), k2=la-(i-1) *9;for (int j=k1;j<=k2;++j ) a.a[i]=a.a[i]*10+ch1[j]-' 0 ';} for (int i=1;i<=b.l;++i) {int K1=max (1,lb-i*9+1), k2=lb-(i-1) *9;for (int j=k1;j<=k2;++j) B.A[I]=B.A[I]*10+CH2[J] -' 0 ';} while (1) {if ((a.a[1]%2==0) && (b.a[1]%2==0)) {diva ();d IVB (); cnt++;} else if ((a.a[1]%2==0)) diva (); else if ((b.a[1]%2==0)) DIVB (); if (COM ()) {a=sub (A, A,), if (!A.L) {while (cnt--) mul ();p rint (b); else {b=sub (b,a), if (!B.L) {while (cnt--) mul ();p rint (a); }return 0;}
2. Calculating the greatest common divisor of a large number and an int type number
#include <stdio.h> #include <string.h> #include <stdlib.h>//calculates a greatest common divisor template int gcd2 (int a) with a large number and an int type integer. int b) { return b==0?a:gcd2 (b, a%b);} int gcd (int *a, int len, int b) { int yu;//save large number to B after remainder of int i, jin=0;//array is stored in 0--(len-1) int cur; for (i=0; i<len; i++) { cur= (a[i]+jin*10)%b; jin=cur; } if (jin==0) {//description can be divisible then greatest common divisor is B return b; You can also calculate the output quotient } else{//Recalculate (large number a%b) with B's greatest common divisor return Gcd2 (Jin, b); }} int main () { char a[1001]; int b; int aa[1001]; while (scanf ("%s%d", A, &b)!=eof) { //converts the string to a numeric string int Len=strlen (a); for (int i=0; i<len; i++) { aa[i]=a[i]-48; } printf ("%d\n", gcd (AA, Len, b)); } return 0;}
Calculates the greatest common divisor "two numeric string" for two large numbers