1002 A+B for Polynomials (25分)
 

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (,) are the exponents and coefficients, respectively. It is given that 1,0.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5
 

Sample Output:

3 2 1.5 1 2.9 0 3.2

  0是零多项式,在题中将之归于“0项”多项式。当然了,实际是没有0项多项式的,只有零多项式,但是非要输出个结果,0还是合理的

我用了另一种思路,用数组模仿链表的实现。将A、B两多项式由次数从高到低依次计算,存入新数组。开个1000 的数组实在是浪费空间

  疑问:

    最初最后一个测试点没过的时候,我以为又是小负数近似格式的问题,便将系数绝对值小于0.05的项全忽略了。

#include <math.h>
#include <stdio.h>
#include <stdlib.h> typedef struct Poly
{
double coef;
int exp;
} Poly[]; int main(void)
{
int Ka, Kb, Ksum = ;
Poly A, B, Sum; scanf("%d", &Ka);
for (int i = ; i < Ka; ++i)
{
scanf("%d %lf", &A[i].exp, &A[i].coef);
} scanf("%d", &Kb);
for (int i = ; i < Kb; ++i)
{
scanf("%d %lf", &B[i].exp, &B[i].coef);
} int i = , j = ;
while (i < Ka || j < Kb)
{ //类似于链表的多项式相加
if (i == Ka || (j < Kb && A[i].exp < B[j].exp))
{ //多项式B长 或者 多项式B指数在A中不存在
Sum[Ksum].exp = B[j].exp;
Sum[Ksum].coef = B[j++].coef;
}
else if (j == Kb || (i < Ka && A[i].exp > B[j].exp))
{ //多项式A长 或者 多项式A指数在B中不存在
Sum[Ksum].exp = A[i].exp;
Sum[Ksum].coef = A[i++].coef;
}
else
{ //
Sum[Ksum].exp = A[i].exp;
Sum[Ksum].coef = A[i++].coef + B[j++].coef;
}
if (fabs(Sum[Ksum].coef) >= 0.05)
{ //小于这个值的被舍去了
Ksum++;
}
} printf("%d", Ksum); for (int i = ; i < Ksum; i++)
{
printf(" %d %.1lf", Sum[i].exp, Sum[i].coef);
} return ;
}

C++版本:

  用了Map和vector,MAP, 将整型的exp最为Map的关键字,再用二维的vector存储结果

#include <iostream>
#include <map>
#include <vector> using namespace std; int main(void)
{
map<int, float> poly1;
int K, exp;
float cof; scanf("%d", &K);
for (int i = ; i < K; ++i)
{ scanf("%d%f", &exp, &cof);
poly1[exp] += cof;
} scanf("%d", &K);
for (int i = ; i < K; ++i)
{ scanf("%d%f", &exp, &cof);
poly1[exp] += cof;
} vector<pair<int, float>> res;
for (map<int, float>::reverse_iterator it = poly1.rbegin(); it != poly1.rend(); it++)
{
if (it->second != ) // 两个系数和为0的得过滤掉
{
res.push_back(make_pair(it->first, it->second));
}
} printf("%lu", res.size());
for (int i = ; i < res.size(); ++i)
{
printf(" %d %.1f", res[i].first, res[i].second);
}
return ;
}

PAT不易,诸君共勉!

P1002 A+B for Polynomials (25分)的更多相关文章

  1. 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 ...

  2. PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值

    题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...

  3. PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...

  4. PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)

    This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...

  5. PAT Advanced 1002 A+B for Polynomials (25 分)(隐藏条件,多项式的系数不能为0)

    This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each ...

  6. PAT 1002 A+B for Polynomials (25分)

    题目 This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: E ...

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

    This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each ...

  8. 【PAT甲级】1002 A+B for Polynomials (25 分)

    题意:给出两个多项式,计算两个多项式的和,并以指数从大到小输出多项式的指数个数,指数和系数. AAAAAccepted code: #include<bits/stdc++.h> usin ...

  9. 【PAT甲级】1009 Product of Polynomials (25 分)

    题意: 给出两个多项式,计算两个多项式的积,并以指数从大到小输出多项式的指数个数,指数和系数. trick: 这道题数据未知,导致测试的时候发现不了问题所在. 用set统计非零项时,通过set.siz ...

随机推荐

  1. C语言:从p所指字符串中找出ASCII码最大的字符,将其放在第一个位置上,并将该字符前的原字符向后顺序移动。-使字符串的前导*号不得多于n个,若多余n个,则删除多余的*号,

    //fun函数:从p所指字符串中找出ASCII码最大的字符,将其放在第一个位置上,并将该字符前的原字符向后顺序移动. #include <stdio.h> void fun( char * ...

  2. 计算机二级-C语言-程序修改题-190123记录-对整数进行取余和除以操作。

    //函数fun功能:将长整型数中每一位上为偶数的数依次取出,构成一个新数放在t中.高位仍在高位,低位仍在低位. //重难点:思路:因为不是字符串,所以可以把问题变成整数的操作,采用取余和除的操作.对整 ...

  3. Json日期格式化,出去返回的T

    第一种办法:前端JS转换: //格式化显示json日期格式 function showDate(jsonDate) { var date = new Date(jsonDate); var forma ...

  4. twisted task.cpperator

    twisted task.cpperator 1.      twisted task.cpperator 1.1.    简介-cooperator 官方文档: https://twistedmat ...

  5. Spring Boot 中使用 HttpClient 进行 POST GET PUT DELETE

    有的时候,我们的 Spring Boot 应用需要调用第三方接口,这个接口可能是 Http协议.可能是 WebService.可能是 FTP或其他格式,本章讨论 Http 接口的调用. 通常基于 Ht ...

  6. 谈一谈并查集QAQ(上)

    最近几日理了理学过的很多oi知识...发现不知不觉就有很多的知识忘记了... 在聊聊并查集的时候顺便当作巩固吧.... 什么是并查集呢? ( Union Find Set ) 是一种用于处理分离集合的 ...

  7. The Captain 题解

    20200216题目题解 这是一篇题解祭题解记,但一共就一道题目.(ROS菜大了) 题目如下: The Captain 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x ...

  8. RestTemplate post请求使用map传参 Controller 接收不到值的解决方案 postForObject方法源码解析.md

    结论 post方法中如果使用map传参,需要使用MultiValueMap来传递 RestTemplate 的 postForObject 方法有四个参数 String url => 顾名思义 ...

  9. 《JavaScript高级程序设计》读书笔记(序)

    1.现大三暑假中,计划9月初北上找前端工作,大三一年时间都在健身和学习专业课知识,技术有点荒废了,7月份忙于学校安排的实习javaweb方向的,到现在才有整段的时间好好把基础巩固一. 2.这几天也在关 ...

  10. 阿里云linux挂载磁盘

    1)使用fdisk -l命令查看主机上的硬盘 2.使用mkfs.ext4命令把硬盘格式化: mkfs.ext4    磁盘名称 如:mkfs.ext4   /dev/vdb/ 3. 使用mount命令 ...