PKU ACM 1131 Octal Fractions http://acm.pku.edu.cn/JudgeOnline/problem?id=1131

此题不用到高精度!跟踪double型数在计算机中的存储就很容易得到解题的方法!代码很简单:
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
char str[65535];
    while (scanf("%s",&str)!=EOF)
{
   double sum=0;
   int n=strlen(str);
   for (int i=2;i<n;i++)
   {
    int temp=str[i]-'0';
    sum+=(double)temp/pow(8.00000,i-1);
   }
     n=(n-2)*3;
   cout<<str<<" [8] "<<"= ";
   printf("0.");
   for (int j=0;j<n;j++)
   {
    int m=sum*10;
    printf ("%d",m);
    sum=sum*10-m;
   }
   printf("%s\n"," [10]");
}
return 0;
}

posted on 2011-05-06 19:16  _Clarence  阅读(176)  评论(0编辑  收藏  举报

导航