TOJ 2861 ZOJ 1086 octal fractions/large number high precision

Source: Internet
Author: User
octal Fractions time limit (normal/java): 1000ms/3000ms Run memory limit: 65536KByte

Description

Fractions in octal (base 8) notation can is expressed exactly in decimal notation. For example, 0.75 in octal are 0.953125 (7/8 + 5/64) in decimal. All octal numbers of n digits to the right of the octal point can is expressed in no further than 3n decimal digits to the RI Ght of the decimal point.

Write a program to convert octal numerals between 0 and 1, inclusive, into equivalent decimal numerals.

input

The input to your program would consist of octal numbers, one per line, to be converted. Each of the input number has the form 0.d1d2d3. DK, where the di is octal digits (0..7). There is no limit on K.

Output

Your output would consist of a sequence of lines of the form

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

The where the left side are the input (in octal), and the right hand side the decimal (base) equivalent. There must be no trailing zeros, i.e. Dm are not equal to 0.

Sample Input

0.75
0.0001
0.01234567

Sample Output

0.75 [8] = 0.953125 [ten]
0.0001 [8] = 0.000244140625 [0.01234567]
[8] = 0.020408093929290771484375 [10]

What can be done 0.75 = (5/8+7)/8

0.01234567 = (((7/8+6)/8) +5)/8 ....

Simulate high-precision on the line

Code High Captain offers

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
string S;
Char res[30000];
int Reslen;
void JS ()
{
	int i,j,k,num=0;
	for (i=0;; i++)
	{
		if (res[i]!=0)
			num = num * + res[i]-' 0 ';
		else 
			num = num * TEN;
		res[i]=num/8+ ' 0 ';
		num=num%8;
		if (res[i+1]==0&&num==0) break;
	}
}
int main ()
{
	while (CIN >> s)
	{
		reverse (s.begin (), S.end ());
		memset (res,0,sizeof (res));
		int i,j,k;
		for (i=0;s[i]!= '. '; i++)
		{
			res[0]=s[i];
			JS ();
		}
		Reverse (S.begin (), S.end ());
		cout << S << "[8] = 0." << res + 1 << "[Ten]" <<endl;
	}
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.