Octal Fractions

                                                   Octal Fractions

                                Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 149  Solved: 98

Description

Fractions in octal (base 8) notation can be expressed exactly in decimal notation. For example, 0.75 in octal is 0.963125 (7/8 + 5/64) in decimal. All octal numbers of n digits to the right of the octal point can be expressed in no more than 3n decimal digits to the right of the decimal point.

Input

Write a program to convert octal numerals between 0 and 1, inclusive, into equivalent decimal numerals. The input to your program will consist of octal numbers, one per line,to be converted. Each input number has the form 0.d1d2d3 ... dk, where the di are octal digits (0..7). There is no limit on k.

Output

Your output will consist of a sequence of lines of the form 0.d1d2d3 ... dk [8] = 0.D1D2D3 ... Dm [10] where the left side is the input (in octal), and the right hand side the decimal (base 10) equivalent. There must be no trailing zeros, i.e. Dm is not equal to 0.

Sample Input

0.75
0.0001
0.01234567

Sample Output

0.75 [8] = 0.953125 [10]
0.0001 [8] = 0.000244140625 [10]
0.01234567 [8] = 0.020408093929290771484375 [10]

因为大数除法是顺的方便,加法是逆的方便,让我纠结了好久=_=,最后觉定都顺吧,就AC了
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 void divi(int p[],int x,int *len)
  5 {
  6     int temp=0,i,j;
  7     for(i=0;i<*len+3;i++)
  8     {
  9         temp=temp*10+p[i];
 10         p[i]=temp/x;
 11         temp%=x;
 12     }
 13 
 14     for(i=*len+3;i>=0;i--)
 15     {
 16         if(p[i])
 17         {
 18            *len=i+1;
 19            break;
 20         }
 21     }
 22 
 23  /*   for(i=0;i<*len;i++)
 24         printf("p[%d]=%d
",i,p[i]);
 25     printf("
");      */
 26 }
 27 
 28 void add(int p1[],int p2[],int *len1,int *len2)
 29 {
 30     int lst,i,j;
 31     int co_p1[200];
 32 
 33     memset(co_p1,0,sizeof(co_p1));
 34     lst=*len1>=*len2?*len1:*len2;
 35 
 36     for(i=0,j=lst-1;i<lst;i++,j--)
 37     {
 38         co_p1[i]+=p2[j]+p1[j];
 39         if(co_p1[i]>9)
 40         {
 41             co_p1[i]-=10;
 42             co_p1[i+1]++;
 43         }
 44     }
 45     *len2=lst;
 46     if(p1[i])
 47        *len2++;
 48 
 49    for(i=0,j=*len2-1;i<*len2;i++,j--)
 50         p1[i]=co_p1[j];
 51  /*   printf("和:");
 52     for(i=0;i<*len2;i++)
 53          printf("%d",p1[i]);
 54     printf("

");*/
 55 }
 56 
 57 int main()
 58 {
 59     //freopen("a.txt","r",stdin);
 60     int i,j,k;
 61     int len;       //放应保留的位数
 62     int len1,len2;  //len1为每个商的长度,len2
 63     int num_a[200];//放商
 64     int num_sum[200];//放商的和
 65     char str[201];
 66 
 67     while(gets(str)!=NULL)
 68     {
 69         printf("%s [8] = 0.",str);
 70         len=strlen(str);
 71         len=(len-2)*3;
 72         len2=len1=1;
 73 
 74         memset(num_sum,0,sizeof(num_sum));
 75 
 76         for(i=2;str[i]!='';i++)
 77         {
 78             len1=1;
 79             memset(num_a,0,sizeof(num_a));
 80             num_a[0]=str[i]-'0';
 81             if(!num_a[0])
 82             {
 83                 continue;
 84             }
 85             else
 86             {
 87                 for(j=0;j<i-1;j++)
 88                 {
 89                     divi(num_a,8,&len1);
 90                  /*   printf("商:");
 91                     for(k=0;k<len1;k++)
 92                         printf("%d",num_a);
 93                     printf("
");   */
 94                 }
 95               //  printf("
");
 96             }
 97 
 98             add(num_sum,num_a,&len1,&len2);
 99         }
100 
101         for(i=1;i<=len;i++)
102            printf("%d",num_sum[i]);
103 
104         printf(" [10]
");
105     }
106     return 0;
107 }
View Code
原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4174342.html