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

RUNTIME ERROR!常见原因有哪些啊?

Posted by suxor at 2005-09-27 21:24:24 on Problem 1001
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#include <math.h>

void convert(string &s,unsigned int n,vector<unsigned int> &v,unsigned int &sh);
void expo(const vector<unsigned int> &v,unsigned int e,vector<unsigned int> &r);
void display(vector<unsigned int> &r,unsigned int sh,vector<string> &str);
void multi(vector<unsigned int> &a,vector<unsigned int> &b,vector<unsigned int> &r);
string inttostr(unsigned int u);


const unsigned long base=10000;
unsigned int main()
{
	const unsigned int num=6;
	string str(num,'0');
	unsigned int n;
	unsigned int shift;
	vector<unsigned int> ivalue;
	vector<unsigned int> result;
	vector<string> strs;

	while(cin>>str>>n)
	{
		convert(str,n,ivalue,shift);
		expo(ivalue,n,result);
		display(result,shift,strs);
		cout<<*(strs.end()-1)<<endl;
		result.clear();
		ivalue.clear();
	}
	//for(unsigned int i=0;i<strs.size();i++)
	//{
		//cout<<strs[i]<<endl;
	//}
	return 0;
}
void convert(string &s,unsigned int n,vector<unsigned int> &v,unsigned int &sh)
{
	unsigned long temp_l;
	unsigned int i=s.length()-1;

	while(s[i]=='0')
	{
		i--;
	}
	
	s.erase(i+1);
	if(s[i]=='.')
	{
		s.erase(i);
		temp_l=atol(s.c_str());
		sh=0;
	}
	else
	{
		sh=(i-s.find('.'))*n;

		string temp_str;
		for(unsigned int j=0;j<=i;j++)
		{
			if(s[j]!='.')
				temp_str+=s[j];
		}
		temp_l=atol(temp_str.c_str());
	}

	v.insert(v.end(),unsigned int(temp_l%base));
	unsigned int t;
	if((t=unsigned int(temp_l/base))!=0)
	{
		v.insert(v.end(),t);
	}
}
void expo(const vector<unsigned int> &v,unsigned int e,vector<unsigned int> & r)
{
	unsigned int num=unsigned int(floor(log(float(e))/log(2.0f)));
	vector<unsigned int> * pv=new vector<unsigned int>[num+1];

	unsigned int i;
	for(i=0;i<v.size();i++)
	{
		pv[0].insert(pv[0].end(),v[i]);
	}

	for(i=1;i<=num;i++)
	{
		multi(pv[i-1],pv[i-1],pv[i]);
	}

	r.clear();
	for(i=0;i<pv[num].size();i++)
	{
		r.insert(r.end(),pv[num][i]);
	}
	e-=(1<<num);
	unsigned int m;
	while(e>0)
	{
		m=unsigned int (floor(log(float(e))/log(2.0)));
		multi(r,pv[m],r);
		e-=(1<<m);
	}

}
void display(vector<unsigned int> &r,unsigned int sh,vector<string> &str)
{
	string s;
	for(int i=r.size()-1;i>=0;i--)
	{
		s+=inttostr(r[i]);
	}
	if(sh<=s.length())
	{
		s.insert(s.length()-sh,1,'.');
	}
	else
	{
		for(unsigned int i=0;i<sh-s.length();i++)
		{
			s.insert(0,"0");
		}
		s.insert(0,".");
	}
	i=0;
	while(s[i]=='0') s.erase(s.begin());
	str.insert(str.end(),s);
}
void multi(vector<unsigned int> & a,vector<unsigned int> &b,vector<unsigned int> &r)
{
	int i,j;
	int m=a.size();
	int n=b.size();
	vector<int> a1,b1;

	for(i=0;i<m;i++)
	{
		a1.insert(a1.end(),a[i]);
	}
	for(i=0;i<n;i++)
	{
		b1.insert(b1.end(),b[i]);
	}
	r.clear();
	r.insert(r.end(),m+n+1,0);

	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			unsigned long t=a1[i]*b1[j];
			unsigned int m1=t%base;
			unsigned int n1=t/base;
			unsigned long p=m1+r[i+j];
			r[i+j]=p%base;
			p/=base;
			unsigned long q=n1+p+r[i+j+1];
			int k=i+j+1;
			unsigned int t1;
			while((t1=q/base)!=0)
			{
				r[k]=q%base;
				q=t1+r[k+1];
				k++;
			}
			r[k]=q;
		}
	}
	if(r[i+j+1]==0) r.erase(r.end());
}
string inttostr(unsigned int u)
{
	string s(4,'0');
	for(int i=3;i>=0;i--)
	{
		s[i]=char(u%10+0x30);
		u/=10;
	}
	return 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