zoukankan      html  css  js  c++  java
  • TOJ 2861 Octal Fractions

    描述

    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 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.

    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.

    输出

    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.

    样例输入

    0.75
    0.0001
    0.01234567

    样例输出

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

    题目来源

    Southern African 2001

    题目意思就是把小数位的八进制转换为十进制。

    【转换方法】

    例如:0.01234567

    则是0/(8)+1/(8*8)+2/(8*8*8)+3/(8*8*8*8)+...+7/(8*8*8*8*8*8*8*8)=0.020408093929290771484375

    因为要转换的数字挺大的所以要用数组来存放小数位,另外计算的公式也转换为:((((7/8+6)/8+5)/8+4)/8+...+0)/8

    #include <stdio.h>
    #include <string.h>
    #define MAXN 10000
    char d[MAXN];
    int D[MAXN];
    int main()
    {
        int cnt;//表示当前数组存放的位置 
        int next;
        int current;
        while(gets(d)!=NULL){
            int len=strlen(d);
            int Dlen=0;        
            memset(D,0,sizeof(D));
            for(int i=len-1; i>=2; i--){
                cnt=0;
                int num=d[i]-'0';
                next=num;
                while(next!=0 || cnt<Dlen){
                    current=next*10+D[cnt];
                    D[cnt++]=current/8;
                    next=current%8;
                }
                Dlen=cnt;    
            }
            printf("%s [8] = 0.",d);
            for(int i=0; i<cnt; i++){
                printf("%d",D[i]);
            }
            printf(" [10]
    ");
        }
        return 0;
    }
  • 相关阅读:
    Python环境的导入导出
    Jenkins+Ant+Jmeter搭建轻量级接口自动化(转载)
    VMware桥接模式连接局域网和互联网
    安装KVM
    [Beyond Compare] 排除/忽略 .svn 文件夹
    Git 如何放弃所有本地修改
    Python2 和 Python3 共存于 Centos7
    kubernetes 之部署metrics-server
    在CentOS 7.6 以 kubeadm 安装 Kubernetes 1.15 最佳实践
    华为云 Kubernetes 管理员实训 五 课后作业
  • 原文地址:https://www.cnblogs.com/chenjianxiang/p/3544585.html
Copyright © 2011-2022 走看看