c语言7-1 多项式a除以b,7-10 多项式A除以B (25分)(多项式除法)

7-10 多项式A除以B (25分)

这仍然是一道关于A/B的题,只不过A和B都换成了多项式。你需要计算两个多项式相除的商Q和余R,其中R的阶数必须小于B的阶数。

输入格式:

输入分两行,每行给出一个非零多项式,先给出A,再给出B。每行的格式如下:

N e[1] c[1] ... e[N] c[N]

其中N是该多项式非零项的个数,e[i]是第i个非零项的指数,c[i]是第i个非零项的系数。各项按照指数递减的顺序给出,保证所有指数是各不相同的非负整数,所有系数是非零整数,所有整数在整型范围内。

输出格式:

分两行先后输出商和余,输出格式与输入格式相同,输出的系数保留小数点后1位。同行数字间以1个空格分隔,行首尾不得有多余空格。注意:零多项式是一个特殊多项式,对应输出为0 0 0.0。但非零多项式不能输出零系数(包括舍入后为0.0)的项。在样例中,余多项式其实有常数项-1/27,但因其舍入后为0.0,故不输出。

输入样例:

4 4 1 2 -3 1 -1 0 -1

3 2 3 1 -2 0 1

输出样例:

3 2 0.3 1 0.2 0 -1.0

1 1 -3.1

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define inf 0x3f3f3f3f

using namespace std;

typedef long long LL;

typedef pair pll;

const int maxn= 2e5+10;

const int mod =1e9+7;

const double EPS = 1e-6;

/*bool cmp()

{

}*/

double a1[maxn],a2[maxn],a3[maxn];

//a1为同时为被除数和余数,a2为除数,a3为商

void solve(double a[],int maxx)

{

int no=0;

for(int i=maxx;i>=0;i--)

{

if(abs(a[i])+0.05>=0.1)

no++;

}

if(no==0) cout << "0 0 0.0" << endl;

else

{

cout << no ;

for(int i=maxx;i>=0;i--)

{

if(abs(a[i])+0.05>=0.1)

printf(" %d %.1f",i,a[i]);

}

cout <

}

}

int main()

{

/* ios::sync_with_stdio(false);

cin.tie(0);

cout.tie(0);*/

int n;

cin >>n;

int maxnum1=-1,maxnum2=-1;

for(int i=1;i<=n;i++)

{

int x;

cin >> x;

cin >> a1[x];

maxnum1=max(maxnum1,x);

}

int m;

cin >> m;

for(int i=1;i<=m;i++)

{

int x;

cin >> x;

cin >> a2[x];

maxnum2=max(maxnum2,x);

}

int cnt1=maxnum1,cnt2=maxnum2;

while(cnt1>=cnt2)

{

double num=a1[cnt1]/a2[cnt2];

a3[cnt1-cnt2]=num;

for(int i=cnt1,j=cnt2;j>=0;i--,j--)

{

a1[i]-=a2[j]*num;

}

while(abs(a1[cnt1])<0.001) cnt1--;

}

//cout << 6666 <

solve(a3,maxnum1-maxnum2);

solve(a1,maxnum1);

return 0;

}

标签:25,10,int,多项式,a1,cnt2,cnt1,include

来源: https://www.cnblogs.com/wjc2021/p/12374325.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值