The square of a large number is opened. The code was originally written using the manual square method found on the Internet. However, the double type of data access is required in the intermediate process. The result is TLE. After debugging, it turns out that the length of the double-type data access is not long enough. It can be saved to the power of 10 to 100, but it is still WA after it is handed in. After debugging for half a day, we found that, when the double-type number is about 18 digits, although the number can still be accessed, the precision of the number has changed. That is to say, the number is still that digit, but the actual number is no longer correct. Finally, the other person's method should be checked to write a large number.
WA code:
#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>char str[1010];void Sqrt(){ long double i,r,n; int j,l,len,num,x[1010]; len=strlen(str); if (len==1&&str[0]=='0') { printf("\n"); return; } if (len%2==1) { n=str[0]-48; l=-1; } else { n=(str[0]-48)*10+str[1]-48; l=0; } r=0,num=0; while (true) { i=0; while (i*(i+20*r)<=n) ++i; --i; n-=i*(i+20*r); r=r*10+i; x[num]=(int)i; ++num; l+=2; if (l>=len) break; n=n*100+(long double)(str[l]-48)*10+(long double)(str[l+1]-48); } for (j=0; j<num; ++j) printf("%d", x[j]); puts("");}int main(){#ifdef test freopen("sample.txt", "r", stdin);#endif int t; scanf("%d", &t); getchar(); while(t--) { scanf("%s", str); Sqrt(); if(t) puts(""); } return 0;}
AC code:
# Define test # include <stdio. h> # include <string. h> # include <stdlib. h> # define depth 10 typedef int bignum_t [1010]; int comp (const bignum_t A, const int C, const int D, const bignum_t B) // compare large numbers {int I, T = 0, O =-depth * 2; If (B [0]-A [0] <D & C) return 1; for (I = B [0]; i> D; I --) {T = T * depth + A [I-d] * C-B [I]; If (T> 0) return 1; if (T <O) return 0 ;}for (I = D; I --) {T = T * depth- B [I]; If (T> 0) return 1; if (T <O) return 0;} return T> 0;} void sub (bignum_t A, const bignum_t B, const int C, const int d) // large number minus {int I, O = B [0] + D; for (I = 1 + D; I <= O; I ++) if (A [I]-= B [I-d] * C) <0) A [I + 1] + = (a [I]-depth + 1)/depth, a [I]-= (a [I]-depth + 1) /depth * depth; For (; A [I] <0; A [I + 1] + = (a [I]-depth + 1)/depth, A [I]-= (a [I]-depth + 1)/depth * depth, I + +); (;! A [A [0] & A [0]> 1; A [0] --);} void SQRT (bignum_t B, bignum_t A) // square {int H, l, m, I; memset (void *) B, 0, sizeof (bignum_t )); for (I = B [0] = (a [0] + 1)> 1; I; sub (a, B, M, I-1 ), B [I] + = m, I --) for (H = depth-1, L = 0, B [I] = m = (H + L + 1)> 1; h> L; B [I] = m = (H + L + 1)> 1) if (COMP (B, M, I-1, )) H = m-1; else l = m; (;! B [B [0] & B [0]> 1; B [0] --); for (I = 1; I <= B [0]; B [I ++] >>> = 1) ;}char STR [1010]; bignum_t a, B; int main () {# ifdef test freopen ("sample.txt ", "r", stdin); # endif int t; scanf ("% d", & T); While (t --) {scanf ("% s", STR ); A [0] = strlen (STR); For (INT I = 1; I <= A [0]; I ++) A [I] = STR [A [0]-I]-'0'; SQRT (B, A); For (INT I = B [0]; I >= 1; I --) printf ("% d", B [I]); printf ("\ n"); If (t) puts ("");} return 0 ;}