题目链接:https://pintia.cn/problem-sets/1108548596745592832/problems/1108548661014913033

题目大意:

这仍然是一道关于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,故不输出。

具体思路:模拟多项式除法,注意精度控制。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int maxn = 1e5+;
double a[maxn],b[maxn],c[maxn];
int main()
{
int n,m,tmp,maxa,maxb;
scanf("%d",&n);
for(int i=; i<n; i++)
{
scanf("%d",&tmp);
scanf("%lf",&a[tmp]);
if(i==)
maxa=tmp;
}
scanf("%d",&m);
for(int i=; i<m; i++)
{
scanf("%d",&tmp);
scanf("%lf",&b[tmp]);
if(i==)
maxb=tmp;
}
int j;
for(int i=maxa; i>=maxb; i--)
{
c[i-maxb]=a[i]/b[maxb];
for( j=maxb; j>=; j--)
{
a[i+j-maxb]-=b[j]*c[i-maxb];
}
}
int num1=,num2=;
for(int i=maxa-maxb; i>=; i--)
{
if(fabs(c[i])>1e-)
{
if(fabs(c[i])<0.05)
c[i]=;
else
num1++;
}
}
if(num1==)
{
printf("0 0 0.0\n");
}
else
{
printf("%d",num1);
for(int i=maxa-maxb; i>=; i--)
{
if(fabs(c[i])>1e-)
printf(" %d %.1lf",i,c[i]);
}
printf("\n");
}
for(int i=maxb-; i>=; i--)
{
if(fabs(a[i])>1e-)
{
// cout<<a[i]<<endl;
if(fabs(a[i])<0.05)
a[i]=;
else
num2++;
}
}
if(num2==)
{
printf("0 0 0.0\n");
}
else
{
printf("%d",num2);
for(int i=maxb-; i>=; i--)
{
if(fabs(a[i])>1e-)
printf(" %d %.1lf",i,a[i]);
}
printf("\n");
}
}

7-10 多项式A除以B (25 分)的更多相关文章

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

    7-10 多项式A除以B (25分)   这仍然是一道关于A/B的题,只不过A和B都换成了多项式.你需要计算两个多项式相除的商Q和余R,其中R的阶数必须小于B的阶数. 输入格式: 输入分两行,每行给出 ...

  2. 9.9递归和动态规划(八)——给定数量不限的硬币,币值为25分,10分,5分,1分,计算n分有几种表示法

    /**  * 功能:给定数量不限的硬币.币值为25分,10分.5分.1分,计算n分有几种表示法. */ public static int makeChange(int n){ return make ...

  3. 1009 Product of Polynomials (25分) 多项式乘法

    1009 Product of Polynomials (25分)   This time, you are supposed to find A×B where A and B are two po ...

  4. 多项式A除以B

    这个问题我是在PAT大区赛题里遇见的.题目如下: 多项式A除以B(25 分) 这仍然是一道关于A/B的题,只不过A和B都换成了多项式.你需要计算两个多项式相除的商Q和余R,其中R的阶数必须小于B的阶数 ...

  5. 1009 Product of Polynomials (25 分)

    1009 Product of Polynomials (25 分) This time, you are supposed to find A×B where A and B are two pol ...

  6. PAT 甲级 1009 Product of Polynomials (25)(25 分)(坑比较多,a可能很大,a也有可能是负数,回头再看看)

    1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are two ...

  7. PAT甲级 1002 A+B for Polynomials (25)(25 分)

    1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...

  8. A1082 Read Number in Chinese (25)(25 分)

    A1082 Read Number in Chinese (25)(25 分) Given an integer with no more than 9 digits, you are suppose ...

  9. A1009 Product of Polynomials (25)(25 分)

    A1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are tw ...

  10. PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)

    1145 Hashing - Average Search Time (25 分)   The task of this problem is simple: insert a sequence of ...

随机推荐

  1. Error[Pe020]: identifier "FILE" is undefined

    Error[Pe020]: identifier "FILE" is undefined 需要添加头文件:#include <stdio.h>

  2. Centos 配置eth0 提示Device does not seem to be present

    Centos 配置eth0 提示Device does not seem to be present 参考资料: http://www.cnblogs.com/fbwfbi/archive/2013/ ...

  3. flask session

    flask session工作机制: 把敏感数据经过加密后放入到‘session’中,然后在把'session'存放到cookie中,下次请求的时候,再从浏览器发送过来的cookie中读取sessio ...

  4. centos 7.4 安装gitlab

    centos 7.4 安装gitlab #curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/scrip ...

  5. oracle中查看所有表和字段以及表注释字段注释

    获取表:select table_name from user_tables; //当前用户拥有的表 select table_name from all_tables; //所有用户的表 selec ...

  6. jmeter元件执行顺序及简介

    最近在学习Jmeter,在进行实操之前,先查看了官方文档.因为官方文档是英文的,为了方便以后查看,自己翻译了一部分,中间个别地方根据自己的理解简单地翻译了部分.如果翻译等有问题,欢迎指正. 一.执行顺 ...

  7. java读取配置文件信息

    ResourceBundle resource = ResourceBundle.getBundle("shopxx");//不要加.properties后缀,我加了报错 reso ...

  8. 在Java中如何高效的判断数组中是否包含某个元素

    原文出处: hollischuang(@Hollis_Chuang) 如何检查一个数组(无序)是否包含一个特定的值?这是一个在Java中经常用到的并且非常有用的操作.同时,这个问题在Stack Ove ...

  9. 他山之石--机器学习 step by step

    练习使用的数据 diabetes.csv 备用百度网盘地址 输入变量与输出变量均为连续变量的预测问题是回归问题: 输出变量为有限个离散变量的预测问题成为分类问题: 其实回归问题和分类问题的本质一样,都 ...

  10. java四种权限修饰符(public > protected > (default) > private)

    权限修饰符在哪里可以访问 (default) : 表示什么权限修饰符都不写 位置 public protected (default) private 同一个类 yes yes yes yes 同一个 ...