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#include #include #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 好了完結!
感謝自己堅持!
本題來自ACM題解2