Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:该程序本质上的计算过程是这样的

Posted by yy17yy at 2010-11-04 10:36:24 on Problem 1131
In Reply To:该程序本质上的计算过程是这样的 Posted by:aoxboxcox at 2009-06-22 12:55:11
> 还以0.123为例
> 初始时:       0[8]=0[10]
> 两边加上3[8]: 3[8]=3[10]
> 两边除以8[10]:0.3[8]=0.375[10]
> 两边加上2[8]: 2.3[8]=2.375[10]
> 两边除以8[10]:0.23[8]=0.296875[10]
> 两边加上1[8]: 1.23[8]=1.296875[10]
> 两边除以8[10]:0.123[8]=0.162109375[10]
> 
> myDivide里做的事情就是对a[]除以8。
> 主函数里对a[0]的赋值相当于在个位数上做加法。
> 算法巧妙地利用了个位数上8进制数等于10进制数,匠心独具,赞一个。
感谢啊!!
我的代码:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
	char a[300];
	int b[300];
	while(cin>>a)
	{
		int i;
		if(a[2]=='\0')
		{
			cout<<"0. [8] = 0. [10]"<<endl;
			continue;
		}
		int lena=strlen(a);
		int lenb=1;
		memset(b,0,sizeof(b));
		for(i=lena-1;i>=2;i--)
		{
			b[0]=a[i]-'0';
			int j;
			for(j=lenb-1;j>=0;j--)
			{
				int temp=b[j]*1000/8;
				b[j]=temp/1000;
				temp=temp-b[j]*1000;
				b[j+1]+=temp/100;
				b[j+2]+=temp%100/10;
				b[j+3]+=temp%10;
			}
			lenb+=3;
		}
		int add=0;
		for(i=lenb-1;i>=0;i--)
		{
			b[i]+=add;
			add=b[i]/10;
			b[i]%=10;
		}
		b[lenb]='\0';
		for(i=lenb-1;b[i]==0;i--)
			lenb--;
		cout<<a<<" [8] = 0.";
		for(i=1;i<lenb;i++)
			cout<<b[i];
		cout<<" [10]"<<endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator