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 N1 aN1 N2 aN2 ... NK aNK
where K is the number of nonzero terms in the polynomial, Ni and aNi (,) 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
题解:用C++写了两次只通过了部分数据额,最后看别人的博客是用C语言来写的,确实,用C++来控制输入输出的格式不太好控制。
#include<iostream>
using namespace std;
int main() {
float a[1001] = {0};
int k;
int exp;
float coe;
cin >> k;
for(int i = 0; i < k; ++i) {
cin >> exp >> coe;
a[exp] += coe;
}
cin >> k;
for(int i = 0; i < k; ++i) {
cin >> exp >> coe;
a[exp] += coe;
}
int count = 0;
for (int i = 0; i < 1001; ++i) {
if (a[i] != 0) count++;
}
cout << count;
for (int i = 1000; i >= 0; --i) {
if (a[i] > 0)
cout << " " << i << " " << a[i];
}
}
2021-01-28
Python列表表达式:[ expression for i in iterable ]
poly = [0 for _ in range(1001)]按照习惯,有时候单个独立下划线是用作一个名字,来表示某个变量是临时的或无关紧要的。
例如,在下面的循环中,我们不需要访问正在运行的索引,我们可以使用“_”来表示它只是一个临时值
poly = [0 for _ in range(1001)] def add():
global poly
line = input().split()[1:]
i = 0
while i < len(line) - 1:
poly[int(line[i])] += float(line[i + 1])
i += 2 add()
add() count = 0
for i in range(1000, -1, -1):
if poly[i] != 0:
count += 1 print(count, end='')
for i in range(1000, -1, -1):
if poly[i] != 0:
print(" %d %.1f" % (i, poly[i]), end='')
1002 A+B for Polynomials (25分)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 【PAT甲级】1002 A+B for Polynomials (25 分)
题意:给出两个多项式,计算两个多项式的和,并以指数从大到小输出多项式的指数个数,指数和系数. AAAAAccepted code: #include<bits/stdc++.h> usin ...
- 1002 A+B for Polynomials (25分) 格式错误
算法笔记上能踩的坑都踩了. #include<iostream> using namespace std; float a[1001];//至少1000个位置 int main(){ in ...
- P1002 A+B for Polynomials (25分)
1002 A+B for Polynomials (25分) This time, you are supposed to find A+B where A and B are two polyn ...
- 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 ...
- 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 ...
- 1002 A+B for Polynomials (25)(25 point(s))
problem 1002 A+B for Polynomials (25)(25 point(s)) This time, you are supposed to find A+B where A a ...
随机推荐
- EFCodeFirst关系映射约定
EFCodeFirst关系映射约定 EFCodeFirst 关系映射约定 默认多重关系的一些约定规则: 1.一对多关系 两个类中分别包含一个引用和一个集合属性. 两个类中一个类包含另一个类的引用属性. ...
- 关于Java中for,while,if,方法的练习
练习 计算0到100之间的奇数和偶数和 package com.kangkang.forDemo;public class demo01 { public static void main(S ...
- 【python3】 解:import导包机制
模块和包 模块:我们定义的.py结尾的文件就是一个模块,模块中通常定义了类.方法.变量等一系列功能: 包:存放模块的文件夹,含有init.py文件,定义path属性. import语句的作用 impo ...
- Hi3559AV100板载开发系列-pthread_create()下V4L2接口MJPEG像素格式的VIDIOC_DQBUF error问题解决-采用阻塞方式下select监听
最近一直加班加点进行基于Hi3559AV100平台的BOXER-8410AI板载开发,在开发的过程中,遇到了相当多的问题,其一是板载的开发资料没有且功能不完整,厂家不提供太多售后技术支持,厂家对部分 ...
- 使用 SVG transform rotate 解决画框中的数字跟随旋转的问题
问题描述 在图片上画框标注数字,旋转画布后,数字随之旋转,可读性不强,要求修改成无论画布怎么旋转,数字都是正向显示~ 原交互图示: 解决方案 先看下 dom 的结构 然后看下下面简单的代码 // 获取 ...
- 记录PHP post提交表单导入mysql中文乱码的问题
记录记录PHP post提交表单导入mysql中文乱码的问题 关于乱码,这是个糟糕的问题!涉及到很多地方 解决思路:程序所涉及的环境字符集不一致导致 mysql出现乱码一般是mysql数据库内部的字符 ...
- 【译】Visual Studio 的 Razor 编辑器的改进
自从我们在一个通用的 Razor 语言服务器上发布了 Visual Studio 的一个新的实验性 Razor 编辑器的第一个预览版以来,已经过去了6个月,现在是时候更新一下我们的进展了.团队一直在努 ...
- sitemesh简单介绍
SiteMesh 是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的. Sitemesh是由一个基于Web页面布局.装饰以及与现存Web应用整合的框架. 它能帮 ...
- 2019 GDUT Rating Contest I : Problem E. Convention
题面: E. Convention Input file: standard input Output file: standard output Time limit: 1 second Memory ...
- KVM虚拟化配置
KVM虚拟化 虚拟化概念 KVM虚拟化概念详讲 虚拟化配置 首先开启虚拟化的支持 并且增加一个50g的硬盘 free查看内存 grep -Ei 'vmx|svm' /proc/cpuinfo查看虚拟机 ...