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

小数位数(未完)

时间:2019-03-19 16:57:21      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:bsp   namespace   部分   queue   不为   ret   style   using   int   

#include<cstdio>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector> 
#include<string>
#include<queue>
#include<map>
#include<stack>
using namespace std; 
string ans,a,b;
ll atoi(string s)
{
    int ans=0,t=1;
    for(int i=s.length()-1;i>=0;i--)
    {
        ans += (s[i]-0)*t;
        t *= 10;
    }
    return ans;
}
string itoa(long long num)
{
    if(num == 0)    return "0";
    string ans;
    int h =0;
    while(num!=0)
    {
        int a = num%10;
        char ch = a+0;
        ans[h] = ch;
        h++;
        num /= 10;
    }
    for(int i=0,j=ans.length()-1;i<j;i++,j--)
    {
        int t;
        ans[i] = t;
        ans[i] = ans[j];
        ans[j] = t;
    }
    return ans;
}
string div(string a,string b,int n)        //a,b不为0 
{
    string ta,ts = "",t="0000000000000000000";
    int pos=0,al = a.length(),bl = b.length();
    long long an,bn = atoi(b);
    int ct=0; 
    while(1)
    {
        if(pos + bl - ts.length()<= al)            //找出每次进行除法的两个数an,bn 
        {
            ta = ts + a.substr(pos,bl-ts.length());
            an = atoi(ta);
            pos += bl-ts.length();
            if(an < bn)
            {
                if(pos == a.length())        //小数部分 
                {
                    an = an * 10;        //补0 
                }
                else
                {
                    an = an*10 + (a[pos]-0);
                    pos++;
                } 
            }
            long long shang,yu;
            yu = an % bn;
            shang = an / bn; 
            if(pos == a.length() && yu==0)
            {
                pos++;
            }
            
            if(yu == 0)        ts="";
            else            ts = itoa(yu);
            
            
        }
        else
        {
            ta = ts + t.substr(0,bl - ts.length());        //a的整数部分用完了,补0 
            an = atoi(ta);
            f(an < bn)
            {
                an = an * 10;        
            }
            long long shang,yu;
            yu = an % bn;
            shang = an / bn; 
            
            if(yu == 0)        break;
            else            ts = itoa(yu);
        }
        

        
    }
}
int main()
{
    int n;
    cin>>a>>b>>n;
    cout<<div(a,b,n);
    return 0;
}

 

小数位数(未完)

标签:bsp   namespace   部分   queue   不为   ret   style   using   int   

原文地址:https://www.cnblogs.com/fzuhyj/p/10559280.html

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