首页 > ACM题库 > HDU-杭电 > Hdu 1376 Octal Fractions-高精度[解题报告] C++
2013
12-09

Hdu 1376 Octal Fractions-高精度[解题报告] C++

Octal Fractions

问题描述 :

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.

样例输入:

0.75
0.0001
0.01234567

样例输出:

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


注意相乘的求余等的顺序

#include<iostream>
#include<algorithm>

using namespace std;

//freopen("C://i.txt","r",stdin);

#define N 1000001

int n;
char s[N];
int go[N];
int ans[N];
int main()
{
	freopen("C://i.txt","r",stdin);
	int i,j,k;
	while (cin>>s)
	{
		n=strlen(s);
		k=0;
		for (i=0;i<n;i++) if (s[i]=='.')
			break;
		
		for (i++;i<n;i++)
		{
			go[k++]=s[i]-'0';
		}
		for (i=0;i<k;i++)
		{
			ans[i]=go[i]*125;
		
		}
	

		for (i=k-1;i;i--)
		{
		
			for (j=i;j<k;j++)
			{
				ans[j]*=125;
				ans[j-1]+=ans[j]/1000;
				ans[j]%=1000;
			}
			//cout<<ans[0]<<' '<<ans[1]<<' '<<ans[2]<<endl;
		}
		
	
		while (ans[k-1]==0)
			k--;
		printf("%s [8] = 0.",s);
		
		for (i=0;i<k;i++)
		{
			if (ans[i]<10)
			{
				printf("00");
			}
			else if (ans[i]<100) 
			{
				printf("0");
			}
			printf("%d",ans[i]);
		}
		
		printf(" [10]\n");
	}

}

 转自:http://blog.csdn.net/jiangjiashi/article/details/7802966


  1. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。

  2. [email protected]

  3. a是根先忽略掉,递归子树。剩下前缀bejkcfghid和后缀jkebfghicd,分拆的原则的是每个子树前缀和后缀的节点个数是一样的,根节点出现在前缀的第一个,后缀的最后一个。根节点b出现后缀的第四个位置,则第一部分为四个节点,前缀bejk,后缀jkeb,剩下的c出现在后缀的倒数第2个,就划分为cfghi和 fghic,第3部分就为c、c