描述

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=;
memset(D,,sizeof(D));
for(int i=len-; i>=; i--){
cnt=;
int num=d[i]-'';
next=num;
while(next!= || cnt<Dlen){
current=next*+D[cnt];
D[cnt++]=current/;
next=current%;
}
Dlen=cnt;
}
printf("%s [8] = 0.",d);
for(int i=; i<cnt; i++){
printf("%d",D[i]);
}
printf(" [10]\n");
}
return ;
}

最新文章

  1. web.config数据库连接字符串
  2. 微信小程序开发—快速掌握组件及API的方法---转载
  3. shell脚本入门及基本元素
  4. json 对c++类的序列化(自动生成代码)
  5. 重温WCF之流与文件传输(七)
  6. Java反射与动态代理
  7. Hbase之尝试使用错误列族获取数据
  8. 恢复Ext3下被删除的文件(转)
  9. javacv实战篇
  10. c语言随机数
  11. CMake高速入门
  12. 给线程发送消息让它执行不同的处理(自己建立消息循环,非常有意思) good
  13. Codeforces Beta Round #1 A,B,C
  14. iOS URL Schemes与漏洞的碰撞组合
  15. 3D数学基础(三)矩阵
  16. BeautifulSoup获取图片
  17. Palindromic Numbers LightOJ - 1205
  18. es的timeout机制
  19. SELinux介绍
  20. Skynet服务器框架(十) CentOS 防火墙设置

热门文章

  1. angular 第二种依赖注入
  2. string类------新标准c++程序设计
  3. C# 向TIM或者QQ自动发送中文消息【微信也是可用的】 附测试GIF
  4. linux下配置apache多站点访问-小案例
  5. 【python跨目录调用】结合自己遇到的问题到解决问题,作个记录
  6. c#优秀文章
  7. CentOS 7 安装中网络设置111
  8. nginx 之 proxy_pass
  9. Docker镜像的导出和载入
  10. 【算法笔记】B1022 D进制的A+B