首页 > 其他 > 详细

Octal Fractions 未完成

时间:2014-12-19 18:39:47      阅读:273      评论:0      收藏:0      [点我收藏+]
技术分享
  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     for(i=0;i<*len;i++)
 23         printf("p[%d]=%d\n",i,p[i]);
 24     printf("\n");
 25 }
 26 
 27 void add(int p1[],int p2[],int *len1,int *len2)
 28 {
 29     int lst,i,j;
 30     int co_p1[200];
 31     lst=*len1>=*len2?*len1:*len2;
 32 
 33     for(i=0,j=lst-1;j>=0;i++,j--)
 34     {
 35         p1[i]=p2[j]+co_p1[j];
 36         if(p1[i]>9)
 37         {
 38             p1[i]-=10;
 39             p1[i+1]++;
 40         }
 41         printf("p1[%d]=%d\n",i,p1[i]);
 42     }
 43     *len2=lst;
 44     if(p1[i])
 45        *len2++;
 46 
 47     printf("和:");
 48     for(i=*len2-1;i>=0;i--)
 49          printf("%d",p1[i]);
 50     printf("\n\n");
 51 }
 52     
 53 int main()
 54 {
 55     freopen("a.txt","r",stdin);
 56     int i,j,k;
 57     int len;       //放应保留的位数
 58     int len1,len2;  //len1为每个商的长度,len2
 59     int num_a[200];//放商
 60     int num_sum[200];//放商的和
 61     char str[201];
 62     
 63     while(gets(str)!=NULL)
 64     {
 65         printf("%s [8] = 0.",str);
 66         len=strlen(str);
 67         len=(len-2)*3;
 68         len2=len1=1;
 69         
 70         memset(num_sum,0,sizeof(num_sum));
 71         
 72         for(i=2;str[i]!=\0;i++)
 73         {
 74             len1=1;
 75             memset(num_a,0,sizeof(num_a));
 76             num_a[0]=str[i]-0;
 77             if(!num_a[0])
 78             {
 79                 continue;
 80             }
 81             else
 82             {
 83                 for(j=0;j<i-1;j++)
 84                 {
 85                     divi(num_a,8,&len1);
 86                  /*   printf("商:");
 87                     for(k=0;k<len1;k++)
 88                         printf("%d",num_a);
 89                     printf("\n");   */
 90                 }
 91               //  printf("\n");
 92             }
 93 
 94             add(num_sum,num_a,&len1,&len2);
 95         }
 96 
 97         for(i=len-1;i>=0;i--)
 98             printf("%d",num_sum[i]);
 99         printf(" [10]\n");
100     }
101     return 0;
102 }
103             
104 
105         
106         
View Code

 

                                                   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]
技术分享
  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     for(i=0;i<*len;i++)
 23         printf("p[%d]=%d\n",i,p[i]);
 24     printf("\n");
 25 }
 26 
 27 void add(int p1[],int p2[],int *len1,int *len2)
 28 {
 29     int lst,i,j;
 30     int co_p1[200];
 31     lst=*len1>=*len2?*len1:*len2;
 32 
 33     for(i=0,j=lst-1;j>=0;i++,j--)
 34     {
 35         p1[i]=p2[j]+co_p1[j];
 36         if(p1[i]>9)
 37         {
 38             p1[i]-=10;
 39             p1[i+1]++;
 40         }
 41         printf("p1[%d]=%d\n",i,p1[i]);
 42     }
 43     *len2=lst;
 44     if(p1[i])
 45        *len2++;
 46 
 47     printf("和:");
 48     for(i=*len2-1;i>=0;i--)
 49          printf("%d",p1[i]);
 50     printf("\n\n");
 51 }
 52     
 53 int main()
 54 {
 55     freopen("a.txt","r",stdin);
 56     int i,j,k;
 57     int len;       //放应保留的位数
 58     int len1,len2;  //len1为每个商的长度,len2
 59     int num_a[200];//放商
 60     int num_sum[200];//放商的和
 61     char str[201];
 62     
 63     while(gets(str)!=NULL)
 64     {
 65         printf("%s [8] = 0.",str);
 66         len=strlen(str);
 67         len=(len-2)*3;
 68         len2=len1=1;
 69         
 70         memset(num_sum,0,sizeof(num_sum));
 71         
 72         for(i=2;str[i]!=\0;i++)
 73         {
 74             len1=1;
 75             memset(num_a,0,sizeof(num_a));
 76             num_a[0]=str[i]-0;
 77             if(!num_a[0])
 78             {
 79                 continue;
 80             }
 81             else
 82             {
 83                 for(j=0;j<i-1;j++)
 84                 {
 85                     divi(num_a,8,&len1);
 86                  /*   printf("商:");
 87                     for(k=0;k<len1;k++)
 88                         printf("%d",num_a);
 89                     printf("\n");   */
 90                 }
 91               //  printf("\n");
 92             }
 93 
 94             add(num_sum,num_a,&len1,&len2);
 95         }
 96 
 97         for(i=len-1;i>=0;i--)
 98             printf("%d",num_sum[i]);
 99         printf(" [10]\n");
100     }
101     return 0;
102 }
103             
104 
105         
106         
View Code

 

Octal Fractions 未完成

原文:http://www.cnblogs.com/get-an-AC-everyday/p/4174342.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!