1002. A+B for Polynomials (25)
来源:互联网 发布:华为mate9网络频段 编辑:程序博客网 时间:2024/06/05 00:21
This time, you are supposed to find A+B where A and B are two polynomials.
Input
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the
exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.
Output
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
polynomials 多项式
exponents 指数
coefficients 系数
思路: 整一个数组a,存系数,而a的下标表示指数,对于每一个输入,把指数下标的那个空间放系数之和,
然后遍历一遍,找系数不是0的,把下标存到一个数组b中,遍历这个数组,输出b,并且输出相应b的a。
注意,如果一开始b中的个数是0,说明系数都是0,那么就只输出0。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
double a[1001];
int main()
{
//初始化a,b
memset(a,0.0,sizeof(a));
//输入
int ka, kb;
cin >> ka;
for (int i = 0; i < ka; i++)
{
int zhi;
double xi;
cin >> zhi >> xi;
a[zhi] += xi;
}
cin >> kb;
for (int i = 0; i < kb; i++)
{
int zhi;
double xi;
cin >> zhi >> xi;
a[zhi] += xi;
}
//遍历一遍a数组,找不是0的
int res[1001];
int cou = 0;
for (int i = 1001; i >= 0; --i)
{
if (a[i] != 0.0)
res[cou++] = i;
}
//输出结果
cout << cou;
if (cou != 0)
cout << " "; //如果是0的话后面的空格就不用输出了
for (int i = 0; i < cou; i++)
{
if (i == 0)
printf("%d %.1f",res[i],a[res[i]]);
else
printf(" %d %.1f",res[i],a[res[i]]);
}
return 0;
}
Input
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the
exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.
Output
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
polynomials 多项式
exponents 指数
coefficients 系数
题目大意:计算A+B的和,A和B都是多项式
思路: 整一个数组a,存系数,而a的下标表示指数,对于每一个输入,把指数下标的那个空间放系数之和,
然后遍历一遍,找系数不是0的,把下标存到一个数组b中,遍历这个数组,输出b,并且输出相应b的a。
注意,如果一开始b中的个数是0,说明系数都是0,那么就只输出0。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
double a[1001];
int main()
{
//初始化a,b
memset(a,0.0,sizeof(a));
//输入
int ka, kb;
cin >> ka;
for (int i = 0; i < ka; i++)
{
int zhi;
double xi;
cin >> zhi >> xi;
a[zhi] += xi;
}
cin >> kb;
for (int i = 0; i < kb; i++)
{
int zhi;
double xi;
cin >> zhi >> xi;
a[zhi] += xi;
}
//遍历一遍a数组,找不是0的
int res[1001];
int cou = 0;
for (int i = 1001; i >= 0; --i)
{
if (a[i] != 0.0)
res[cou++] = i;
}
//输出结果
cout << cou;
if (cou != 0)
cout << " "; //如果是0的话后面的空格就不用输出了
for (int i = 0; i < cou; i++)
{
if (i == 0)
printf("%d %.1f",res[i],a[res[i]]);
else
printf(" %d %.1f",res[i],a[res[i]]);
}
return 0;
}
阅读全文
0 0
- 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 【PAT】1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- PAT 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- [PAT]1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- pat 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- PAT 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- Linux中chown和chmod的区别和用法
- Linux——系统分区之分区与格式化
- html5可缩放时钟
- C++模拟ATM
- Linux使用笔记
- 1002. A+B for Polynomials (25)
- openjudge 6256 带通配符的字符串匹配(动态规划)
- Linux网络编程——I/O多路复用
- 17.8.27 校内赛 解题报告【卢卡斯定理】【dfs+hash】【线段树】
- 小白文科生眼中的Linux系统
- Spring 事务详解
- 51Nod 迷宫游戏
- 匈牙利模板--poj 3041最小匹配
- 【asp.net】小结