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 ...
随机推荐
- DRF的封装:APIView类及五大模块
目录 一.drf框架的封装特点 1.APIView类 二.drf的基础组件 1.请求模块 1.1 请求模块做了什么 1.2 请求request参数 2.解析模块 3.响应模块 4.渲染模块(了解) 5 ...
- 微信小程序去除页面滚动条
::-webkit-scrollbar { width: 0; height: 0; color: transparent; display: none; } 父级元素(滚动的元素) width:10 ...
- POJ-3436(网络流+最大流+输出路径)
ACM Computer Factory POJ-3436 题目就是一个工厂n个加工机器,每个机器有一个效率w,q个材料入口,q个材料出口,每个口有三个数表示状态,1表示一定有入/出的材料,0表示没有 ...
- Web微信协议
[文档]Web微信协议 1.登录 1.1 获取uuid(get) 请求 https://login.wx.qq.com/jslogin?appid=wx782c26e4c19acffb&red ...
- linux安装uwsgi,报错问题解决
uwsgi安装 uwsgi启动后出 -- unavailable modifier requested: 0 出现问题的的原因是找不到python的解释器(其他语言同理) 你使用的yum instal ...
- MyBatis(三):自定义持久层框架实现
代码已上传至码云:https://gitee.com/rangers-sun/mybatis 新建Maven工程 架构端MyPersistent.使用端MyPersistentTest,使用端引入架构 ...
- FreeBSD 中文TTY控制台
freebsd新型终端VT,支持cjk,所以丢个字体进去,就能显示中文了1,首先你没有改过控制台程序,使用的是默认的,,2,最新版本,本说明是以freebsd12.1release字体格式为.fnt命 ...
- (3)MySQL进阶篇SQL优化(索引)
1.索引问题 索引是数据库优化中最常用也是最重要的手段之一,通过索引通常可以帮助用户解决大多数 的SQL性能问题.本章节将对MySQL中的索引的分类.存储.使用方法做详细的介绍. 2.索引的存储分类 ...
- Mac下安装lightgb并在jupyter中使用
1.先安装cmake和gcc brew install cmake brew install gcc 2.下载后确定自己的gcc版本 cd /usr/local/opt/gcc/lib/gcc/ 看到 ...
- Python基础之:Python中的IO
目录 简介 linux输入输出 格式化输出 f格式化 format格式化 repr和str %格式化方法 读写文件 文件对象的方法 使用json 简介 IO就是输入和输出,任何一个程序如果和外部希望有 ...