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<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<utility>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<bitset>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef pair<int,int> 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 <<endl;
    }
}


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 <<endl;
    solve(a3,maxnum1-maxnum2);
    solve(a1,maxnum1);
    return 0;
}

 

posted @ 2020-02-27 20:54  晴天要下雨  阅读(529)  评论(0编辑  收藏  举报