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

请教,WA(很清楚)

Posted by bnuwuwenbin at 2008-04-06 21:52:32 on Problem 1001
#include<iostream>
#include<string>
using namespace std;

string clear(string s)
{
	if(s=="")
	s="0";
	
	while(s.length()>0 && s[0]=='0')
		s.erase(0,1);
	
	if(s=="")
		s="0";

	return s;
}

string subclear(string s)
{
	if(s=="")
	s="0";

	while(s.length()>0 && s[s.length()-1]=='0')
		s.erase(s.length()-1,1);
	
	if(s=="")
	s="0";
	return s;
}


string addition(string s1,string s2)
{
	int i;
	while(s1.length()<s2.length())
		s1="0"+s1;
	while(s1.length()>s2.length())
		s2="0"+s2;
	s1="0"+s1;
	s2="0"+s2;
  	for(i=s1.length()-1;i>=0;i--)
	{
		s1[i]+=s2[i]-'0';
		if(s1[i]>'9')
		{
			s1[i]-=10;
			s1[i-1]+=1;
		}
	}
	return clear(s1);
}

string multiply(string s1,string s2)
{
int i;
char C;
string Result;
int str1=0,str2=0;
string s11,s12;

	if(s1=="0" || s2=="0") return "0";
	for(i=0;i<s1.length();i++)
	{
		if(s1[i]=='.') {
			str1=s1.length()-1-i;
			s1.erase(i,1);
			break;
			}
		if(i==s1.length()-1 && s1[s1.length()-1]!='.')
		str1=0;
	}
	for(i=0;i<s2.length();i++)
	{
		if(s2[i]=='.') {
			str2=s2.length()-1-i;
			s2.erase(i,1);
			break;
			}
		if(i==s2.length()-1 && s2[s2.length()-1]!='.')
		str2=0;
	}
    Result="0";
    for(i=s2.length()-1;i>=0;i--)
	{   
         for(C='1';C<=s2[i];C++)
         Result=addition(Result,s1);

         s1+="0";
	}
	if(str1+str2==0)	return clear(Result);
	else if(str1+str2<Result.length())
		Result.insert(Result.length()-str1-str2,".");
	else if(str1+str2==Result.length())
	{
		Result="0."+Result;
	}
	else
	{
		while(str1+str2>Result.length())
			Result="0"+Result;
		Result="0."+Result;
	}
	for(i=0;i<Result.length();i++)
	{
		if(Result[i]=='.') {
			s11=Result.substr(0,i);
			s12=Result.substr(i+1);
			break;
			}
	}
	s11=clear(s11);
	s12=subclear(s12);
	
	if(s12=="0") return s11;
	else if(s11=="0") return "."+s12;
	else return s11+"."+s12;
}

string mul(string s,int n)
{
	int i=0;
	if(n==0) return "1";
	if(n==1) return s;
	else if(n==2) return multiply(s,s);
	else return n%2==0?multiply(mul(s,n/2),mul(s,n/2)):multiply(mul(s,(n+1)/2),mul(s,(n-1)/2));
}

int main()
{
	string str;
	int s;
		

	while(cin>>str>>s)
	{
		str=mul(str,s);
		cout<<str<<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