Document directory
- Sample Input
- Sample output
Result |
Time Limit |
Memory limit |
Run times |
AC times |
Judge |
|
3 S |
8192 K |
3686 |
828 |
Standard |
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, intrusive, 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.750.00010.01234567
Sample output
0.75 [8] = 0.953125 [10]0.0001 [8] = 0.000244140625 [10]0.01234567 [8] = 0.020408093929290771484375 [10]
/*
Kong Niu uses the qinjiu algorithm. The algorithm I paste here uses the number of kilobytes as the intermediate medium to convert octal to decimal. The time is the same, in fact, the essence of algorithms is the same (early code is messy );
*/
# Include <iostream>
# Include <stdio. h>
# Include <memory>
# Include <string. h>
Using namespace STD;
Const int maxn = 2000;
Double Oct [8] = {0,125,250,375,500,625,750,875 };
Int main ()
{
Char STR [maxn];
Int oC [maxn], Dec [3 * maxn];
While (memset (STR, '/0', sizeof (STR), CIN> Str)
{
Int I, Len = strlen (STR), J;
For (I = 0, j = len-1; STR [J]! = '.'; I ++, j --)
OC [I] = STR [J]-48;
Cout <STR <"[8] = 0 .";
For (I = 0; I <len-2; I ++)
For (j = 0; j <len-2-i; j ++)
{
OC [J] = OC [J] * 125;
If (oc [J]> 999)
{
OC [J + 1] + = OC [J]/1000;
OC [J] = OC [J] % 1000;
}
} // Kilobytes (Save the original number x 125 to the new array)
For (I = len-3; I> 0; I --)
{
If (oc [I]> 99)
Cout <oC [I];
Else if (oc [I] <100 & oC [I]> 9)
Cout <"0" <oC [I];
Else if (oc [I] <10)
Cout <"00" <oC [I];
} // Three-bit one output, leading zero is not enough;
If (oc [0] % 10) cout <oC [0];
Else if (oc [0] % 100) cout <oC [0]/10;
Else if (! (Oc [0] % 100) cout <oC [0]/100;
// Clear the suffix with zero
Cout <"[10]/n ";
}
Return 0;
}