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

谁能给我指出哪里出了错?

Posted by xiayuhai at 2009-04-18 12:47:41 on Problem 1001
#include <iostream>
#include <string>
using namespace std;

void trans(string &s,int &pos)
{//此函数把初始数据可以去掉的前后0以及.去掉,并把数字顺序倒置
	string t;
	int i,k,p=0,f=0;
	for (i=0;i<s.length();i++)if (s[i]=='.')f=1; 
	pos=0;
	k=s.length()-1;
	if (f==1)
	{
		while (s[k]=='0'){s.erase(k,1);k--;}
	//	if ((s.length()==1)&&(s[0]=='.'))s[0]='0';
		if (s[k]=='.')
		{	
			if (k==0)s[k]='0';
			else {s.erase(k,1);k--;}
		}else {
			while (s[p]!='.')p++;
			pos=k-p;
			for (;p<k;p++)s[p]=s[p+1];
			s.erase(k,1);
			k--;
		}
	}
	t.resize(s.length());
	for (p=0;p<s.length();p++)t[p]=s[s.length()-1-p];
	s=t;
	while ((s.length()>1)&&(s[s.length()-1]=='0')){s.erase(s.length()-1,1);}
}//trans

string mul(string a,string b)
{//此函数用来计算2个数的乘积
	string result;
	int i,t,j;
	result.resize(a.length()+b.length());
	for (i=0;i<result.length();i++)result[i]='0';
	for (i=0;i<b.length();i++)
	{
		for (j=0;j<a.length();j++)
		{	
			t=(b[i]-48)*(a[j]-48);
			result[i+j+1]=result[i+j+1]+(result[i+j]-48+t)/10;
			result[i+j]=48+(result[i+j]-48+t)%10;
		}
	}
	if (result[result.length()-1]==48) result.erase(result.length()-1,1);
	//cout<<result.length()<<endl<<result<<endl;
	return result;
}//mul

int main(){
	string s,result;
	int n,i,k,pos;
	while (cin>>s>>n)
	{
		trans(s,pos);
		result=s;
		for (i=1;i<n;i++)
			result=mul(result,s);
		k=result.length();	
		if (k>n*pos)
		{//小数位数小于上面得出乘积的位数的情况
			s.resize(k+1);
			for (i=0;i<k;i++) 
			{
				if (i<pos*n) s[k-i]=result[i];
				else s[k-i-1]=result[i];
			}
			if (pos>0)s[k-pos*n]='.';
			result=s;
		}else 
		{//小数位数不小于上面得出乘积的位数的情况
			s.resize(n*pos+1);
			s[0]='.';
			for (i=1;i<n*pos+1-k;i++)s[i]='0';
			for (i=n*pos+1-k;i<n*pos+1;i++)s[i]=result[n*pos-i];
			result=s;
		}
		k=result.length()-1;
		while (result[k]=='0'){result.erase(k,1);k--;}
		cout<<result<<endl;
		s="";
	}
}

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