码迷,mamicode.com
首页 > 其他好文 > 详细

大数开方

时间:2018-08-10 13:17:07      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:style   names   void   +=   out   size   stream   using   --   

#include <iostream>
using namespace std;
#include <stdlib.h>
#include <string.h>

void sqrt(char *str)
{
    double i,r,n;
    int j,l,size,num,x[1005];
    size=strlen(str);
    if (size==1&&str[0]==0)
    {
        cout<<0<<endl;
        return;
    }
    if (size%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>=size)
            break;
        n=n*100+(double)(str[l]-48)*10+(double)(str[l+1]-48);
    }
    for (j=0; j<num; ++j)
        cout<<x[j];
    cout<<endl;
    
}
int main()
{
    char ch[1005];
    while (cin>>ch)
        sqrt(ch);
    return 0;
}

 

大数开方

标签:style   names   void   +=   out   size   stream   using   --   

原文地址:https://www.cnblogs.com/xiao-xue-di/p/9454261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!