求A/B高精度值

来源:互联网 发布:赛亚人网络 编辑:程序博客网 时间:2024/05/29 13:34

求A/B高精度值

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 38   Accepted Submission(s) : 9

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

计算A/B的精确值,设A、B是以一般整数输入,计算结果精确到小数点后20位(若不足20位,末尾不用补0)

Input

输入有多组数据,每组数据为两个数A和B

Output

对于每组数据输出A/B的精确值

Sample Input

24 330 6

Sample Output

4/3=1.3333333333333333333330/6=5.0

 #include <iostream>
 #include <string.h>
 #include <stdio.h>
 using namespace std;
 int main ()
 { int t,a,b,c[30],x,i,j;
   cin>>t;
   while(t--)
   {  memset(c,0,sizeof(c));
    cin>>a>>b;
    c[0]=a/b;
       if(a%b==0)
        {cout<<a<<"/"<<b<<"="<<c[0]<<".0"<<endl;continue;}
        cout<<a<<"/"<<b<<"="<<c[0]<<".";
      x=a%b;
      for(i=1;i<21;)
      {
          x=x*10;
          c[i++]=x/b;
          x=x%b;
      }
      for(i=20;i>0;i--)
        if(c[i]!=0)
        break;
      for(j=1;j<=i;j++)
        cout<<c[j];
      cout<<endl;
   }
     return 0;
 }

原创粉丝点击