大数运算-除法-Octal Fractions
测试地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=86
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.
题目大意: 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]=' '; printf("%s [8] = 0.%s [10]n",octal8,octal10); } return 0; } 好了完结! 感谢自己坚持! 本题来自ACM题解2 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |