求助,最后两个点WA
查看原帖
求助,最后两个点WA
178195
人间温柔楼主2020/5/5 17:12

我的大致思路: 通过求根公式

x1=b+b24ac2ax_1=\frac{-b+\sqrt{b^2-4ac}}{2a} x2=bb24ac2ax_2=\frac{-b-\sqrt{b^2-4ac}}{2a}

因式分解后得到(xx1)(xx2)(x-x_1)(x-x_2),但是为啥最后22个点WAWA

我的代码:

#include<bits/stdc++.h>
using namespace std;
string s;
string num;
int a,b,c;
int x1,x2;
int main()
{
	cin>>s;
	a=1;
	s.erase(0,3);
	int pos=s.find('x');
	//cout<<pos<<endl;
	if(pos==-1)
	{
		b=0;
	}
	else
	{
		num=s.substr(0,pos);
		b=0;
		for(int i=1;i<num.size();i++)
		{
			b=b*10+num[i]-'0';
		}
		b=b* ((num[0]=='-') ? -1 : 1);
	}
	if(b!=0)
	{
		if(pos==-1)
		{
			s.erase(0,num.size());
		}
		else
		{
			s.erase(0,num.size()+1);
		}
	}
	//cout<<s<<endl;
	if(s=="+x" || s=="-x")
	{
		c=1;
	}
	else
	{
		for(int i=1;i<s.size();i++)
		{
			c=c*10+s[i]-'0';
		}
		c=c* ((s[0]=='-') ? -1 : 1);
	}
	//2a分之-b加减根号b方-4ac 
	x1=((-b) + sqrt( b*b -4*a*c) ) / (2*a);
	x2=((-b) - sqrt( b*b -4*a*c) ) / (2*a);
	//cout<<a<<"  "<<b<<"  "<<c<<"  "<<x1<<"  "<<x2<<"  ";
	if(x1==x2 && x1!=0)
	{
		cout<<"(x";
		if(x1>0)
		{
			cout<<"-";
		}
		if(x1<0)
		{
			cout<<"+";
			x1=abs(x1);
		}
		cout<<x1<<")^2";
		return 0;
	}
	if(x1==0 && x2==0)
	{
		cout<<"x^2";
		return 0;
	}
	if(x1==0 && x2!=0)
	{
		cout<<"(x";
		if(x2>0)
		{
			cout<<"-";
		}
		if(x2<0)
		{
			cout<<"+";
			x2=abs(x2);
		}
		cout<<x2<<")x";
		return 0;
	}
	if(x2==0 && x1!=0)
	{
		cout<<"(x";
		if(x1>0)
		{
			cout<<"-";
		}
		if(x1<0)
		{
			cout<<"+";
			x1=abs(x1);
		}
		cout<<x1<<")x";
		return 0;
	}
	if(x1!=0 && x2!=0)
	{
		cout<<"(x";
		int max_x=max(x1,x2);
		int min_x=min(x1,x2);
		if(min_x>0)
		{
			cout<<"-";
		}
		if(min_x<0)
		{
			cout<<"+";
			min_x=abs(min_x);
		}
		cout<<min_x<<")(x";
		if(max_x>0)
		{
			cout<<"-";
		}
		if(max_x<0)
		{
			cout<<"+";
			max_x=abs(max_x);
		}
		cout<<max_x<<")";
		return 0;
	}
}
2020/5/5 17:12
加载中...