Octal Fractions 未完成

  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         

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         

时间: 2024-10-10 22:57:26

Octal Fractions 未完成的相关文章

POJ Octal Fractions(JAVA水过)

题目链接:CLICK HERE~ 虽然java一下模拟水过,但是我看到别人的一段神奇代码,贴出和大家共享. import java.math.*; import java.util.*; class Main{ public static void main(String args[]){ Scanner cin = new Scanner(System.in); BigDecimal Eight = new BigDecimal(8); while(cin.hasNext()){ String

POJ 1131 Octal Fractions (Java大数,八进制转十进制)

Octal Fractions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6959   Accepted: 3825 Description Fractions in octal (base 8) notation can be expressed exactly in decimal notation. For example, 0.75 in octal is 0.953125 (7/8 + 5/64) in d

Octal Fractions java秒 C++

Octal Fractions 题目抽象:   将八进制小数转换成十进制小树.小数的为数很大. 可以用java  中的BigDeciaml 秒掉.  time:297ms 1 import java.math.*; 2 import java.util.*; 3 import java.io.*; 4 import java.text.*; 5 6 public class Main 7 { 8 static int MS=3005; 9 public static void main(Stri

Poj1131-Octal Fractions

Octal Fractions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6669   Accepted: 3641 Description Fractions in octal (base 8) notation can be expressed exactly in decimal notation. For example, 0.75 in octal is 0.953125 (7/8 + 5/64) in d

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

expect 实现交互(未完成)

expect介绍 expect命令是一个实现交互功能的软件套件,是基于TCL的脚本编程语言,在企业运维中,系统会以交互的形式要求运维人员输入指定的字符串,之后才能继续执行命令.例如:为用户设置密码时,一般情况下需要手工输入两次密码,比如使用ssh连接远程服务器时,第一次连和系统实现两次交互. 简单的说,expect用来自动实现与交互程序通信的,无需管理员手工干预 spawn启动指定进程>expect获取期待的关键字>send向指定进程发送字符>进程执行完毕,退出 expect  表达式 

USACO 2.1 Ordered Fractions

Ordered Fractions Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less than or equal to N. Here is the set when N = 5: 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1 Write a program that, given an integer N between