UVa 10023 - Square root

来源:互联网 发布:aes加密算法c语言实现 编辑:程序博客网 时间:2024/04/29 23:23

大数开平方,本来用从网上查的手动开平方的方法写的代码,但中间过程需要用到double型的量存取,结果TLE,调试了一下,原来是double型的量存取长度不够,换成long double型的量,可以存到10的100次方了,但交上以后依然WA,又调试了半天才发现,当double型量存取数字到18位左右以后,虽然依然可以存取数字,但数字的精度已经会有改变,也就是说位数还是那个位数,但实际数字已经不对了。最后还是查的其他人的方法,果断还是得写大数。

WA代码:

#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代码:

#define test#include<stdio.h>#include<string.h>#include<stdlib.h>#define DEPTH 10typedef int bignum_t[1010];int comp(const bignum_t a, const int c, const int d, const bignum_t b) //大数比较{    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; 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) //大数减{    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++)        ;    for (; !a[a[0]] && a[0] > 1; a[0]--)        ;}void Sqrt(bignum_t b, bignum_t a) //开平方{    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, a))                h = m - 1;            else                l = m;    for (; !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;}


原创粉丝点击