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 CydorniaKnight at 2009-04-05 21:58:05 on Problem 3727
#include<iostream>
#include<cmath>
using namespace std;

double x0,x1;
int num[1000],_num[1000],num2[1000],_num2[1000],n,_n,ans;
double f,fp;
const double eps=1e-6;
char str;

void Newton_root()
{
	int i,j;
	num2[0]=0;
	for(i=1;i<=n;++i)
		num2[i-1]=i*num[i];
	for(i=1;i<=_n;++i)
		_num2[i+1]=-i*_num[i];
	for(ans=0;ans<=1000;++ans)
	{
		for(f=num[0],i=1;i<=n;++i)
			f+=pow(x0,double(i))*double(num[i]);
		for(i=1;i<=_n;++i)
			f+=pow(x0,double(-i))*double(_num[i]);
		for(fp=num2[0],i=1;i<n;++i)
			fp+=pow(x0,double(i))*double(num2[i]);
		for(i=2;i<=_n+1;++i)
			fp+=pow(x0,double(-i))*double(_num2[i]);
		if(0==fp){
			ans=10000;
			return;
		}
		x1=x0-f/fp;
//		cout<<x1<<"="<<x0<<"-"<<f<<"/"<<fp<<endl;//
		if(fabs(f)<eps) return;
		if((fabs(x1-x0))<eps){
			++ans;
			return;
		}
		x0=x1;
	}
}

int main()
{
	int tmp,p,i;
	while(scanf("%d",&tmp)!=EOF){
		memset(num,0,sizeof(num));//正数项
		memset(_num,0,sizeof(_num));//负数项
		_n=n=0;
		do{
			scanf("%c",&str);
			if(' '==str){
				num[0]+=tmp;
			}else if('x'==str){
				scanf("%c",&str);
				if(' '==str){
					num[1]+=tmp;
					if(n<1) n=1;
				}else{
					scanf("%d",&p);
					if(p>0){
						num[p]+=tmp;
						if(n<p) n=p;
					}else{
						_num[-p]+=tmp;
						if(_n<-p) _n=-p;
					}
				}
			}else if('='==str)
				break;
			scanf("%d",&tmp);
		}while(1);
		scanf("%d",&tmp);
		scanf("%lf",&x0);
		Newton_root();
		if(ans>1000) printf("Bad x0 or bad equation!\n");
		else printf("%d\n",ans);
	}
	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