您的位置:首页 > 其它

大数运算-除法-Octal Fractions

2014-04-16 21:11 176 查看
测试地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=86

Octal Fractions

Time Limit: 2 Seconds
Memory Limit: 65536 KB

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.

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.

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]

本人英文白痴题目看懂请略过。翻译只是为了 不那么白痴。
题目大意:

8进制的小数能用10进制的数很好表示。例如:0.75在十进制中是0.963125 (7/8 + 5/64) 。八进制小数点右边的n位数可以表示成小数位数不多于3n位的十进制数。

写个程序 去转换0-1之间的八进制数为十进制的数。每一行输入一个八进制数,去转换。 每一个数字

0.d1d2d3 ... dk每个di为(0-7)。k没有限制。你输出一个与这个八进制相等的十进制。

0.d1d2d3 ... dk [8] = 0.D1D2D3 ... Dm [10]

输出左边为八进制,右边为10进制。没有尾部的零。

好了。。终于结束了。弱智翻译请见谅。

这个题运用了一个数学技巧就是 7*(8的付一次方)+5*(8的负二次方)---》((5/8)+7)/8)这样来求。

#include<iostream>
#include<stdio.h>
#include<string.h>
#define MaxN 100
using namespace std;

int main()
{
char octal8[MaxN];//用于存放输入的八进制数
while(scanf("%s",octal8)!=EOF)
{
char octal10[MaxN]={'0'};//用于存放要输出的10进制数
int index =0;//表示十进制中的数到了第几位了。
int j;
for(int i=strlen(octal8)-1;i>1;i--)//从八进制中的最后一位开始做除法
{
int num = octal8[i]-'0';//把字符转换成数字,表示余数

for( j=0;(j<index||num);j++)//如果没有到10进制的最后一位或者余数不为0就继续运行;
{
int temp = num*10+(j<index?octal10[j]-'0':0);
octal10[j]=temp/8+'0';
num=temp%8;
}
index=j;//更新最长位
}
octal10[j]='\0';
printf("%s [8] = 0.%s [10]\n",octal8,octal10);
}
return 0;
}


好了完结!

感谢自己坚持!

本题来自ACM题解2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: